File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question (1's complement and shifting)

 
Indika Hewage
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 : 00000000 00000000 00000000 00000001
~1 : 11111111 11111111 11111111 11111110 (-2)
~1 >> 1 : 11111111 11111111 11111111 11111111 (-1)
 
Indika Hewage
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for the in detail explanation.

Regards,
Indika S
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic