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
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.
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.
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 ]
Danger, 10,000 volts, very electic .... tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps