code_junkie

How do you set, clear and toggle a single bit in C?

Nov 10th, 2011
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. number |= 1 << x;
  2.  
  3. number &= ~(1 << x);
  4.  
  5. number ^= 1 << x;
  6.  
  7. bit = number & (1 << x);
  8.  
  9. #include <bitset>
  10. #include <iostream>
  11.  
  12. int main()
  13. {
  14. std::bitset<5> x;
  15.  
  16. x[1] = 1;
  17. x[2] = 0;
  18. // Note x[0-4] valid
  19.  
  20. std::cout << x << std::endl;
  21. }
  22.  
  23. [Alpha:] > ./a.out
  24. 00010
  25.  
  26. struct bits {
  27. unsigned int a:1;
  28. unsigned int b:1;
  29. unsigned int c:1;
  30. };
  31.  
  32. struct bits mybits;
  33.  
  34. mybits.b = 1;
  35. mybits.c = 0;
  36.  
  37. mybits.a = !mybits.a;
  38. mybits.b = ~mybits.b;
  39. mybits.c ^= 1; /* all work */
  40.  
  41. if (mybits.c)
  42.  
  43. enum ThingFlags = {
  44. ThingMask = 0x0000,
  45. ThingFlag0 = 1 << 0,
  46. ThingFlag1 = 1 << 1,
  47. ThingError = 1 << 8,
  48. }
  49.  
  50. thingstate |= ThingFlag1;
  51. thingstate &= ~ThingFlag0;
  52. if (thing | ThingError) {...}
  53.  
  54. /*
  55. ** Bit set, clear, and test operations
  56. **
  57. ** public domain snippet by Bob Stout
  58. */
  59.  
  60. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  61.  
  62. #define BOOL(x) (!(!(x)))
  63.  
  64. #define BitSet(arg,posn) ((arg) | (1L << (posn)))
  65. #define BitClr(arg,posn) ((arg) & ~(1L << (posn)))
  66. #define BitTst(arg,posn) BOOL((arg) & (1L << (posn)))
  67. #define BitFlp(arg,posn) ((arg) ^ (1L << (posn)))
  68.  
  69. 0000 0000 0000 0000 0000 0000 0000 0001 binary.
  70.  
  71. 0000 0000 0000 0000 0000 0001 0000 0000 binary.
  72.  
  73. #define BOOL(x) (!(!(x)))
  74.  
  75. #define BitSet(arg,posn) ((arg) | (1L << (posn)))
  76. #define BitClr(arg,posn) ((arg) & ~(1L << (posn)))
  77. #define BitTst(arg,posn) BOOL((arg) & (1L << (posn)))
  78. #define BitFlp(arg,posn) ((arg) ^ (1L << (posn)))
  79.  
  80. int bitmanip(int word)
  81. {
  82. word = BitSet(word, 2);
  83. word = BitSet(word, 7);
  84. word = BitClr(word, 3);
  85. word = BitFlp(word, 9);
  86. return word;
  87. }
  88.  
  89. /* a=target variable, b=bit number to act upon 0-n */
  90. #define BIT_SET(a,b) ((a) |= (1<<(b)))
  91. #define BIT_CLEAR(a,b) ((a) &= ~(1<<(b)))
  92. #define BIT_FLIP(a,b) ((a) ^= (1<<(b)))
  93. #define BIT_CHECK(a,b) ((a) & (1<<(b)))
  94.  
  95. /* x=target variable, y=mask */
  96. #define BITMASK_SET(x,y) ((x) |= (y))
  97. #define BITMASK_CLEAR(x,y) ((x) &= (~(y)))
  98. #define BITMASK_FLIP(x,y) ((x) ^= (y))
  99. #define BITMASK_CHECK(x,y) ((x) & (y))
  100.  
  101. #define BITS 8
  102. #define BIT_SET( p, n) (p[(n)/BITS] |= (0x80>>((n)%BITS)))
  103. #define BIT_CLEAR(p, n) (p[(n)/BITS] &= ~(0x80>>((n)%BITS)))
  104. #define BIT_ISSET(p, n) (p[(n)/BITS] & (0x80>>((n)%BITS)))
  105.  
  106. #define BITOP(a,b,op)
  107. ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
  108.  
  109. BITOP(array, bit, |=);
  110.  
  111. BITOP(array, bit, &=~);
  112.  
  113. BITOP(array, bit, ^=);
  114.  
  115. if (BITOP(array, bit, &)) ...
  116.  
  117. struct HwRegister {
  118. unsigned int errorFlag:1; // one-bit flag field
  119. unsigned int Mode:3; // three-bit mode field
  120. unsigned int StatusCode:4; // four-bit status code
  121. };
  122.  
  123. struct HwRegister CR3342_AReg;
  124.  
  125. #define bit_test(x, y) ( ( ((const char*)&(x))[(y)>>3] & 0x80 >> ((y)&0x07)) >> (7-((y)&0x07) ) )
  126.  
  127. int main(void)
  128. {
  129. unsigned char arr[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
  130.  
  131. for (int ix = 0; ix < 64; ++ix)
  132. printf("bit %d is %dn", ix, bit_test(arr, ix));
  133.  
  134. return 0;
  135. }
  136.  
  137. const unsigned char TQuickByteMask[8] =
  138. {
  139. 0x01, 0x02, 0x04, 0x08,
  140. 0x10, 0x20, 0x40, 0x80,
  141. };
  142.  
  143.  
  144.  
  145.  
  146. /** Set bit in any sized bit mask.
  147. *
  148. * @return none
  149. *
  150. * @param bit - Bit number.
  151. * @param bitmap - Pointer to bitmap.
  152. */
  153. void TSetBit( short bit, unsigned char *bitmap)
  154. {
  155. short n, x;
  156.  
  157. x = bit / 8; // Index to byte.
  158. n = bit % 8; // Specific bit in byte.
  159.  
  160. bitmap[x] |= TQuickByteMask[n]; // Set bit.
  161. }
  162.  
  163.  
  164.  
  165. /** Reset bit in any sized mask.
  166. *
  167. * @return None
  168. *
  169. * @param bit - Bit number.
  170. * @param bitmap - Pointer to bitmap.
  171. */
  172. void TResetBit( short bit, unsigned char *bitmap)
  173. {
  174. short n, x;
  175.  
  176. x = bit / 8; // Index to byte.
  177. n = bit % 8; // Specific bit in byte.
  178.  
  179. bitmap[x] &= (~TQuickByteMask[n]); // Reset bit.
  180. }
  181.  
  182.  
  183.  
  184.  
  185. /** Toggle bit in any sized bit mask.
  186. *
  187. * @return none
  188. *
  189. * @param bit - Bit number.
  190. * @param bitmap - Pointer to bitmap.
  191. */
  192. void TToggleBit( short bit, unsigned char *bitmap)
  193. {
  194. short n, x;
  195.  
  196. x = bit / 8; // Index to byte.
  197. n = bit % 8; // Specific bit in byte.
  198.  
  199. bitmap[x] ^= TQuickByteMask[n]; // Toggle bit.
  200. }
  201.  
  202.  
  203.  
  204.  
  205. /** Checks specified bit.
  206. *
  207. * @return 1 if bit set else 0.
  208. *
  209. * @param bit - Bit number.
  210. * @param bitmap - Pointer to bitmap.
  211. */
  212. short TIsBitSet( short bit, const unsigned char *bitmap)
  213. {
  214. short n, x;
  215.  
  216. x = bit / 8; // Index to byte.
  217. n = bit % 8; // Specific bit in byte.
  218.  
  219. // Test bit (logigal AND).
  220. if (bitmap[x] & TQuickByteMask[n])
  221. return 1;
  222.  
  223. return 0;
  224. }
  225.  
  226.  
  227.  
  228.  
  229.  
  230. /** Checks specified bit.
  231. *
  232. * @return 1 if bit reset else 0.
  233. *
  234. * @param bit - Bit number.
  235. * @param bitmap - Pointer to bitmap.
  236. */
  237. short TIsBitReset( short bit, const unsigned char *bitmap)
  238. {
  239. return TIsBitSet( bit, bitmap) ^ 1;
  240. }
  241.  
  242.  
  243.  
  244. /** Count number of bits set in a bitmap.
  245. *
  246. * @return Number of bits set.
  247. *
  248. * @param bitmap - Pointer to bitmap.
  249. * @param size - Bitmap size (in bits).
  250. *
  251. * @note Not very efficient in terms of execution speed. If you are doing
  252. * some computationally intense stuff you may need a more complex
  253. * implementation which would be faster (especially for big bitmaps).
  254. * See (http://graphics.stanford.edu/~seander/bithacks.html).
  255. */
  256. int TCountBits( const unsigned char *bitmap, int size)
  257. {
  258. int i, count=0;
  259.  
  260. for (i=0; i<size; i++)
  261. if (TIsBitSet( i, bitmap)) count++;
  262.  
  263. return count;
  264. }
Add Comment
Please, Sign In to add comment