namespace Houdar.Core.Util.Extension
{
    /// 
    ///     位运算操作
    /// 
    public static class BitExtension
    {
        #region ushort 
        /// 
        /// 设置指定位置的位值的值
        /// 
        /// ushort对象
        /// 指定位置
        /// 值
        /// 
        public static ushort SetBit(this ushort value, int position, bool flag)
        {
            return SetBits(value, position, 1, flag ? (byte) 1 : (byte) 0);
        }
        /// 
        /// 批量设置指定位置的位值的值
        /// 
        /// ushort对象
        /// 开始位置
        /// 长度
        /// 值
        /// 
        public static ushort SetBits(this ushort value, int position, int length, ushort bits)
        {
            if (length <= 0 || position >= 16) return value;
            var mask = (2 << (length - 1)) - 1;
            value &= (ushort) ~(mask << position);
            value |= (ushort) ((bits & mask) << position);
            return value;
        }
        /// 
        /// 获取指定位置的值
        /// 
        /// ushort对象
        /// 指定位置
        /// 
        public static bool GetBit(this ushort value, int position)
        {
            return GetBits(value, position, 1) == 1;
        }
        /// 
        /// 批量获取指定位置的值
        /// 
        /// ushort对象
        /// 开始位值
        /// 长度
        /// 
        public static ushort GetBits(this ushort value, int position, int length)
        {
            if (length <= 0 || position >= 16) return 0;
            var mask = (2 << (length - 1)) - 1;
            return (ushort) ((value >> position) & mask);
        }
        #endregion
        #region byte
        /// 
        /// 设置指定位置的值
        /// 
        /// byte对象
        /// 指定位置
        /// 设置值
        /// 
        public static byte SetBit(this byte value, int position, bool flag)
        {
            if (position >= 8) return value;
            var mask = (2 << (1 - 1)) - 1;
            value &= (byte) ~(mask << position);
            value |= (byte) (((flag ? 1 : 0) & mask) << position);
            return value;
        }
        /// 
        /// 获取指定位置的值
        /// 
        /// byte对象
        /// 指定位置
        /// 
        public static bool GetBit(this byte value, int position)
        {
            if (position >= 8) return false;
            var mask = (2 << (1 - 1)) - 1;
            return (byte) ((value >> position) & mask) == 1;
        }
        #endregion
        #region uint
        /// 
        /// 设置指定位置的位值的值
        /// 
        /// uint对象
        /// 指定位置
        /// 值
        /// 
        public static uint SetBit(this uint value, int position, bool flag)
        {
            return SetBits(value, position, 1, flag ? (byte) 1 : (byte) 0);
        }
        /// 
        /// 批量设置指定位置的位值的值
        /// 
        /// uint对象
        /// 开始位置
        /// 长度
        /// 值
        /// 
        public static uint SetBits(this uint value, int position, int length, uint bits)
        {
            if (length <= 0 || position >= 32) return value;
            var mask = (2 << (length - 1)) - 1;
            value &= (uint) ~(mask << position);
            value |= (uint) ((bits & mask) << position);
            return value;
        }
        /// 
        /// 获取指定位置的值
        /// 
        /// uint对象
        /// 指定位置
        /// 
        public static bool GetBit(this uint value, int position)
        {
            return GetBits(value, position, 1) == 1;
        }
        /// 
        /// 批量获取指定位置的值
        /// 
        /// uint对象
        /// 开始位值
        /// 长度
        /// 
        public static uint GetBits(this uint value, int position, int length)
        {
            if (length <= 0 || position >= 32) return 0;
            var mask = (2 << (length - 1)) - 1;
            return (uint) ((value >> position) & mask);
        }
        #endregion
        #region ulong
        /// 
        /// 设置指定位置的位值的值
        /// 
        /// ulong对象
        /// 指定位置
        /// 值
        /// 
        public static ulong SetBit(this ulong value, int position, bool flag)
        {
            return SetBits(value, position, 1, flag ? (byte) 1 : (byte) 0);
        }
        /// 
        /// 批量设置指定位置的位值的值
        /// 
        /// ulong对象
        /// 开始位置
        /// 长度
        /// 值
        /// 
        public static ulong SetBits(this ulong value, int position, int length, ulong bits)
        {
            if (length <= 0 || position >= 64) return value;
            var mask = (ulong) (2 << (length - 1)) - 1;
            value &= ~(mask << position);
            value |= (bits & mask) << position;
            return value;
        }
        /// 
        /// 获取指定位置的值
        /// 
        /// ulong对象
        /// 指定位置
        /// 
        public static bool GetBit(this ulong value, int position)
        {
            return GetBits(value, position, 1) == 1;
        }
        /// 
        /// 批量获取指定位置的值
        /// 
        /// ulong对象
        /// 开始位值
        /// 长度
        /// 
        public static ulong GetBits(this ulong value, int position, int length)
        {
            if (length <= 0 || position >= 64) return 0;
            var mask = (ulong) (2 << (length - 1)) - 1;
            return (value >> position) & mask;
        }
        #endregion
    }
}