比如 5个字节的数据
6位表示消息类型
2位表示一个数
30位表示一个码
2位又表示一个数
这样的数据按位取值的要怎么弄呀

解决方案 »

  1.   

    static bool GetBit(int data, int bit)
    {
        int test = 1 << (bit - 1);
        return (data | test) == data;
    }
      

  2.   

    System.Collections.BitArray,或者BitConverter.ToInt64/ToUInt64然后用位运算来取值
      

  3.   

    直接对齐成64位长整,不足部分补0凑数:
    这对现在64位的CPU毫无压力,一秒种可以处理上亿。
    namespace ConsoleApplication6
    {
        class Program
        {
            static byte[] bs = { 231, 132, 120, 97, 65, 0, 0, 0 };
            static void Main(string[] args)
            {
                ulong i64 = BitConverter.ToUInt64(bs, 0);
                Console.WriteLine("{0:x}", getNumber(i64, 0, 6)); //0-6位
                Console.WriteLine("{0:x}", getNumber(i64, 6, 2)); //7-8位
                //.....
                Console.ReadLine();
            }        static ulong getNumber(ulong i64, int start, int len)
            {
                i64 = i64 << 64 - start - len >> 64 - len;
                return i64;
            }
        }
    }