You need to know the character width (in bits) on your computer. For almost everyone, this is 8. You can use the CHAR_BIT
constant from limits.h
in a C program. Then you can do some pretty simple math to find the bit offset (depending on how you count them).
The numbering of bits is on the left, with bit 2 "per bit [0] - bit 0, bit 2 bit - bit 7, and bit 2 bits in [1] - bit 8, this gives:
offset = 513 / CHAR_BIT; /* using integer (truncating) math, of course */ bit = 513 % CHAR_BIT; a[offset] |= (0x80>>bit)
There are many reasonable ways to number bits, here are two:
a[0] a[1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 This is the above 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 This is |= (1<<bit)
You can also specify a number from the other end of the array (treating it as a very large large number).
source share