namespace VCommon { /// bit操作捷径 public static class BitAccessor { //byte /// 获取指定bit值, 为1返回true否则返回false public static bool Bit(this byte me, int bit) => 0 != (me & 1 << bit); /// 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 public static byte Bit(this byte me, int bit, bool value) => (byte)(value ? me | 1 << bit : me & ~(1 << bit)); //sbyte /// 获取指定bit值, 为1返回true否则返回false public static bool Bit(this sbyte me, int bit) => 0 != (me & 1 << bit); /// 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 public static sbyte Bit(this sbyte me, int bit, bool value) => (sbyte)(value ? me | (sbyte)(1 << bit) : me & ~(1 << bit)); //short /// 获取指定bit值, 为1返回true否则返回false public static bool Bit(this short me, int bit) => 0 != (me & 1 << bit); /// 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 public static short Bit(this short me, int bit, bool value) => (short)(value ? me | (short)(1 << bit) : me & ~(1 << bit)); //ushort /// 获取指定bit值, 为1返回true否则返回false public static bool Bit(this ushort me, int bit) => 0 != (me & 1 << bit); /// 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 public static ushort Bit(this ushort me, int bit, bool value) => (ushort)(value ? me | (ushort)(1 << bit) : me & ~(1 << bit)); //int /// 获取指定bit值, 为1返回true否则返回false public static bool Bit(this int me, int bit) => 0 != (me & 1 << bit); /// 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 public static int Bit(this int me, int bit, bool value) => value ? me | 1 << bit : me & ~(1 << bit); //uint /// 获取指定bit值, 为1返回true否则返回false public static bool Bit(this uint me, int bit) => 0 != (me & 1 << bit); /// 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 public static uint Bit(this uint me, int bit, bool value) => (uint)(value ? me | (uint)(1 << bit) : me & ~(1 << bit)); //long /// 获取指定bit值, 为1返回true否则返回false public static bool Bit(this long me, int bit) => 0 != (me & 1 << bit); /// 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 public static long Bit(this long me, int bit, bool value) => value ? me | ((long)1 << bit) : me & ~(1 << bit); //ulong /// 获取指定bit值, 为1返回true否则返回false public static bool Bit(this ulong me, int bit) => 0 != (me & 1ul << bit); /// 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 public static ulong Bit(this ulong me, int bit, bool value) => value ? me | ((ulong)1 << bit) : me & ~(1ul << bit); } }