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.

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? Kathy

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

posted

0

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. Thanks [This message has been edited by Latha Kalaga (edited December 07, 2000).]

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: -128/64/32/16/8/4/2/1 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

posted

0

Thanks a lot Peter. Every bit of information certainly helps.