but I'm missing some basic things like why the heck *is* -1 represented as 11111111 in binary?
Hi Jody,
I am sure explanations of others and the site links must have helped you, here is my attempt to simplify it.
I hope you are clear with positive binary numbers and the way they are represented, simply put you can get decimal number from a binary number in this way -
<table border="0" ><tr><td >0</td><td >0</td><td >0</td><td >0</td><td >0</td><td >1</td><td >0</td><td >1</td><td ></td></tr><tr><td >(128)</td><td >(64)</td><td >(32)</td><td >(16)</td><td >(8)</td><td >(4)</td><td >(2)</td><td >(1)</td><td >=> doubled on the left</td></tr><tr><td >2
7)</td><td >(2
6)</td><td >(2
5)</td><td >(2
4)</td><td >(2
3)</td><td >(2
2)</td><td >(2
1)</td><td >(2
0)</td><td ></td></tr></table> Now strike out the ones which have
0 above them and keep the ones which have
1 above them. Sum the numbers left after striking out numbers below
0. What you have is the decimal equivalent of the binary number. <table border="0" ><tr><td >0</td><td >0</td><td >0</td><td >0</td><td >0</td><td >1</td><td >0</td><td >1</td><td ></td></tr><tr><td >
(128)</td><td >(64)</td><td >(32)</td><td >(16)</td><td >(8)</td><td >(4)</td><td >(2)</td><td >(1)</td><td ></td></tr></table> 4 + 1 = 5. YES!!! Now negative numbers in binary - In 2's complement system (as it is adpted by most modern computer languages to represent negative numbers), any negative number will always have its leftmost bit (MSB - Most significant Bit) set to 1. Consider a byte (8 bits) as we have seen in the above example. A byte can represent up to 256 positive numbers (28 i.e. 0 to 255). If we use it to represent signed numbers, we'll need to have leftmost bit (MSB)for sign determination. O means positive sign and 1 means negative sign. In case of negative numbers the leftmost bit (MSB) denotes the value in negative, i.e. for a signed byte having 1 in the MSB position gives it value -128 ( - 27 ). Rest of the bits denote positive value. SO we have - <table border="0" ><tr><td >1</td><td >0</td><td >0</td><td >0</td><td >0</td><td >1</td><td >0</td><td >1</td><td ></td></tr><tr><td >(-128)</td><td >(64)</td><td >(32)</td><td >(16)</td><td >(8)</td><td >(4)</td><td >(2)</td><td >(1)</td><td ></td></tr></table> -128 + 4 + 1 = -123 in decimal. Similarly, <table border="0" ><tr><td >1</td><td >1</td><td >1</td><td >1</td><td >1</td><td >1</td><td >1</td><td >1</td><td ></td></tr><tr><td >(-128)</td><td >(64)</td><td >(32)</td><td >(16)</td><td >(8)</td><td >(4)</td><td >(2)</td><td >(1)</td><td ></td></tr></table>
-128 + 64 + 32 + 16 + 8 + 4 + 3 + 2 + 1 i.e
-128 + 127 = -1
So 11111111 binary represents -1 decimal for a byte. Same is true for an integer as well, but an integer in java is 4 bytes (32 bits), hence
11111111 11111111 11111111 11111111 represents - 1 for a signed inter.
Converting a bianry number to its 2's complement (negative number) is fairly simple.
Let's say we want to find binary equivalent of -5 (minus 5).
5 is represented in binary as -
00000101
To find 2's complement, first we 'complement' the bits, converting 0 to 1 and 1 to 0. Next we add binary 1 to that number,the resulting number is the 2's complement of that number.
00000101=> 5 in binary
11111010=> complement (also called 1's complement)
+
00000001=> add 1
------------
11111011
which is -128 + 123 = -5. Simple, isn't it
You can use same method for finding 2's complement of -1 as well. (And now you also know the reason why 11111111 represents -1).
I hope that now you'll be more comfortable with the binary numbers.
HTH,
- Manish
p.s. I don't know why I am having so many big gaps in between, I hope thet go after this edit!
[This message has been edited by Manish Hatwalne (edited September 28, 2001).]