File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Complement of 41 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Complement of 41" Watch "Complement of 41" New topic

Complement of 41

Latha Kalaga
Ranch Hand

Joined: Nov 13, 2000
Posts: 96
Please excuse my ignorance, but I am missing something here. I am trying to get the complement of 41 and I am not ending up with the correct answer of -42. Please can someone help.
Here is what i did, can some one tell me where I am going wrong
41 in binary is 0010 1001
complement is 1101 0110
How does this translate to -42...
Thanks in advance.
Kathy Rogers
Ranch Hand

Joined: Aug 04, 2000
Posts: 104
Sorry, Latha, I don't really understand what you're asking. Are you not sure about how Java represents negative numbers - i.e that the left-most bit (most significant bit) indicates whether or not the number is negative and how 2's complement is used to represent numbers. Or are you unsure how to use the complement operator and what result you'll get?
Victoria Bernstein

Joined: Dec 07, 2000
Posts: 15
Let's say we have n1 and n2. I guess what you are trying to do is:
n1 = 41;
n2 = ~n1; (compliment)
41 => 0010 1001, then the compliment of this is n2=1101 0110
Now, since the most significant bit is 1, the resulting number is negative. In order to find this number you do:
1. 1's compliment of n2: 0010 1001
2. add 1: 0010 1010
3. this is 42, but since it is supposed to be negative, n2 = -42
Hopefully, it makes sense
Latha Kalaga
Ranch Hand

Joined: Nov 13, 2000
Posts: 96
Hi! Victoria and Kathy
Thanks for you messages. After going through Victoria's explanation I think I now understand how the answer came out to -42.
I was only inverting the bits in 41's decimal value which would then give me a result of -22.
[This message has been edited by Latha Kalaga (edited December 07, 2000).]
peter waterhouse

Joined: Oct 20, 2000
Posts: 15
hi! thought I would add the exact Mathematical interpretation!
the twos complement binary that java uses represents bytes as an 8 digit number where from left to right the value of each bit is:
so 41 in 2's complement binary is: 00101001
and ~41 is : 11010110
which in decimal is: -128 + 64 + 16 + 4 + 2 = -42.
hope that helps!!
Latha Kalaga
Ranch Hand

Joined: Nov 13, 2000
Posts: 96
Thanks a lot Peter. Every bit of information certainly helps.
I agree. Here's the link:
subject: Complement of 41
It's not a secret anymore!