-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathbitsArray.c
More file actions
32 lines (25 loc) · 798 Bytes
/
bitsArray.c
File metadata and controls
32 lines (25 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#define SetBit(A, k) (A[k/32] |= (1 << k%32))
#define ClearBit(A, k) (A[k/32] &= ~(1 << k%32))
#define TestBit(A, k) ((A[k/32] & (1 << k%32)))
int main( int argc, char* argv[] )
{
int A[10] = {0};
int i;
for ( i = 0; i < 10; i++ )
A[i] = 0; // Clear the bit array
printf("Set bit poistions 100, 200 and 300\n");
SetBit( A, 100 ); // Set 3 bits
SetBit( A, 200 );
SetBit( A, 300 );
// Check if SetBit() works:
for ( i = 0; i < 320; i++ )
if ( TestBit(A, i) )
printf("Bit %d was set !\n", i);
printf("\nClear bit poistions 200 \n");
ClearBit( A, 200 );
// Check if ClearBit() works:
for ( i = 0; i < 320; i++ )
if ( TestBit(A, i) )
printf("Bit %d was set !\n", i);
}