開發中,有時候會涉及到需要獲取一個byte數據某一位上的值0/1.
byte 8位,現在我自己bit編號,從左到右為7,6,5,4,3,2,1,0
//返回true表示所取位值為1,返回false,表示所取位值為0
//bit索引下標依次為 7,6,5,4,3,2,1,0
private bool GetBitValue(byte value, byte bit)
{
return (value & (byte) Math.Pow(2, bit)) > 0 ? true : false;
}
自己工作中遇到需要對單字節的高位、低位進行賦值,即一個字節byte,想要給每一位都賦值,這個值是動態來的,是0或是1。
好不容易收集到一些珍貴資料,整理一下:
一、設置
方法code:
/// <summary>
/// 設置某一位的值
/// </summary>
/// <param name="data"></param>
/// <param name="index">要設置的位, 值從低到高為 1-8</param>
/// <param name="flag">要設置的值 true / false</param>
/// <returns></returns>
byte set_bit(byte data, int index, bool flag)
{
if (index > 8 || index < 1)
throw new ArgumentOutOfRangeException();
int v = index < 2 ? index : (2 << (index - 2));
return flag ? (byte)(data | v) : (byte)(data & ~v);
}
調用code:
byte s = set_bit(8, 8, true);
結果:
s 的值為 136, 結果正確。
二、獲取值
獲取一個字節中的每一位的值,需要分別與128 64 32 16 8 4 2 1相與&運算
假設字節為byte1
bit8 = byte1 & 128 == 128 ? 1 : 0;
bit7 = byte1 & 64 == 64 ? 1 : 0;
bit6 = byte1 & 32 == 32 ? 1 : 0;
bit5 = byte1 & 16 == 16 ? 1 : 0;
bit4 = byte1 & 8 == 8 ? 1 : 0;
bit3 = byte1 & 4 == 4 ? 1 : 0;
bit2 = byte1 & 2 == 2 ? 1 : 0;
bit1 = byte1 & 1 == 1 ? 1 : 0;
另外,收集到網絡上的其他資料:
引用:http://topic.csdn.net/u/20100121/11/66a2561e-49de-48d6-b0aa-4f3d1fea62e4.html
你好,感謝你閱讀此帖.
今天我們要討論的是在C#中如何獲取一個數值中的某一位的數據,比如一個Byte型數據8,它的二進制表示為00001000(高位到低位),那我應該怎樣獲取它的第3位的值1呢?
我的想法是這樣的,先把第3位的值右移7-3=4位,然后再右移7位,最后取這個值,這樣就把第3位前后的值都變為0了,最后輸出它的值為1.下面是我寫的一個方法:
- C# code
-
/// <summary> /// 獲取數據中某一位的值 /// </summary> /// <param name="input">傳入的數據類型,可換成其它數據類型,比如Int</param> /// <param name="index">要獲取的第幾位的序號,從0開始</param> /// <returns>返回值為-1表示獲取值失敗</returns> private int GetbitValue(byte input,int index) { if (index > sizeof(byte)) { return -1; } //左移到最高位 int value = input << (sizeof(byte) - 1 - index); //右移到最低位 value = value >> (sizeof(byte) - 1); return value; }
懇請大家指正,另外我想把它變成能處理不同數據類型的方法,比如運用范型,但是不知道怎么使用,請大家幫幫忙.
我看到C#中還有一些位操作的類,比如BitArray,BitVector32,好像都不合適,BitConvert好像也只是針對對字節流的轉換
不知道大家有沒有更好的方法,歡迎大家討論.
祝你工作順利,天天開心.
回復:
想看那一位是1就把第幾位設置為1,其他設置為0,同input進行與操作,返回,大於0則是1,==0則是0.
回復:
//index從0開始
//獲取取第index位
public static int GetBit(byte b, int index) { return ((b & (1 << index)) > 0) ? 1: 0; }
//將第index位設為1
public static byte SetBit(byte b, int index) { return (byte)(b | (1 << index)); }
//將第index位設為0
public static byte ClearBit(byte b, int index) { return (byte)(b & (byte.MaxValue - (1 << index))); }
//將第index位取反
public static byte ReverseBit(byte b, int index) { return (byte)(b ^ (byte)(1 << index)); }
回復:
private static int GetbitValue(byte input, int index)
{
int value;
value = index>0? input >> index-1: input;
return value &= 1;
}
回復:
C# code//每8位為一個字節 private const int bitCout = 8; ///查詢對象內存第index位值 static int GetValueOfIndex(object obj, int index) { int size = Marshal.SizeOf(obj); System.IntPtr intPtr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(obj,intPtr,true); byte[] byteArr = new byte[size]; Marshal.Copy(intPtr,byteArr,0,size); int count; index = Math.DivRem(index, 8, out count); Marshal.FreeHGlobal(intPtr); return (byteArr[size-index-1] >> (8-count-1)) & 1; } for (int i = 0; i < 32; i++) { int j = (int)Math.Pow(2, i); Console.WriteLine(j + " : " + GetValueOfIndex(j, 31 - i)); } Console.WriteLine(15.0f + " : " + GetValueOfIndex(13, 28));