Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
1.7 KiB

  1. // Copyright (c) 1997-1999 Microsoft Corporation
  2. //
  3. // bit twiddling stuff
  4. //
  5. // 8-5-98 sburns
  6. // One would think that after n decades of C, these would be builtin
  7. // keywords...
  8. // Returns state of bit n: true if it is set, false if not
  9. //
  10. // bits - set of bits
  11. //
  12. // n - bit to test
  13. inline
  14. bool
  15. getbit(ULONG bits, unsigned int n)
  16. {
  17. if (bits & (1 << n))
  18. {
  19. return true;
  20. }
  21. return false;
  22. }
  23. // flips bit n (sets it to the opposite state), returns new state of bit n
  24. //
  25. // bits - set of bits.
  26. //
  27. // n - bit to flip
  28. inline
  29. bool
  30. flipbit(ULONG& bits, unsigned int n)
  31. {
  32. return getbit((bits ^= (1 << n)), n);
  33. }
  34. // sets bit n to 1
  35. //
  36. // bits - set of bits
  37. //
  38. // n - bit to set
  39. inline
  40. void
  41. setbit(ULONG& bits, unsigned int n)
  42. {
  43. bits |= (1 << n);
  44. }
  45. // sets bit n to 0
  46. //
  47. // bits - set of bits
  48. //
  49. // n - bit to clear
  50. inline
  51. void
  52. clearbit(ULONG& bits, unsigned int n)
  53. {
  54. ULONG mask = (1 << n);
  55. bits &= ~mask;
  56. }
  57. // Sets all bits in mask. Returns result
  58. //
  59. // bits - holds bits to be set
  60. //
  61. // mask - mask of bits to set
  62. inline
  63. DWORD
  64. setbits(ULONG& bits, ULONG mask)
  65. {
  66. ASSERT(mask);
  67. bits |= mask;
  68. return bits;
  69. }
  70. // Clears all bits in mask. Returns result.
  71. //
  72. // bits - holds bits to be cleared
  73. //
  74. // mask - mask of bits to clear
  75. inline
  76. DWORD
  77. clearbits(ULONG& bits, ULONG mask)
  78. {
  79. ASSERT(mask);
  80. bits &= ~mask;
  81. return bits;
  82. }
  83. // Toggles all bits in mask. bits that were set are cleared, and vice-
  84. // versa. Returns result.
  85. //
  86. // bits - holds bits to be toggled
  87. //
  88. // mask - mask of bits to toggled
  89. inline
  90. DWORD
  91. flipbits(ULONG& bits, ULONG mask)
  92. {
  93. bits ^= mask;
  94. return bits;
  95. }