changeset 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 82fbc8478f7b
files c/bitVector/main.cc
diffstat 1 files changed, 8 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/c/bitVector/main.cc	Mon Jun 01 17:20:08 2015 +0900
+++ b/c/bitVector/main.cc	Thu Jun 04 17:54:13 2015 +0900
@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+int bitBlock = sizeof(unsigned long) * 8;
+
 typedef struct bitInfo {
     int arrayNum;
     unsigned long *bitContainer;
@@ -12,16 +14,16 @@
     unsigned long tmp = 1;
     int arrayPosition = 0;
 
-    arrayPosition = bitSetPosition / 64;
-    bitSetPosition = bitSetPosition % 64;
+    arrayPosition = bitSetPosition / bitBlock;
+    bitSetPosition = bitSetPosition % bitBlock;
 
-    tmp = tmp << (63 - bitSetPosition);
+    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 = 63; j >= 0; j--) {
+        for (int j = bitBlock - 1; j >= 0; j--) {
             printf( "%lu", ( bi->bitContainer[i] >> j ) & 1 );
         }
         printf(" ");
@@ -40,8 +42,8 @@
         }
     }
 
-    bi->arrayNum = (bitSetPosition + 1) / 64;
-    if (((bitSetPosition + 1) % 64) != 0) bi->arrayNum++;
+    bi->arrayNum = (bitSetPosition + 1) / bitBlock;
+    if (((bitSetPosition + 1) % bitBlock) != 0) bi->arrayNum++;
 
     printf("Array Num : %d\n",bi->arrayNum);