티스토리 뷰
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 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- iTextSharp
- java
- jre
- DotNetMagic
- windows
- ScreenHunter
- AxWebBrowser
- READYSTATE_COMPLETE
- Automation
- Regular Expressions
- Service pack
- registry
- download.com
- c#
- AdSense감추기
- WinAutomation
- 유틸리티
- Microsoft
- Rollback Rx
- Phalanger
- Sample Code
- 애드센스감추기
- iText
- tagREADYSTATE
- 애드센스숨기기
- AdSense숨기기
- .net framework
- autohotkey
- 스크린캡쳐
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
글 보관함