BitAccessor.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. namespace VCommon
  2. {
  3. /// <summary> bit操作捷径 </summary>
  4. public static class BitAccessor
  5. {
  6. //byte
  7. /// <summary> 获取指定bit值, 为1返回true否则返回false </summary>
  8. public static bool Bit(this byte me, int bit) => 0 != (me & 1 << bit);
  9. /// <summary> 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 </summary>
  10. public static byte Bit(this byte me, int bit, bool value) => (byte)(value
  11. ? me | 1 << bit
  12. : me & ~(1 << bit));
  13. //sbyte
  14. /// <summary> 获取指定bit值, 为1返回true否则返回false </summary>
  15. public static bool Bit(this sbyte me, int bit) => 0 != (me & 1 << bit);
  16. /// <summary> 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 </summary>
  17. public static sbyte Bit(this sbyte me, int bit, bool value) => (sbyte)(value
  18. ? me | (sbyte)(1 << bit)
  19. : me & ~(1 << bit));
  20. //short
  21. /// <summary> 获取指定bit值, 为1返回true否则返回false </summary>
  22. public static bool Bit(this short me, int bit) => 0 != (me & 1 << bit);
  23. /// <summary> 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 </summary>
  24. public static short Bit(this short me, int bit, bool value) => (short)(value
  25. ? me | (short)(1 << bit)
  26. : me & ~(1 << bit));
  27. //ushort
  28. /// <summary> 获取指定bit值, 为1返回true否则返回false </summary>
  29. public static bool Bit(this ushort me, int bit) => 0 != (me & 1 << bit);
  30. /// <summary> 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 </summary>
  31. public static ushort Bit(this ushort me, int bit, bool value) => (ushort)(value
  32. ? me | (ushort)(1 << bit)
  33. : me & ~(1 << bit));
  34. //int
  35. /// <summary> 获取指定bit值, 为1返回true否则返回false </summary>
  36. public static bool Bit(this int me, int bit) => 0 != (me & 1 << bit);
  37. /// <summary> 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 </summary>
  38. public static int Bit(this int me, int bit, bool value) => value
  39. ? me | 1 << bit
  40. : me & ~(1 << bit);
  41. //uint
  42. /// <summary> 获取指定bit值, 为1返回true否则返回false </summary>
  43. public static bool Bit(this uint me, int bit) => 0 != (me & 1 << bit);
  44. /// <summary> 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 </summary>
  45. public static uint Bit(this uint me, int bit, bool value) => (uint)(value
  46. ? me | (uint)(1 << bit)
  47. : me & ~(1 << bit));
  48. //long
  49. /// <summary> 获取指定bit值, 为1返回true否则返回false </summary>
  50. public static bool Bit(this long me, int bit) => 0 != (me & 1 << bit);
  51. /// <summary> 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 </summary>
  52. public static long Bit(this long me, int bit, bool value) => value
  53. ? me | ((long)1 << bit)
  54. : me & ~(1 << bit);
  55. //ulong
  56. /// <summary> 获取指定bit值, 为1返回true否则返回false </summary>
  57. public static bool Bit(this ulong me, int bit) => 0 != (me & 1ul << bit);
  58. /// <summary> 设置指定bit值, 为true设置为1否则设置为0, 将计算结果返回 </summary>
  59. public static ulong Bit(this ulong me, int bit, bool value) => value
  60. ? me | ((ulong)1 << bit)
  61. : me & ~(1ul << bit);
  62. }
  63. }