I have some trouble with a C program that does some bit manipulation. In the program, I use an unsigned long long int variable to represent a 64 bit map, each bit representing a position on the map. I need to be able to update these bits (positions) i.e. setting or clearing a bit. To clear and set a bit, I do (0 is the least significant position):
map &= ~(1 << pos) // clear bit in position 'pos' map |= (1 << pos) // set bit in position 'pos' The problem is that when i perform these operations, all the bits in the map which are to the left of pos get set to 0 (while I want only the bit in position pos to change).
What am I doing wrong?
1ULLto ensure the mask is the correct size.1.