Reputation: 2767
I have an array of 16-bit words and I want to calculate the bit to set in the proper word when just given the bit number. For example, bit 24 sets the 8th bit in the 2nd word.
Upvotes: 0
Views: 96
Reputation: 20312
Just use division to obtain the index in the array, and the remainder will be the bit number to set.
int N = 24;
int index = N / 16;
int bit = N % 16;
words[index] |= (1 << bit);
Upvotes: 8