Skip to content

Commit 9ca600f

Browse files
add array of bits
1 parent 0e476b3 commit 9ca600f

File tree

4 files changed

+94
-3
lines changed

4 files changed

+94
-3
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CC=gcc
2+
CFLGAS=-Wall
3+
DEPS =
4+
OBJ = bitsArray
5+
6+
%.o: %.c $(DEPS)
7+
$(CC) -c -o $@ $< $(CFLAGS)
8+
9+
$(OBJ): $(OBJ).o
10+
$(CC) -o $@ $^ $(CFLAGS)
11+
12+
clean:
13+
rm -f $(OBJ) $(OBJ).o
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## Bits Array
2+
### Usage
3+
```
4+
make
5+
./bitsArray
6+
```
7+
8+
### Code
9+
```c
10+
#include <stdio.h>
11+
12+
#define SetBit(A, k) (A[k/32] |= (1 << k%32))
13+
#define ClearBit(A, k) (A[k/32] &= ~(1 << k%32))
14+
#define TestBit(A, k) ((A[k/32] & (1 << k%32)))
15+
16+
int main( int argc, char* argv[] )
17+
{
18+
int A[10] = {0};
19+
int i;
20+
21+
for ( i = 0; i < 10; i++ )
22+
A[i] = 0; // Clear the bit array
23+
24+
printf("Set bit poistions 100, 200 and 300\n");
25+
SetBit( A, 100 ); // Set 3 bits
26+
SetBit( A, 200 );
27+
SetBit( A, 300 );
28+
29+
// Check if SetBit() works:
30+
for ( i = 0; i < 320; i++ )
31+
if ( TestBit(A, i) )
32+
printf("Bit %d was set !\n", i);
33+
34+
printf("\nClear bit poistions 200 \n");
35+
ClearBit( A, 200 );
36+
37+
// Check if ClearBit() works:
38+
for ( i = 0; i < 320; i++ )
39+
if ( TestBit(A, i) )
40+
printf("Bit %d was set !\n", i);
41+
}
42+
43+
```
44+
45+
## Reference
46+
47+
[Array of bits introduction](http://www.mathcs.emory.edu/~cheung/Courses/255/Syllabus/1-C-intro/bit-array.html)
48+
49+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdio.h>
2+
3+
#define SetBit(A, k) (A[k/32] |= (1 << k%32))
4+
#define ClearBit(A, k) (A[k/32] &= ~(1 << k%32))
5+
#define TestBit(A, k) ((A[k/32] & (1 << k%32)))
6+
7+
int main( int argc, char* argv[] )
8+
{
9+
int A[10] = {0};
10+
int i;
11+
12+
for ( i = 0; i < 10; i++ )
13+
A[i] = 0; // Clear the bit array
14+
15+
printf("Set bit poistions 100, 200 and 300\n");
16+
SetBit( A, 100 ); // Set 3 bits
17+
SetBit( A, 200 );
18+
SetBit( A, 300 );
19+
20+
// Check if SetBit() works:
21+
for ( i = 0; i < 320; i++ )
22+
if ( TestBit(A, i) )
23+
printf("Bit %d was set !\n", i);
24+
25+
printf("\nClear bit poistions 200 \n");
26+
ClearBit( A, 200 );
27+
28+
// Check if ClearBit() works:
29+
for ( i = 0; i < 320; i++ )
30+
if ( TestBit(A, i) )
31+
printf("Bit %d was set !\n", i);
32+
}

Data_Struct_Implementation/bitsArray/bitsArray.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)