I am currently working on a project in which I need bit sets. I am using uint64_t array for bit set.
My current problem is that whenever I want to set or check a bit, I need to perform the following operation:
uint64_t index = 42; bArr[index/64] |= (((uint64_t)1)<<(index%64));
I can rewrite the division modulo with some smart and bitwise operations, but listing 1 bothers me. I need this tide, since otherwise 1 treated as a 32-bit unit. As you can see from this example, you get the wrong output without casting:
uint64_t bArr[4]; // 256 bits bArr[0] = bArr[1] = bArr[2] = bArr[3] = 0; // Set to 0 uint64_t i = 255; bArr[i/64] = (bArr[i/64] | (((uint64_t)1)<<(i%64))); uint32_t i2; for (i2 = 0; i2 < 256; i2++) { if ((bArr[i2/64] & (((uint64_t)1)<<(i2%64))) != 0) { printf("bArray[%" PRIu32 "] = 1\n", i2); } }
Can I get around this toss in a smart way? I thought that performance probably suffers from a cast on every read / write ...
source share