Mercurial > hg > Applications > Grep
annotate c/bitVector/main.cc @ 50:bb0e88fbbe02
impl bitGet and bitSet
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 26 May 2015 00:54:52 +0900 |
parents | f76fe618d5a7 |
children | 898e8d9e1c67 |
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 |
50
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
5 unsigned long bitSet(int bitSetPosition, unsigned long bitContainer) { |
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
6 unsigned long tmp = 1; |
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
7 tmp = tmp << (63 - bitSetPosition); |
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
8 bitContainer = bitContainer | tmp; |
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
9 return bitContainer; |
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
10 } |
49
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
11 |
50
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
12 void bitGet(unsigned long bitContainer) { |
49
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
13 for (int i = 63; i >= 0; i--) { |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
14 printf( "%lu", ( bitContainer >> i ) & 1 ); |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
15 } |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
16 puts(""); |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
17 } |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
18 |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
19 int main(int argc, char **argv) { |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
20 |
50
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
21 int bitSetPosition = 0; |
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
22 unsigned long bitContainer = 0; |
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
23 |
49
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
24 for (int i = 1; i < argc ; i++) { |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
25 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
|
26 bitSetPosition = atoi(argv[i+1]); |
49
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
27 } |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
28 } |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
29 |
50
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
30 bitContainer = bitSet(bitSetPosition,bitContainer); |
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
31 bitGet(bitContainer); |
45 | 32 return 0; |
33 } |