using System;
namespace IndexerSample
{
class Program
{
static void Main(string[] args)
{
IntBits bits = new IntBits(Int32.MaxValue);
printIntBits(bits);
printBinaryString(Int32.MaxValue);
bits.Value = 1;
printIntBits(bits);
printBinaryString(1);
bits.Value = 0;
printIntBits(bits);
printBinaryString(0);
bits.Value = -1;
printIntBits(bits);
printBinaryString(-1);
bits.Value = Int32.MinValue;
printIntBits(bits);
printBinaryString(Int32.MinValue);
}
static void printIntBits(IntBits bits)
{
for (int i = 31; i >= 0; i--)
{
if (bits[i])
{
Console.Write("1");
}
else
{
Console.Write("0");
}
}
Console.WriteLine();
}
static void printBinaryString(int num)
{
string bin = Convert.ToString(num, 2);
bin = bin.PadLeft(32, '0');
Console.WriteLine("{0} : {1}", bin, num);
}
} // class Program
struct IntBits
{
private int bits;
public int Value
{
get { return bits; }
set { bits = value; }
}
public IntBits(int initialBitValue)
{
bits = initialBitValue;
}
// indexer
public bool this[int index]
{
get
{
if (index < 0 || index > 31)
{
throw new ArgumentOutOfRangeException("index");
}
return (bits & (1 << index)) != 0;
}
set
{
if (index < 0 || index > 31)
{
throw new ArgumentOutOfRangeException("index");
}
if (value) // Turn the bit on if value is true, otherwise turn it off
bits |= (1 << index);
else
bits &= ~(1 << index);
}
}
} // struct IntBits
}
namespace IndexerSample
{
class Program
{
static void Main(string[] args)
{
IntBits bits = new IntBits(Int32.MaxValue);
printIntBits(bits);
printBinaryString(Int32.MaxValue);
bits.Value = 1;
printIntBits(bits);
printBinaryString(1);
bits.Value = 0;
printIntBits(bits);
printBinaryString(0);
bits.Value = -1;
printIntBits(bits);
printBinaryString(-1);
bits.Value = Int32.MinValue;
printIntBits(bits);
printBinaryString(Int32.MinValue);
}
static void printIntBits(IntBits bits)
{
for (int i = 31; i >= 0; i--)
{
if (bits[i])
{
Console.Write("1");
}
else
{
Console.Write("0");
}
}
Console.WriteLine();
}
static void printBinaryString(int num)
{
string bin = Convert.ToString(num, 2);
bin = bin.PadLeft(32, '0');
Console.WriteLine("{0} : {1}", bin, num);
}
} // class Program
struct IntBits
{
private int bits;
public int Value
{
get { return bits; }
set { bits = value; }
}
public IntBits(int initialBitValue)
{
bits = initialBitValue;
}
// indexer
public bool this[int index]
{
get
{
if (index < 0 || index > 31)
{
throw new ArgumentOutOfRangeException("index");
}
return (bits & (1 << index)) != 0;
}
set
{
if (index < 0 || index > 31)
{
throw new ArgumentOutOfRangeException("index");
}
if (value) // Turn the bit on if value is true, otherwise turn it off
bits |= (1 << index);
else
bits &= ~(1 << index);
}
}
} // struct IntBits
}
Output
01111111111111111111111111111111
01111111111111111111111111111111 : 2147483647
00000000000000000000000000000001
00000000000000000000000000000001 : 1
00000000000000000000000000000000
00000000000000000000000000000000 : 0
11111111111111111111111111111111
11111111111111111111111111111111 : -1
10000000000000000000000000000000
10000000000000000000000000000000 : -2147483648
01111111111111111111111111111111 : 2147483647
00000000000000000000000000000001
00000000000000000000000000000001 : 1
00000000000000000000000000000000
00000000000000000000000000000000 : 0
11111111111111111111111111111111
11111111111111111111111111111111 : -1
10000000000000000000000000000000
10000000000000000000000000000000 : -2147483648
'Code > C#' 카테고리의 다른 글
| [C#] Exception Tracing Sample Code (0) | 2010.04.16 |
|---|---|
| [C#] Windows UI Automation Sample (0) | 2010.04.13 |
| [C#] Shift Operator (0) | 2010.02.17 |
| [C#] Register/Unregister ActiveX Control (0) | 2009.03.15 |
| [C#] Detect UserControl's Closing (0) | 2009.01.07 |