• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Complement of 41

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Peter. Every bit of information certainly helps.
 
reply
    Bookmark Topic Watch Topic
  • New Topic