Module: C:\BINK\tst.cGroup: 'DGROUP' CONST,CONST2,_DATA,_BSS
Segment: _TEXT BYTE 00000008 bytes0000 0c 84 bitmanip_ or al,84H ; set bits 2 and 70002 80 f4 02 xor ah,02H ; flip bit 9 of EAX (bit 1 of AH)0005 24 f7 and al,0f7H0007 c3 ret
No disassembly errors
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;}
struct HwRegister {unsigned int errorFlag:1; // one-bit flag fieldunsigned int Mode:3; // three-bit mode fieldunsigned int StatusCode:4; // four-bit status code};
struct HwRegister CR3342_AReg;
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 %d\n", ix, bit_test(arr, ix));
return 0;}
注释:这被设计成快速(考虑到它的灵活性)和非分支。当编译Sun Studio 8时,它会产生高效的SPARC机器代码;我还在amd64上使用MSVC++2008对它进行了测试。可以制作类似的宏来设置和清除位。与这里的许多其他解决方案相比,这个解决方案的关键区别在于它适用于几乎任何类型的变量的任何位置。
// Initialize boolean array to detect UN-printable characters,// then call function to toggle required bits true, while initializing a 2nd// boolean array as the complement of the 1st.for(i=0; i<sizeof(IsGph); i++) {if(IsGph[i]) {IsNotGph[i] = 0;} else {IsNotGph[i] = 1;}}
set_bit Atomically set a bit in memoryclear_bit Clears a bit in memorychange_bit Toggle a bit in memorytest_and_set_bit Set a bit and return its old valuetest_and_clear_bit Clear a bit and return its old valuetest_and_change_bit Change a bit and return its old valuetest_bit Determine whether a bit is set
int set_nth_bit(int num, int n){return (num | 1 << n);}
int clear_nth_bit(int num, int n){return (num & ~( 1 << n));}
int toggle_nth_bit(int num, int n){return num ^ (1 << n);}
int check_nth_bit(int num, int n){return num & (1 << n);}
#define INT_BIT (unsigned int) (sizeof(unsigned int) * 8U) //number of bits in unsigned int
int main(void){
unsigned int k = 5; //k is the bit position; here it is the 5th bit from the LSb (0th bit)
unsigned int regA = 0x00007C7C; //we perform bitwise operations on regA
regA |= (1U << k); //Set kth bit
regA &= ~(1U << k); //Clear kth bit
regA ^= (1U << k); //Toggle kth bit
regA = (regA << k) | regA >> (INT_BIT - k); //Rotate left by k bits
regA = (regA >> k) | regA << (INT_BIT - k); //Rotate right by k bits
return 0;}