This week's book giveaway is in the Cloud/Virtualization forum.We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Liutauras Vilda
• Knute Snortum
• Bear Bibeault
Sheriffs:
• Devaka Cooray
• Jeanne Boyarsky
• Junilu Lacar
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Carey Brown
• salvin francis
Bartenders:
• Tim Holloway
• Piet Souris
• Frits Walraven

Negative Binary representation

Greenhorn
Posts: 5
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);
}
}
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
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
Thanks! That makes sense I just haven't seen it explained before anywhere.

Ranch Hand
Posts: 225
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
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
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

 Sometimes you feel like a nut. Sometimes you feel like a tiny ad. Java file APIs (DOC, XLS, PDF, and many more) https://products.aspose.com/total/java