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