C#实现类似C/C++的位域

#region 位(BIT)类型自定义
[StructLayout(LayoutKind.Explicit, Size = 1)]
public struct IO_BIT_BYTE
{
    [FieldOffset(0)]
    byte _bit0;
    [FieldOffset(0)]
    byte _bit1;
    [FieldOffset(0)]
    byte _bit2;
    [FieldOffset(0)]
    byte _bit3;
    [FieldOffset(0)]
    byte _bit4;
    [FieldOffset(0)]
    byte _bit5;
    [FieldOffset(0)]
    byte _bit6;
    [FieldOffset(0)]
    byte _bit7;
    [FieldOffset(0)]
    public byte Value;

    #region 属性
    public bool Bit0
    {
        set
        {
            if (value)
            {
                _bit0 |= (byte)0x01;
            }
            else
            {
                _bit0 &= (byte)0xFE;
            }
        }
        get
        {
            bool result = false;
            byte t = (byte)(_bit0 << 7);
            result = (byte)(t & 0x80) == (byte)0x80 ? true : false;
            return result;
        }
    }

    public bool Bit1
    {
        set
        {
            if (value)
            {
                _bit1 |= (byte)0x02;
            }
            else
            {
                _bit1 &= (byte)0xFD;
            }
        }
        get
        {
            byte t = (byte)(_bit1 << 6);
            bool result = (byte)(t & 0x80) == (byte)0x80 ? true : false;
            return result;
        }
    }

    public bool Bit2
    {
        set
        {
            if (value)
            { _bit2 |= (byte)0x04; }
            else
            { _bit2 &= (byte)0xFB; }

        }
        get
        {
            byte t = (byte)(_bit2 << 5);
            bool result = (byte)(t & 0x80) == (byte)0x80 ? true : false;
            return result;
        }
    }

    public bool Bit3
    {
        set
        {
            if (value)
            { _bit3 |= (byte)0x08; }
            else
            { _bit3 &= (byte)0xF7; }
        }
        get
        {
            byte t = (byte)(_bit3 << 4);
            bool result = (byte)(t & 0x80) == (byte)0x80 ? true : false;
            return result;
        }
    }

    public bool Bit4
    {
        set
        {
            if (value)
            { _bit4 |= (byte)0x10; }
            else
            { _bit4 &= (byte)0xEF; }
        }
        get
        {
            byte t = (byte)(_bit4 << 3);
            bool result = (byte)(t & 0x80) == (byte)0x80 ? true : false;
            return result;
        }
    }

    public bool Bit5
    {
        set
        {
            if (value)
            { _bit5 |= (byte)0x20; }
            else
            { _bit5 &= (byte)0xDF; }
        }
        get
        {
            byte t = (byte)(_bit5 << 2);
            bool result = (byte)(t & 0x80) == (byte)0x80 ? true : false;
            return result;
        }
    }

    public bool Bit6
    {
        set
        {
            if (value)
            { _bit6 |= (byte)0x40; }
            else
            { _bit6 &= (byte)0xBF; }
        }
        get
        {
            byte t = (byte)(_bit6 << 1);
            bool result = (byte)(t & 0x80) == (byte)0x80 ? true : false;
            return result;
        }
    }

    public bool Bit7
    {
        set
        {
            if (value)
            { _bit7 |= (byte)0x80; }
            else
            { _bit7 &= (byte)0x7F; }
        }
        get
        {
            bool result = (byte)(_bit7 & 0x80) == (byte)0x80 ? true : false;
            return result;
        }
    }

    public bool this[int idx]
    {
        set
        {
            switch (idx)
            {
            case 0:
                Bit0 = value;
                break;
            case 1:
                Bit1 = value;
                break;
            case 2:
                Bit2 = value;
                break;
            case 3:
                Bit3 = value;
                break;
            case 4:
                Bit4 = value;
                break;
            case 5:
                Bit5 = value;
                break;
            case 6:
                Bit6 = value;
                break;
            case 7:
                Bit7 = value;
                break;
            default:
                throw new IndexOutOfRangeException();
            }
        }
        get
        {
            switch (idx)
            {
            case 0:
                return Bit0;
            case 1:
                return Bit1;
            case 2:
                return Bit2;
            case 3:
                return Bit3;
            case 4:
                return Bit4;
            case 5:
                return Bit5;
            case 6:
                return Bit6;
            case 7:
                return Bit7;
            default:
                throw new IndexOutOfRangeException();
            }
        }

    }
    #endregion 属性

    #region 重载

    public static IO_BIT_BYTE operator & (IO_BIT_BYTE value1, byte value2)
    {
        IO_BIT_BYTE result = new IO_BIT_BYTE();
        result.Value = (byte)(value1.Value & value2);
        return result;
    }
    public static IO_BIT_BYTE operator | (IO_BIT_BYTE value1, byte value2)
    {
        IO_BIT_BYTE result = new IO_BIT_BYTE();
        result.Value = (byte)(value1.Value | value2);
        return result;
    }

    public static IO_BIT_BYTE operator >> (IO_BIT_BYTE value, int num)
    {
        IO_BIT_BYTE result = new IO_BIT_BYTE();
        result.Value = (byte)(value.Value >> num);
        return result;
    }

    public static IO_BIT_BYTE operator << (IO_BIT_BYTE value, int num)
    {
        IO_BIT_BYTE result = new IO_BIT_BYTE();
        result.Value = (byte)(value.Value << num);
        return result;
    }
    public static bool operator == (IO_BIT_BYTE value1, IO_BIT_BYTE value2)
    {
        if (value1.Value == value2.Value) return true;
        return false;
    }

    public static bool operator != (IO_BIT_BYTE value1, IO_BIT_BYTE value2)
    {
        if (value1.Value == value2.Value) return false;
        return true;
    }

    /// <summary>
    /// 显示二进制值
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 7; i >= 0; i--)
        {
            if (this[i]) sb.Append("1"); else sb.Append("0");
        }
        return sb.ToString();
    }
    #endregion 重载
}
#endregion 位(BIT)类型自定义

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值