Mercurial > hg > Members > masakoha > testcode
view 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 |
line wrap: on
line source
#include <stdio.h> #include <stdlib.h> #include <string.h> int bitBlock = sizeof(unsigned long) * 8; 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 / bitBlock; bitSetPosition = bitSetPosition % bitBlock; tmp = tmp << (bitBlock - 1 - bitSetPosition); bi->bitContainer[arrayPosition] = bi->bitContainer[arrayPosition] | tmp; } void bitPrint(BitInfoPtr bi) { for (int i = 0; i < bi->arrayNum ; i++) { for (int j = bitBlock - 1; 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; for (int i = 1; i < argc ; i++) { if (strcmp(argv[i],"-n") == 0) { bitSetPosition = atoi(argv[i+1]); } } bi->arrayNum = (bitSetPosition + 1) / bitBlock; if (((bitSetPosition + 1) % bitBlock) != 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; }