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:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Junilu Lacar
• Liutauras Vilda
Sheriffs:
• Paul Clapham
• Jeanne Boyarsky
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Piet Souris
• Carey Brown
Bartenders:
• Jesse Duncan
• Frits Walraven
• Mikalai Zaikin

# very basic question

Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
how to represent -ve numbers in binary...
eg
x=6;
~x=-7

i know that x= 0000....0110
~x=1111....1001(how do v interpret this as 7)

Ranch Hand
Posts: 3244
• Number of slices to send:
Optional 'thank-you' note:
Priya
To use your number as an example:
7 in binary is
00000000 00000000 00000000 000000111
to get a negative number you flip all of the bits (turn all of the 1s into 0s and all of the 0s into 1s) then add 1 to the result.
To change 7 into -7:

So, the binary number 11111111 11111111 11111111 111111001, is the same as -7 in decimal.
Does that help?

Ranch Hand
Posts: 112
• Number of slices to send:
Optional 'thank-you' note:
There's a Cat and Mouse game with Bits on JavaRanch you may want to check out.
It's easiest to flip the bits and add one to get a negative number.
Here's another way to look at it.
If you substract the highest bit from the rest and negate it.
I'm rusty on bits but here's some examples:

[ August 14, 2002: Message edited by: Greg Ostravich ]
[ August 14, 2002: Message edited by: Greg Ostravich ]

 crispy bacon. crispy tiny ad: Free, earth friendly heat - from the CodeRanch trailboss https://www.kickstarter.com/projects/paulwheaton/free-heat