aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question (1's complement and shifting) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question (1 Watch "Question (1 New topic
Author

Question (1's complement and shifting)

Indika Hewage
Ranch Hand

Joined: May 18, 2004
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

Joined: Apr 09, 2004
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.
Wladimir Babitzki
Greenhorn

Joined: Apr 27, 2004
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

Joined: Jul 10, 2003
Posts: 113
1 : 00000000 00000000 00000000 00000001
~1 : 11111111 11111111 11111111 11111110 (-2)
~1 >> 1 : 11111111 11111111 11111111 11111111 (-1)
Indika Hewage
Ranch Hand

Joined: May 18, 2004
Posts: 35
Thank you very much for the in detail explanation.

Regards,
Indika S
 
 
subject: Question (1's complement and shifting)