using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CSharpUtil { public static class BitsUtil { #region byte extension /// /// 返回Int数据中某一位是否为1 /// /// /// 32位数据的从右向左的偏移位索引(0~31) /// true表示该位为1,false表示该位为0 public static bool GetBitValue(this byte value, int index) { Trace.Assert(index >= 0 && index <= 7, $"{nameof(index)} is Out Of Range(0..7).");//索引出错 int val = 1 << index; return (value & val) == val; } /// /// 设定Int数据中某一位的值 /// /// 位设定前的值 /// 32位数据的从右向左的偏移位索引(0~31) /// true设该位为1,false设为0 /// 返回位设定后的值 public static byte SetBitValue(ref this byte value, int index, bool bitValue) { Trace.Assert(index >= 0 && index <= 7, $"{nameof(index)} is Out Of Range(0..7).");//索引出错 var val = (byte)(1 << index); value = (byte)(bitValue ? (value | val) : (value & ~val)); return value; } /// /// 根据Int类型的值,返回用1或0(对应True或Flase)填充的数组 /// 从右侧开始向左索引(0~31) /// /// /// public static IEnumerable GetBitList(this byte value) { var list = new List(8); for (var i = 0; i <= 7; i++) { int val = 1 << i; list.Add((value & val) == val); } return list; } /// /// /// /// /// /// public static bool Bit(this byte value, int index) => GetBitValue(value, index); /// /// /// /// /// public static bool[] Bits(this byte value) { var arr = new bool[8]; for (var i = 0; i <= 7; i++) { int x = 1 << i; arr[i] = ((value & x) == x); } return arr; } #endregion #region ushort extension /// /// 返回Int数据中某一位是否为1 /// /// /// 32位数据的从右向左的偏移位索引(0~31) /// true表示该位为1,false表示该位为0 public static bool GetBitValue(this ushort value, int index) { Trace.Assert(index >= 0 && index <= 15, $"{nameof(index)} is Out Of Range(0..15).");//索引出错 int val = 1 << index; return (value & val) == val; } /// /// 设定Int数据中某一位的值 /// /// 位设定前的值 /// 32位数据的从右向左的偏移位索引(0~31) /// true设该位为1,false设为0 /// 返回位设定后的值 public static ushort SetBitValue(ref this ushort value, int index, bool bitValue) { Trace.Assert(index >= 0 && index <= 15, $"{nameof(index)} is Out Of Range(0..15).");//索引出错 var val = (ushort)(1 << index); value = (ushort)(bitValue ? (value | val) : (value & ~val)); return value; } /// /// 根据Int类型的值,返回用1或0(对应True或Flase)填充的数组 /// 从右侧开始向左索引(0~31) /// /// /// public static IEnumerable GetBitList(this ushort value) { var list = new List(16); for (var i = 0; i <= 15; i++) { int val = 1 << i; list.Add((value & val) == val); } return list; } /// /// /// /// /// /// public static bool Bit(this ushort value, int index) => GetBitValue(value, index); /// /// /// /// /// public static bool[] Bits(this ushort value) { var arr = new bool[16]; for (var i = 0; i <= 15; i++) { int x = 1 << i; arr[i] = ((value & x) == x); } return arr; } #endregion #region short extension /// /// 返回Int数据中某一位是否为1 /// /// /// 32位数据的从右向左的偏移位索引(0~31) /// true表示该位为1,false表示该位为0 public static bool GetBitValue(this short value, int index) { Trace.Assert(index >= 0 && index <= 15, $"{nameof(index)} is Out Of Range(0..15).");//索引出错 int val = 1 << index; return (value & val) == val; } /// /// 设定Int数据中某一位的值 /// /// 位设定前的值 /// 32位数据的从右向左的偏移位索引(0~31) /// true设该位为1,false设为0 /// 返回位设定后的值 public static short SetBitValue(ref this short value, int index, bool bitValue) { Trace.Assert(index >= 0 && index <= 15, $"{nameof(index)} is Out Of Range(0..15).");//索引出错 var val = (short)(1 << index); value = (short)(bitValue ? (value | val) : (value & ~val)); return value; } /// /// 根据Int类型的值,返回用1或0(对应True或Flase)填充的数组 /// 从右侧开始向左索引(0~31) /// /// /// public static IEnumerable GetBitList(this short value) { var list = new List(16); for (var i = 0; i <= 15; i++) { int val = 1 << i; list.Add((value & val) == val); } return list; } /// /// /// /// /// /// public static bool Bit(this short value, int index) => GetBitValue(value, index); /// /// /// /// /// public static bool[] Bits(this short value) { var arr = new bool[16]; for (var i = 0; i <= 15; i++) { int x = 1 << i; arr[i] = ((value & x) == x); } return arr; } #endregion #region Int extension /// /// 返回Int数据中某一位是否为1 /// /// /// 32位数据的从右向左的偏移位索引(0~31) /// true表示该位为1,false表示该位为0 public static bool GetBitValue(this int value, int index) { Trace.Assert(index >= 0 && index <= 31, $"{nameof(index)} is Out Of Range(0..31).");//索引出错 int val = 1 << index; return (value & val) == val; } /// /// 设定Int数据中某一位的值 /// /// 位设定前的值 /// 32位数据的从右向左的偏移位索引(0~31) /// true设该位为1,false设为0 /// 返回位设定后的值 public static int SetBitValue(ref this int value, int index, bool bitValue) { Trace.Assert(index >= 0 && index <= 31, $"{nameof(index)} is Out Of Range(0..31).");//索引出错 int val = 1 << index; value = bitValue ? (value | val) : (value & ~val); return value; } /// /// 根据Int类型的值,返回用1或0(对应True或Flase)填充的数组 /// 从右侧开始向左索引(0~31) /// /// /// public static IEnumerable GetBitList(this int value) { var list = new List(32); for (var i = 0; i <= 31; i++) { int val = 1 << i; list.Add((value & val) == val); } return list; } /// /// /// /// /// /// public static bool Bit(this int value, int index) => GetBitValue(value,index); /// /// /// /// /// public static bool[] Bits(this int value) { var arr = new bool[32]; for (var i = 0; i <= 31; i++) { int x = 1 << i; arr[i] = ((value & x) == x); } return arr; } #endregion #region uint extension /// /// 返回Int数据中某一位是否为1 /// /// /// 32位数据的从右向左的偏移位索引(0~31) /// true表示该位为1,false表示该位为0 public static bool GetBitValue(this uint value, int index) { Trace.Assert(index >= 0 && index <= 31, $"{nameof(index)} is Out Of Range(0..31).");//索引出错 uint val = 1u << index; return (value & val) == val; } /// /// 设定Int数据中某一位的值 /// /// 位设定前的值 /// 32位数据的从右向左的偏移位索引(0~31) /// true设该位为1,false设为0 /// 返回位设定后的值 public static uint SetBitValue(ref this uint value, int index, bool bitValue) { Trace.Assert(index >= 0 && index <= 31, $"{nameof(index)} is Out Of Range(0..31).");//索引出错 uint val = 1u << index; value = bitValue ? (value | val) : (value & ~val); return value; } /// /// 根据Int类型的值,返回用1或0(对应True或Flase)填充的数组 /// 从右侧开始向左索引(0~31) /// /// /// public static IEnumerable GetBitList(this uint value) { var list = new List(32); for (var i = 0; i <= 31; i++) { uint val = 1u << i; list.Add((value & val) == val); } return list; } /// /// /// /// /// /// public static bool Bit(this uint value, int index) => GetBitValue(value, index); /// /// /// /// /// public static bool[] Bits(this uint value) { var arr = new bool[32]; for (var i = 0; i <= 31; i++) { uint x = 1u << i; arr[i] = ((value & x) == x); } return arr; } #endregion #region Long extension /// /// 返回Int数据中某一位是否为1 /// /// /// 32位数据的从右向左的偏移位索引(0~31) /// true表示该位为1,false表示该位为0 public static bool GetBitValue(this long value, int index) { Trace.Assert(index >= 0 && index <= 63, $"{nameof(index)} is Out Of Range(0..63).");//索引出错 long val = 1 << index; return (value & val) == val; } /// /// 设定Int数据中某一位的值 /// /// 位设定前的值 /// 32位数据的从右向左的偏移位索引(0~31) /// true设该位为1,false设为0 /// 返回位设定后的值 public static long SetBitValue(ref this long value, int index, bool bitValue) { Trace.Assert(index >= 0 && index <= 63, $"{nameof(index)} is Out Of Range(0..63).");//索引出错 long val = 1 << index; value = bitValue ? (value | val) : (value & ~val); return value; } /// /// 根据Int类型的值,返回用1或0(对应True或Flase)填充的数组 /// 从右侧开始向左索引(0~31) /// /// /// public static IEnumerable GetBitList(this long value) { var list = new List(64); for (var i = 0; i <= 63; i++) { long val = 1 << i; list.Add((value & val) == val); } return list; } /// /// /// /// /// /// public static bool Bit(this long value, int index) => GetBitValue(value, index); /// /// /// /// /// public static bool[] Bits(this long value) { var arr = new bool[64]; for (var i = 0; i <= 31; i++) { long x = 1 << i; arr[i] = ((value & x) == x); } return arr; } #endregion } }