I have this code where I use "bitwise and" (&
) to try and change x
:
#include <stdio.h>
int main (void){
unsigned long x = 0;
printf("x = %lu\n", x);
x &= 1;
printf("x = %lu\n", x);
return 0;
}
I compile and execute the code in order to get output:
x = 0
x = 0
But I expected:
x = 0
x = 1
Why?
It seems you mean the bitwise inclusive OR operator that can be used to set bits
Or you could use another bitwise operator: the bitwise exclusive OR operator that can be used to toggle bits
Otherwise
0 & 1
will give0
because initiallyx
was initialized by0
Here is a demonstration program
The program output is