Wednesday, June 23, 2010

C bitwise operator

~x + 1 = -x;
e.g. ~(-1) +1 = -(-1); that is ~(-1) = 0;
e.g. ~27 +1 = -27; that is ~27 = -28;

iofmain

test 9, testing insert key, the instruction says typing 'a' 'b' 'c', but actually it
accepts 'a' 'b' because it's longer than the maxlen, so if shows all, it failed.

Monday, June 21, 2010

bitwise operator

27&14
0001 1011
0000 1110
---------
0000 1010
only 1 & 1 is 1, any other is 0
in decimal, it's 10; in Hex, it's A

27|14
0001 1011
0000 1110
---------
0001 1111
only 0 | 0 is 0, any other is 1
in decimal, it's 31; in Hex, it's 1F

27^14
0001 1011
0000 1110
---------
0001 0101
if same 0, if different 1
in decimal, it's 21; in Hex, it's 15

so, if z = x^y; z^x = y; z^y = x;

about C data type and bitwise operator

Q?1: How to case a char or int constant to short in C?
Q?2: How to case '\213' to unsigned char?
Q?3: Why float(4 bytes) allow 23 bits for mantissa since exponent occupied 8 bits,(sign takes one bit? but exponent takes 7 or 8 )
Q?4: %02x 0x stands for hexadecimal, but what is '2' for?
Q?5: why ~(-1) is 0, ~27 is -28, what is binary for '-' sign.