|
However:
|
|
|
|
Step-by-step: int i = k/32; // i = array index (use: A[i]) int pos = k%32; // pos = bit position in A[i] unsigned int flag = 1; // flag = 0000.....00001 flag = flag << pos; // flag = 0000...010...000 (shifted k positions) A[i] = A[i] | flag; // Set the bit at the k-th position in A[i] |
"Professional" version:
SetBit( A[ ], k ): A[k/32] |= 1 << (k%32); // Set the bit at the k-th position in A[i] |
In C macro definition:
#define SetBit(A,k) ( A[(k/32)] |= (1 << (k%32)) ) |
Step-by-step: int i = k/32; int pos = k%32; unsigned int flag = 1; // flag = 0000.....00001 flag = flag << pos; // flag = 0000...010...000 (shifted k positions) flag = ~flag; // flag = 1111...101..111 A[i] = A[i] & flag; // RESET the bit at the k-th position in A[i] |
Pro version:
ClearBit(A, k): A[k/32] &= ~(1 << (k%32)); |
In C macro definition:
#define ClearBit(A,k) ( A[(k/32)] &= ~(1 << (k%32)) ) |
Step-by-step: int i = k/32; int pos = k%32; unsigned int flag = 1; // flag = 0000.....00001 flag = flag << pos; // flag = 0000...010...000 (shifted k positions) if ( A[i] & flag ) // Test the bit at the k-th position in A[i] // k-th bit is 1 else // k-th bit is 0 |
Pro version:
TestBit( A, k ): if ( (A[k/32] & (1 << (k%32) )) != 0 ) // k-th bit is 1 else // k-th bit is 0 Or: if ( (A[k/32] & (1 << (k%32) )) ) // value != 0 is "true" in C ! // k-th bit is 1 else // k-th bit is 0 |
In C macro definition:
#define TestBit(A,k) ( A[(k/32)] & (1 << (k%32)) ) |
How to run the program:
|