Search code examples
csignbituint32-t

C changing a bit of uint32_t not working


I got

uint32_t bits = 0;

bits |= 1<< 31;
bits |= 1<< 15;
bits |= 1<< 14;
bits |= 1<< 13;

which gives me 10000000000000001110000000000000

and in another function I do *(bits) |= 0 << 15; but it doesn't do anything, it should change the 16th 1 to a 0, so the following should come: 10000000000000000110000000000000

any help?


Solution

  • The correct way to change a bit to 0 is to and the negation, e.g.

    bits &= ~(1 << 15);