About Bitmap

BitMap is a useful struct, here is a simple class for get and set

 public class BitMap
{
private const int count = 100;
private byte[] bitfile = new byte[count];

public BitMap()
{
Init();
}
private void Init()
{
for (int i = 0; i < count; i++)
{
bitfile[i] = 0;
}
}

public bool GetBit(int pos)
{
int mod = pos % 8;
int quo = pos >> 3;
int result = ( (int)bitfile[quo]) & (1 << mod) ;
return Convert.ToBoolean(result);
}

public void SetBit(int pos, bool flag)
{
int mod = pos % 8;
int quo = pos>>3;
int result;
if (flag) // true == flag
{
result = ((int)bitfile[quo]) | (1 << mod);
}
else
{
result = ((int)bitfile[quo]) & ((1 << mod) ^ 0xFF);
}
bitfile[quo] = Convert.ToByte(result);
}
}

BitMap bm = new BitMap();
bm.SetBit(9, true);
bm.SetBit(10, true);
bm.SetBit(9, true);
bm.SetBit(10, false);
for (int i = 0; i < 20; i++)
{
Console.WriteLine("getbit : {1} : {0}", bm.GetBit(i), i);
}

 

posted on 2011-11-17 17:07  子虚乌有  阅读(153)  评论(0)    收藏  举报