Mercurial > hg > Members > masakoha > testcode
annotate c/bitVector/main.cc @ 52:a2826bf4e80a
remove magic number
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 04 Jun 2015 17:54:13 +0900 |
parents | 898e8d9e1c67 |
children | 3d1c71fbd4a4 |
rev | line source |
---|---|
45 | 1 #include <stdio.h> |
49
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
2 #include <stdlib.h> |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
3 #include <string.h> |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
4 |
52
a2826bf4e80a
remove magic number
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
51
diff
changeset
|
5 int bitBlock = sizeof(unsigned long) * 8; |
a2826bf4e80a
remove magic number
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
51
diff
changeset
|
6 |
51
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
7 typedef struct bitInfo { |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
8 int arrayNum; |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
9 unsigned long *bitContainer; |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
10 }BitInfo,*BitInfoPtr; |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
11 |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
12 void bitSet(BitInfoPtr bi, int bitSetPosition) { |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
13 |
50
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
14 unsigned long tmp = 1; |
51
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
15 int arrayPosition = 0; |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
16 |
52
a2826bf4e80a
remove magic number
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
51
diff
changeset
|
17 arrayPosition = bitSetPosition / bitBlock; |
a2826bf4e80a
remove magic number
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
51
diff
changeset
|
18 bitSetPosition = bitSetPosition % bitBlock; |
51
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
19 |
52
a2826bf4e80a
remove magic number
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
51
diff
changeset
|
20 tmp = tmp << (bitBlock - 1 - bitSetPosition); |
51
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
21 bi->bitContainer[arrayPosition] = bi->bitContainer[arrayPosition] | tmp; |
50
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
22 } |
49
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
23 |
51
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
24 void bitPrint(BitInfoPtr bi) { |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
25 for (int i = 0; i < bi->arrayNum ; i++) { |
52
a2826bf4e80a
remove magic number
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
51
diff
changeset
|
26 for (int j = bitBlock - 1; j >= 0; j--) { |
51
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
27 printf( "%lu", ( bi->bitContainer[i] >> j ) & 1 ); |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
28 } |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
29 printf(" "); |
49
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
30 } |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
31 puts(""); |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
32 } |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
33 |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
34 int main(int argc, char **argv) { |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
35 |
51
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
36 BitInfoPtr bi = (BitInfoPtr)malloc(sizeof(BitInfo)); |
50
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
37 int bitSetPosition = 0; |
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
38 |
49
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
39 for (int i = 1; i < argc ; i++) { |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
40 if (strcmp(argv[i],"-n") == 0) { |
50
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
41 bitSetPosition = atoi(argv[i+1]); |
49
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
42 } |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
43 } |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
44 |
52
a2826bf4e80a
remove magic number
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
51
diff
changeset
|
45 bi->arrayNum = (bitSetPosition + 1) / bitBlock; |
a2826bf4e80a
remove magic number
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
51
diff
changeset
|
46 if (((bitSetPosition + 1) % bitBlock) != 0) bi->arrayNum++; |
51
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
47 |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
48 printf("Array Num : %d\n",bi->arrayNum); |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
49 |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
50 unsigned long bitContainer[bi->arrayNum]; |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
51 for (int i = 0; i < bi->arrayNum; i++) { |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
52 bitContainer[i] = 0; |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
53 } |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
54 |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
55 bi->bitContainer = bitContainer; |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
56 bitSet(bi,bitSetPosition); |
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
57 bitPrint(bi); |
45 | 58 return 0; |
59 } |