Question (1's complement and shifting)

Indika Hewage
Ranch Hand
Posts: 35
Hi,
Please explain how can I work out value of a.

int a = 1;
a = (~a>>a);

Thanks

(edited title to be a trifle more meaningful)
[ May 19, 2004: Message edited by: Barry Gaunt ]

Karel KoboojBot
Ranch Hand
Posts: 35

1. You first check out the precedence of the operators. In this case the ~ has precedence over >>. So this will be executed first. resulting in a binary value of 111111...1111 (32 times 1).

2. Then you get the expression 1111...1111 >> 1 which results in 111...111

3. This means that the value of a will be -1.

PS: The way to convert (~) a value is by taking the binary form, inverting all bits and then adding one to the result.

Greenhorn
Posts: 19
just as a little complement to the Karnel's posting in my opinion

this will be executed first. resulting in a binary value of 111111...1111 (32 times 1).

if we have (int) 1 in its binary presentation it should be

bits___31 30 29 ... 2 1 0
values_0 0 0 ... 0 0 1

after invertion we should get

bits___31 30 29 ... 2 1 0
values_1 1 1 ... 1 1 0

also 31 times 1 and one 0 at the 0th bit
[ May 19, 2004: Message edited by: Wladimir Babitzki ]

V Bose
Ranch Hand
Posts: 113
1 : 00000000 00000000 00000000 00000001
~1 : 11111111 11111111 11111111 11111110 (-2)
~1 >> 1 : 11111111 11111111 11111111 11111111 (-1)

Indika Hewage
Ranch Hand
Posts: 35
Thank you very much for the in detail explanation.

Regards,
Indika S