Mercurial > hg > Members > masakoha > testcode
view c/regexParser/bitVector.cc @ 94:43b807f88961 impl-bitvector
implement bitvector (suspend)
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 09 Nov 2015 12:26:07 +0900 |
parents | 1d7ca366b199 |
children | d0d2262d4edf |
line wrap: on
line source
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "bitVector.h" void bitPrint(BitVectorPtr bi); int bitBlock = sizeof(unsigned long) * 8; BitVectorPtr bitSet(int bitSetPosition) { BitVectorPtr bi = (BitVectorPtr)malloc(sizeof(BitVector)); bi->bitContainer = (unsigned long*)malloc(sizeof(unsigned long)*bi->arrayNum); bi->arrayNum = (bitSetPosition + 1) / bitBlock; if (((bitSetPosition + 1) % bitBlock) != 0) bi->arrayNum++; printf("Array Num : %d\n",bi->arrayNum); for (int i = 0; i < bi->arrayNum; i++) { bi->bitContainer[i] = 0; } 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; return bi; } void bitPrint(BitVectorPtr 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(""); }