-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitArray.c
105 lines (98 loc) · 3.7 KB
/
bitArray.c
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "stdio.h"
#define BITMASK(HiBit, LoBit) (((0x01 << ((HiBit) + 1 - (LoBit))) - 1) << (LoBit))
#define SIZE 7 //8 26 bits field 26*8 = 208 bits, ie., 7 32 bit field is needed
#define MAX_PAD 6 // every byte may have max 26 bits of data, so max padding
// in a byte is 32-26 = 6 bits
#define DATA_SIZE 26// size of each value 26*8 entries = 208 bits
//
int bitArr[SIZE] = {0};
int read(unsigned int addr, int *data){
int idx = 0, pos = 0, mask = 0, mask1 = 0, mask2 = 0;
idx = SIZE - ((addr*DATA_SIZE) / 32) - 1;
pos = ((addr*DATA_SIZE) % 32);
if(pos < MAX_PAD) { // value is present in a 1 byte bitArr[idx]
mask = BITMASK(pos+DATA_SIZE-1, pos);
*data = (bitArr[idx] & mask) >> (pos);
}else { // value is spread across 2 bytes bitArr[idx] and bitArr[idx-1]
mask1 = BITMASK(31, pos);
mask2 = BITMASK((pos-MAX_PAD-1), 0);
*data = ((bitArr[idx] & mask1)>>pos) | ((bitArr[idx - 1] & mask2)<<(32-pos));
}
return 0;
}
int write(unsigned int addr, int * data){
int idx = 0, pos = 0, mask = 0, mask1 = 0, mask2 = 0;
idx = SIZE - ((addr*DATA_SIZE) / 32) - 1;
pos = ((addr*DATA_SIZE) % 32);
if(pos < MAX_PAD) { // value is present in a 1 byte bitArr[idx]
mask = BITMASK(pos+DATA_SIZE, pos);
bitArr[idx] = (bitArr[idx] & ~mask) | (*data << pos);
} else { // value is spread across 2 bytes bitArr[idx] and bitArr[idx-1]
mask1 = BITMASK(31, pos);
mask2 = BITMASK(pos-MAX_PAD-1,0);
bitArr[idx] = (bitArr[idx] & ~mask1) | ((*data & BITMASK(31-pos, 0)) << pos);
bitArr[idx-1] = (bitArr[idx - 1] & ~mask2) | ((*data & BITMASK(DATA_SIZE-1,32-pos)) >> (32-pos));
}
return 0;
}
int main(){
char c;
unsigned int loc = 0, data = 0, n = 0, i = 0;
do {
printf("BitArray Read/write/display/quit(r/w/d/q):");
scanf(" %c",&c);
switch(c){
case 'r':
printf("enter location(0-7):");
scanf(" %d",&loc);
if(loc>7){
printf("invalid location %d\n",loc);
continue;
}
read(loc, &data);
printf("[%x] : 0x%x\n", loc, data & 0x3ffffff);
break;
case 'w':
printf("enter location(0-7):");
scanf(" %d",&loc);
if(loc>7){
printf("invalid location %d\n",loc);
continue;
}
printf("Enter data(range: 0x0-0x3FFFFFF):");
scanf(" %x",&data);
if (data > 0x3FFFFFF){
printf("data exceeding 26 bits range (0x0-0x3FFFFFF): 0x%x\n",data);
continue;
}
write(loc,&data);
read(loc, &data);
printf("[%x] : 0x%x\n", loc, data);
break;
case 'd':
printf("enter start location(0-7):");
scanf(" %d",&loc);
if(loc>7){
printf("invalid location %d\n",loc);
continue;
}
printf("no of entry(range:[1-%d]):",(8-loc));
scanf(" %d",&n);
if (n > (8 - loc)){
printf("num entry out of range[1-%d]",(8-loc));
continue;
}
for (i=0;i<n;i++){
read(loc+i, &data);
printf("[%x] : 0x%x\n", loc+i, data);
}
break;
case 'q':
break;
default:
printf("invalid option\n");
return 0;
}
}while(c != 'q');
return 0;
}