• 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

Negative Binary representation

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just came across this question on a mock exam:
What will be the result if you attempt to compile and run the following code ?
public class Flip{
public static void main(String argv[]){
System.out.println(~4);
}
}
The answer is -5.
I understand that the binary representation of 4 is:
0000 0000 0000 0000 0000 0000 0000 0100
and that after the flip of all the bits, the binary result is:
1111 1111 1111 1111 1111 1111 1111 1011
I know that a 1 in the leftmost position means the number is negative, but I don't understand how th above represents -5.
I realize this is kind of a basic question, but can anyone explain? Thanks.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Negative values are stored using 2's complement. To convert a 2's complement value to get its value, you first flip all of the bits and then add 1. In your example, you'd do this:

0101, is, of course, 5. So, whenever you see that you have a binary number (you can tell if the sign bit is a 1), you need to flip the bits and add 1 to determine what that number really is.
If you're still confused or want some extra practice, search for 2's complement on this forum - I'm sure you'll get a lot of results.
I hope that helps,
Corey
 
Theresa Fitzgerald
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! That makes sense I just haven't seen it explained before anywhere.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Theresa,
If you want to find the complement (~) for any number, you can also use the following expression,
~x=-x-1
Lets say you wanna find complement of 4 that is ~4, we get
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Theresa,
If you want to find the complement (~) for any number, you can also use the following expression,
~x=-x-1
Lets say you wanna find complement of 4 that is ~4, we get
~4=-4-1 = -5
I thought this was a nice trick, for the test, instead of flipping bits n all that stuff, but it doesnt hurt to know the fundas.
I hope it helps.
 
Theresa Fitzgerald
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow! That will really help out with the exam, plus knowing the fundamentals behind it is a plus. Glad to be part of this group1
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic