Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- number |= 1 << x;
- number &= ~(1 << x);
- number ^= 1 << x;
- bit = number & (1 << x);
- #include <bitset>
- #include <iostream>
- int main()
- {
- std::bitset<5> x;
- x[1] = 1;
- x[2] = 0;
- // Note x[0-4] valid
- std::cout << x << std::endl;
- }
- [Alpha:] > ./a.out
- 00010
- struct bits {
- unsigned int a:1;
- unsigned int b:1;
- unsigned int c:1;
- };
- struct bits mybits;
- mybits.b = 1;
- mybits.c = 0;
- mybits.a = !mybits.a;
- mybits.b = ~mybits.b;
- mybits.c ^= 1; /* all work */
- if (mybits.c)
- enum ThingFlags = {
- ThingMask = 0x0000,
- ThingFlag0 = 1 << 0,
- ThingFlag1 = 1 << 1,
- ThingError = 1 << 8,
- }
- thingstate |= ThingFlag1;
- thingstate &= ~ThingFlag0;
- if (thing | ThingError) {...}
- /*
- ** Bit set, clear, and test operations
- **
- ** public domain snippet by Bob Stout
- */
- typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
- #define BOOL(x) (!(!(x)))
- #define BitSet(arg,posn) ((arg) | (1L << (posn)))
- #define BitClr(arg,posn) ((arg) & ~(1L << (posn)))
- #define BitTst(arg,posn) BOOL((arg) & (1L << (posn)))
- #define BitFlp(arg,posn) ((arg) ^ (1L << (posn)))
- 0000 0000 0000 0000 0000 0000 0000 0001 binary.
- 0000 0000 0000 0000 0000 0001 0000 0000 binary.
- #define BOOL(x) (!(!(x)))
- #define BitSet(arg,posn) ((arg) | (1L << (posn)))
- #define BitClr(arg,posn) ((arg) & ~(1L << (posn)))
- #define BitTst(arg,posn) BOOL((arg) & (1L << (posn)))
- #define BitFlp(arg,posn) ((arg) ^ (1L << (posn)))
- int bitmanip(int word)
- {
- word = BitSet(word, 2);
- word = BitSet(word, 7);
- word = BitClr(word, 3);
- word = BitFlp(word, 9);
- return word;
- }
- /* a=target variable, b=bit number to act upon 0-n */
- #define BIT_SET(a,b) ((a) |= (1<<(b)))
- #define BIT_CLEAR(a,b) ((a) &= ~(1<<(b)))
- #define BIT_FLIP(a,b) ((a) ^= (1<<(b)))
- #define BIT_CHECK(a,b) ((a) & (1<<(b)))
- /* x=target variable, y=mask */
- #define BITMASK_SET(x,y) ((x) |= (y))
- #define BITMASK_CLEAR(x,y) ((x) &= (~(y)))
- #define BITMASK_FLIP(x,y) ((x) ^= (y))
- #define BITMASK_CHECK(x,y) ((x) & (y))
- #define BITS 8
- #define BIT_SET( p, n) (p[(n)/BITS] |= (0x80>>((n)%BITS)))
- #define BIT_CLEAR(p, n) (p[(n)/BITS] &= ~(0x80>>((n)%BITS)))
- #define BIT_ISSET(p, n) (p[(n)/BITS] & (0x80>>((n)%BITS)))
- #define BITOP(a,b,op)
- ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
- BITOP(array, bit, |=);
- BITOP(array, bit, &=~);
- BITOP(array, bit, ^=);
- if (BITOP(array, bit, &)) ...
- struct HwRegister {
- unsigned int errorFlag:1; // one-bit flag field
- unsigned int Mode:3; // three-bit mode field
- unsigned int StatusCode:4; // four-bit status code
- };
- struct HwRegister CR3342_AReg;
- #define bit_test(x, y) ( ( ((const char*)&(x))[(y)>>3] & 0x80 >> ((y)&0x07)) >> (7-((y)&0x07) ) )
- int main(void)
- {
- unsigned char arr[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
- for (int ix = 0; ix < 64; ++ix)
- printf("bit %d is %dn", ix, bit_test(arr, ix));
- return 0;
- }
- const unsigned char TQuickByteMask[8] =
- {
- 0x01, 0x02, 0x04, 0x08,
- 0x10, 0x20, 0x40, 0x80,
- };
- /** Set bit in any sized bit mask.
- *
- * @return none
- *
- * @param bit - Bit number.
- * @param bitmap - Pointer to bitmap.
- */
- void TSetBit( short bit, unsigned char *bitmap)
- {
- short n, x;
- x = bit / 8; // Index to byte.
- n = bit % 8; // Specific bit in byte.
- bitmap[x] |= TQuickByteMask[n]; // Set bit.
- }
- /** Reset bit in any sized mask.
- *
- * @return None
- *
- * @param bit - Bit number.
- * @param bitmap - Pointer to bitmap.
- */
- void TResetBit( short bit, unsigned char *bitmap)
- {
- short n, x;
- x = bit / 8; // Index to byte.
- n = bit % 8; // Specific bit in byte.
- bitmap[x] &= (~TQuickByteMask[n]); // Reset bit.
- }
- /** Toggle bit in any sized bit mask.
- *
- * @return none
- *
- * @param bit - Bit number.
- * @param bitmap - Pointer to bitmap.
- */
- void TToggleBit( short bit, unsigned char *bitmap)
- {
- short n, x;
- x = bit / 8; // Index to byte.
- n = bit % 8; // Specific bit in byte.
- bitmap[x] ^= TQuickByteMask[n]; // Toggle bit.
- }
- /** Checks specified bit.
- *
- * @return 1 if bit set else 0.
- *
- * @param bit - Bit number.
- * @param bitmap - Pointer to bitmap.
- */
- short TIsBitSet( short bit, const unsigned char *bitmap)
- {
- short n, x;
- x = bit / 8; // Index to byte.
- n = bit % 8; // Specific bit in byte.
- // Test bit (logigal AND).
- if (bitmap[x] & TQuickByteMask[n])
- return 1;
- return 0;
- }
- /** Checks specified bit.
- *
- * @return 1 if bit reset else 0.
- *
- * @param bit - Bit number.
- * @param bitmap - Pointer to bitmap.
- */
- short TIsBitReset( short bit, const unsigned char *bitmap)
- {
- return TIsBitSet( bit, bitmap) ^ 1;
- }
- /** Count number of bits set in a bitmap.
- *
- * @return Number of bits set.
- *
- * @param bitmap - Pointer to bitmap.
- * @param size - Bitmap size (in bits).
- *
- * @note Not very efficient in terms of execution speed. If you are doing
- * some computationally intense stuff you may need a more complex
- * implementation which would be faster (especially for big bitmaps).
- * See (http://graphics.stanford.edu/~seander/bithacks.html).
- */
- int TCountBits( const unsigned char *bitmap, int size)
- {
- int i, count=0;
- for (i=0; i<size; i++)
- if (TIsBitSet( i, bitmap)) count++;
- return count;
- }
Add Comment
Please, Sign In to add comment