int main() {
unsigned char a = 5;
a |= (1<<((sizeof(char)<<3)-1));
char b = a;
printf("%d %d\n",b,a);
printf("%u %u\n",b,a);
}
carefully ! Here unsigned is used so range will get modified from 32/64 machine to machine.
(1<<((sizeof(char)<<3)-1))
(1<<((1<<3)-1)) ==> (1<<((8)-1)) ==> (1<<(8-1)) ==>(1<<7) so,shifting 00000001 become 10000000(128)
now, a |= (1<<((sizeof(char)<<3)-1)); //here | sign means OR so a value is 5 a= a | 128
a= 00000101 | 10000000
a=133
now, we know
Type |
Storage size |
Value range |
char |
1 byte |
-128 to 127 or 0 to 255 |
unsigned char |
1 byte |
0 to 255 |
so, b(SIGNED INT)=a(UNSIGNED INT);
so b=133%256; ==> b = -123;
printf("%d %d\n",b,a); //OUTPUT:- -123 133
printf("%u %u\n",b,a); //NOTE %u for unsigned use as value at address whereas for signed address so
//OUTPUT:- ANY MEMORY ADDRESS 133
for more see http://code.geeksforgeeks.org/pDibZO