Mercurial > hg > Members > masakoha > testcode
changeset 51:898e8d9e1c67
implement variable bitContainers array
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 01 Jun 2015 17:20:08 +0900 |
parents | bb0e88fbbe02 |
children | a2826bf4e80a |
files | c/bitVector/main.cc |
diffstat | 1 files changed, 33 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/c/bitVector/main.cc Tue May 26 00:54:52 2015 +0900 +++ b/c/bitVector/main.cc Mon Jun 01 17:20:08 2015 +0900 @@ -2,24 +2,37 @@ #include <stdlib.h> #include <string.h> -unsigned long bitSet(int bitSetPosition, unsigned long bitContainer) { +typedef struct bitInfo { + int arrayNum; + unsigned long *bitContainer; +}BitInfo,*BitInfoPtr; + +void bitSet(BitInfoPtr bi, int bitSetPosition) { + unsigned long tmp = 1; + int arrayPosition = 0; + + arrayPosition = bitSetPosition / 64; + bitSetPosition = bitSetPosition % 64; + tmp = tmp << (63 - bitSetPosition); - bitContainer = bitContainer | tmp; - return bitContainer; + bi->bitContainer[arrayPosition] = bi->bitContainer[arrayPosition] | tmp; } -void bitGet(unsigned long bitContainer) { - for (int i = 63; i >= 0; i--) { - printf( "%lu", ( bitContainer >> i ) & 1 ); +void bitPrint(BitInfoPtr bi) { + for (int i = 0; i < bi->arrayNum ; i++) { + for (int j = 63; j >= 0; j--) { + printf( "%lu", ( bi->bitContainer[i] >> j ) & 1 ); + } + printf(" "); } puts(""); } int main(int argc, char **argv) { + BitInfoPtr bi = (BitInfoPtr)malloc(sizeof(BitInfo)); int bitSetPosition = 0; - unsigned long bitContainer = 0; for (int i = 1; i < argc ; i++) { if (strcmp(argv[i],"-n") == 0) { @@ -27,7 +40,18 @@ } } - bitContainer = bitSet(bitSetPosition,bitContainer); - bitGet(bitContainer); + bi->arrayNum = (bitSetPosition + 1) / 64; + if (((bitSetPosition + 1) % 64) != 0) bi->arrayNum++; + + printf("Array Num : %d\n",bi->arrayNum); + + unsigned long bitContainer[bi->arrayNum]; + for (int i = 0; i < bi->arrayNum; i++) { + bitContainer[i] = 0; + } + + bi->bitContainer = bitContainer; + bitSet(bi,bitSetPosition); + bitPrint(bi); return 0; }