zoukankan      html  css  js  c++  java
  • [转]C# 设置和获取一个字节的某一位的值的方法

    原网页:http://blog.csdn.net/mao_mao37/article/details/52584383

    自己工作中遇到需要对单字节的高位、低位进行赋值,即一个字节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));
    
  • 相关阅读:
    深入浅出SQL教程之Group by和Having
    AFNetworking3.0 Https P12证书
    C#访问注册表
    One reason for not able to show chinese correctly in installation
    Debugging DLL loading issues with GFLAGS
    RegistryFree COM Registration
    RegistrationFree COM Interop
    net use
    MS UI Automation原来如此
    取景器的视野率和放大倍率
  • 原文地址:https://www.cnblogs.com/LOGHG/p/7357701.html
Copyright © 2011-2022 走看看