现在的位置: 首页 > 综合 > 正文

在C#中实现类似位域的操作(转)

2012年05月07日 ⁄ 综合 ⁄ 共 2830字 ⁄ 字号 评论关闭

http://www.cnblogs.com/TianFang/archive/2011/01/23/1942446.html

这篇文章和前面的那篇在C#中实现BigEndian的数字一样,都是在解析网络协议和文件时遇到的。有时协议会规定把一个byte的数字分成几个部分,某一部分表示一个数字。这种情况下在C中可以用位域很简单的实现,但是,在C#中就没那么容易了。这里是我写的一个类,专门用来辅助实现这种操作。

    /// <summary>
    ///
这个类主要用于那种将一个字节分成若干部分,每个部分表示一个数的类型
    /// </summary>
    class BitHelper
    {
        public static byte GetByte(byte data, byte index, byte count)
        {
            Contract.Requires(index + count <= 8);

            return (byte)((data >> (8 - index - count)) % (1 << count));
        }

        public static byte SetByte(int value, byte data, byte index, byte count)
        {
            Contract.Requires(index + count <= 8);
            Contract.Requires(value < (1 << count));

            var tail = data % (1 << (8 - index - count));
            return (byte)((((data >> (8 - index) << count) + value) << index) + tail);
        }

        //测试某一位是否为1
        public static bool TestBit(byte data, byte index)
        {
           
         return GetByte(data, index, 1) == 1;
       
         }      

        public static byte SetBit(bool value, byte data, byte index)
        {
            return SetByte(value ? 1 : 0, data, index, 1);
        }
    }

这个类并不能直接用,也是辅助实现的操作,一般用于在属性中封装某个字段。

   private byte flags = 5;
    public byte Flags
    {
        get { return flags;
     }
        set { flags = value; }
    }

    public bool TypeFlagsAudio
    {
        get { return BitHelper.TestBit(flags, 5); }
        set { flags = BitHelper.SetBit(value, flags, 5); }
    }

    public bool TypeFlagsVideo
    {
        get { return BitHelper.TestBit(flags, 7); }
        set { flags = BitHelper.SetBit(value, flags, 7); }
    }

这篇文章和前面的那篇在C#中实现BigEndian的数字一样,都是在解析网络协议和文件时遇到的。有时协议会规定把一个byte的数字分成几个部分,某一部分表示一个数字。这种情况下在C中可以用位域很简单的实现,但是,在C#中就没那么容易了。这里是我写的一个类,专门用来辅助实现这种操作。

    /// <summary>
    ///
这个类主要用于那种将一个字节分成若干部分,每个部分表示一个数的类型
    /// </summary>
    class BitHelper
    {
        public static byte GetByte(byte data, byte index, byte count)
        {
            Contract.Requires(index + count <= 8);

            return (byte)((data >> (8 - index - count)) % (1 << count));
        }

        public static byte SetByte(int value, byte data, byte index, byte count)
        {
            Contract.Requires(index + count <= 8);
            Contract.Requires(value < (1 << count));

            var tail = data % (1 << (8 - index - count));
            return (byte)((((data >> (8 - index) << count) + value) << index) + tail);
        }

        //测试某一位是否为1
        public static bool TestBit(byte data, byte index)
        {
           
         return GetByte(data, index, 1) == 1;
       
         }      

        public static byte SetBit(bool value, byte data, byte index)
        {
            return SetByte(value ? 1 : 0, data, index, 1);
        }
    }

这个类并不能直接用,也是辅助实现的操作,一般用于在属性中封装某个字段。

   private byte flags = 5;
    public byte Flags
    {
        get { return flags;
     }
        set { flags = value; }
    }

    public bool TypeFlagsAudio
    {
        get { return BitHelper.TestBit(flags, 5); }
        set { flags = BitHelper.SetBit(value, flags, 5); }
    }

    public bool TypeFlagsVideo
    {
        get { return BitHelper.TestBit(flags, 7); }
        set { flags = BitHelper.SetBit(value, flags, 7); }
    }

抱歉!评论已关闭.