This week's book giveaway is in the OCAJP 8 forum.We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!

Signing the bits clarification!

Gary Busby
Greenhorn

Joined: Feb 02, 2003
Posts: 5
I am working towards the SCJP 1.4 and have been working through the Mughal and Rasmussen Cert Guide. I see I have alot more work to do on bit wise operators etc.
To the point though. I have been trying to figure out how a sequence of bits could be negative or positive when it could easily be a whole number.
By trawling various threads I see that if the first bit is 0 or 1 in the highest position for that primitive type signifies whether the decimal representation is negative or positive.
eg> the dots signify zeros or ones
on a primative
byte 0... .... means it is positive and 1... .... is negative
short 0... .... .... .... is positive and 1... .... .... .... negative
int 0(31dots) is positive and 1(31dots) is negative
etc
So
byte has one sign and 7 positions
short has one sign and 15 positions
etc
I just need to get this straight in my head before moving on.
Sorry if this is obvious.
Thanks so much for your help so far, and the help in the future.
Regards,
Gary
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Was there a question there, Gary, or are you just trying to get this straight by explaining it?
Simply put, the most significant bit is a "sign" bit. It isn't really part of the number - it simply tells you if the value is negative or not. Think of it like you would the '-' in front of a negative number like -23. The '-' isn't really part of the number, it just tells you that we've got "negative" 23, rather than "positive" 23. The sign bit behaves exactly the same way.

SCJP Tipline, etc.
Gary Busby
Greenhorn

Joined: Feb 02, 2003
Posts: 5
I was trying to get it straight by explaining it.
Thank you,
Gary
Sachin Tendulkar
Greenhorn

Joined: Sep 20, 2003
Posts: 27
Yes the first bit of the number does denote the sign for all signed integral primitives. However it does not just denote the - or +. It does play a role in the magnitude or the value of a negative number. To get the actual value of a negative number you need to use the two's complement. this is done by reversing all the bits of that number and adding 1 to the result.
So if we have a number say -5. How would you denote the same in binary?
Assuming we want it to represent a byte
5 in binary is 0000 0101
Invert all the bits 1111 1010
Add 1 to the result 1111 1011 --> Thats negative 5.
As you can see that the first bit is 1. So we know its a negative number and its magnitude or value is known only after using two's complement.
So working backwards given a binary representation for a number we can get the decimal number as shown below.
Again we assume we have a byte.
Number given 1111 0011 --> Highest bit is 1, so negative
Invert all bits 0000 1100
Add 1 0000 1101 --> Thats 13
So the number given was -13.
Hope that helps.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
There are 256 8-bit binary numbers.

Half of those numbers have been designated as representing negative numbers.
Each pair, a number from the first half and a designated negative number from the second half always add up to 256.
0000 0001 + 1111 1111 == 1 0000 0000
0111 1111 + 1000 0001 == 1 0000 0000
As a consequence of this scheme, all negative numbers have a 1 in the most significant bit. This particular bit is often called the sign bit.
There is a different representation called sign-and-magnitude with a separate bit to designate the sign. A separate sign bit means there is both a positive and negative 0. Floating point numbers have a separate sign bit.
[ October 08, 2003: Message edited by: Marlene Miller ]

I agree. Here's the link: http://aspose.com/file-tools

subject: Signing the bits clarification!