c#设置或者获取一个泛型整数指定bit的值

发布于:2024-05-16 ⋅ 阅读:(69) ⋅ 点赞:(0)

1.设置

代码如下(示例):

		/// <summary>
        /// 设置一个整形数的每一个bit的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="flag">bit位0或者1</param>
        /// <param name="num">需要改变的数</param>
        /// <param name="offset">从右往左第几位,从0开始</param>
        /// <returns></returns>
        T Changebit<T>(bool flag, T num, int offset) where T : struct
        {
            // 确保 T 是一种整数类型
            int len = 0;
            if (_data is uint )
            {
                len = sizeof(uint) * 8;
            }
            else if (_data is int)
            {
                len = sizeof(int) * 8;
            }
            else if (_data is long)
            {
                len = sizeof(long) * 8;
            }
            else if (_data is ulong)
            {
                len = sizeof(ulong) * 8;
            }
            else if (_data is short)
            {
                len = sizeof(short) * 8;
            }
            else if (_data is ushort)
            {
                len = sizeof(ushort) * 8;
            }
            else if (_data is byte)
            {
                len = sizeof(byte) * 8;
            }
            else
            {
                throw new InvalidOperationException("Only integer types are supported.");
            }
            if (index > len || index < 0)
                throw new InvalidOperationException("Length too long.");
            // 将 T 转换为整数,以便进行位操作
            dynamic value = num;
            // 创建一个掩码,指定偏移量位置
            int mask = 1 << offset;
            if (flag)
            {
                // 如果 flag 为真,设置相应位置的位
                value |= mask;
            }
            else
            {
                // 如果 flag 为假,清除相应位置的位
                value &= ~mask;
            }
            // 返回处理后的结果
            return (T)value;
        }

2.get每一个bit位

代码如下(示例):

bool GetIntBit<T>(T _data, int index)
        {
            int len = 0;
            if (_data is uint)
            {
                len = sizeof(uint) * 8;
            }
            else if (_data is int)
            {
                len = sizeof(int) * 8;
            }
            else if (_data is long)
            {
                len = sizeof(long) * 8;
            }
            else if (_data is ulong)
            {
                len = sizeof(ulong) * 8;
            }
            else if (_data is short)
            {
                len = sizeof(short) * 8;
            }
            else if (_data is ushort)
            {
                len = sizeof(ushort) * 8;
            }
            else if (_data is byte)
            {
                len = sizeof(byte) * 8;
            }
            else
            {
                throw new InvalidOperationException("Only integer types are supported.");
            }
            if (index > len || index < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            dynamic b = _data;
            return (b & (1 << index)) != 0;
        }