aspose file tools*
The moose likes Beginning Java and the fly likes Output of Bitwise ~ operator ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Output of Bitwise ~ operator ?" Watch "Output of Bitwise ~ operator ?" New topic
Author

Output of Bitwise ~ operator ?

Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
hi all
i have the following code
int a=3;
int b=~a;

but if we are calculating it a=0011 in binary after inverting we get 1100 to decimal it is 12 .but i am getting as output -4 .can anyone tell why the output is like that ?

thanks


Anto Telvin Mathew<br />Many of the life failures are people who did not realize how close they were to success when they give up. EDISON
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
I hope you know that In Java, negative values are stored in two's complement format.

and
int a = 3 does not mean a = 0011 in binary because int is 32 bit in java
therfore
a is 00000000 00000000 00000000 00000011 in binary
now taking ~a ==> 11111111 11111111 11111111 11111100 which is a negative value . We will take two`s compliment now. Which comes out to be 4

Hence answer is -4.
Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
thanks i got it
 
jQuery in Action, 2nd edition
 
subject: Output of Bitwise ~ operator ?