The truth table for x AND y
is:
x y Output
-----------
0 0 0
0 1 0
1 0 0
1 1 1
In other words, the output/result will only be 1 if both inputs are 1, which means that you cannot change a bit from 0 to 1 through a bitwise AND. Use a bitwise OR for that (e.g. int newnumber = number | (1 << 3);
)
To summarize:
Use & ~(1 << n)
to clear bit n
.
Use | (1 << n)
to set bit n
.