I don't understand why the final printf
in the code below is not printing 255.
char c;
c = c & 0;
printf("The value of c is %d", (int)c);
int j = 255;
c = (c | j);
printf("The value of c is %d", (int)c);
In most implementations the char
type is signed, so it ranges from -128
to 127
.
This means, 11111111
(which is 255
written in binary) is equal to -1
. (As it's represented as a value stored in two's complement)
To get what you expect, you need to declare c
as a unsigned char
, like so:
unsigned char c = 0;
int j = 255;
c = (c | j);
printf("The value of c is %d", (int)c);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With