• 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

Y 2's compliment notion used to store the value of a variable?

 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
As you know in Java, in the binary form for the value of a variable the most significant bit represents the sign and the rest bits represents the value of the variable. The value is stored as 2's compliment of the actual value.
So to get back the actual value we would need to take the 1's compliment first and then add 1 to it ? Why follow this when we could have stored the value directly?
Thanks
Deepu
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
umm... this has nothing to do with SCJP so i think you could post this in another section to get better replies.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Java in General (Intermediate)
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2s complements work perfectly in binary adders, which are easily composed of and & or gates on the lowest level of microchips. So it's a hardware influenced choice. HERE is the first binary adder that came up on Google. It's good fun to figure these things out.

Another choice is to use the high-order bit for sign and make 2 and -2 identical except for the sign bit. That's easier for humans, but it doesn't work out as well in binary arithmetic.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by deepu jain:
Why follow this when we could have stored the value directly?



The value *is* stored "directly", in 2's complement. 1's complement isn't "more direct" for the computer.

You need to understand that there isn't *the* natural representation for a number. In our culture we use the decimal system, but the romans used roman numbers. In computer source code, we often use octal or hexadecimal, when it's convenient. And then there are several ways to encode numbers in bits - 1's complement, 2's complement, BCD etc.

None of those are more direct than the others - all are equally artificial representations of an abstract concept.

Having said that, the advantage of 2's complement is that for operations such as addition, you don't need to distinguish between negative and positive values - they just work the same way.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic