• 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

two's complement to original

 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basic to bits are bugging me...
Java stores negative numbers in form of two's complement. Ok! I know what two's complement is all about, turning 0's to 1's, 1's to 0's and adding a 1 to it. So let us say to store -7 in java way, I do a two's complement of its binary equivalent

0000 0000 0000 0000 0000 0000 0000 0111

and get

1111 1111 1111 1111 1111 1111 1111 1001.

Now, how do I revert back to -7 from here? I mean how do you say that it is a negative number and that it equals to seven, ok forget about the sign part that will be known by MSB, but what about value? :roll:

[ July 04, 2005: Message edited by: Akhil Trivedi ]
[ July 04, 2005: Message edited by: Akhil Trivedi ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It is the same thing : turns ones to zeros, and add one.

Marc.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get you marc, and as you said it goes like this...

0000 0000 0000 0000 0000 0000 0000 0111

i.e.

+7

If I leave MSB then

1000 0000 0000 0000 0000 0000 0000 0111

it goes somewhere else..

So given a binary number, you look at its MSB
if its 0-- then number is positive, nothing to do, just find the value.
if its 1-- then number is negative and is in its two's complement format, to find its value again take two's complement (including MSB) .

Thanks a lot marc. :-)
 
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic