Jody Frease

Greenhorn

Posts: 2

posted 15 years ago

Can anyone direct me to some good sources on the Web dealing with the basics of binary numbers and how they work? I read the article in the Campfire about bit manipulation in Java and it was tremendously helpful, but I'm missing some basic things like why the heck *is* -1 represented as 11111111 in binary?

Something along the lines of "Binary numbers for the village idiot" would be most helpful....thanks!

Something along the lines of "Binary numbers for the village idiot" would be most helpful....thanks!

posted 15 years ago

Integral numbers in Java take the form of a "signed 2's complement" value, as they do in C/C++.

The high-order bit shows the sign of the value and is not part of the numeric value itself. Putting sign aside, -1 is then stored as the bitwise complement of +1. All positive integrals have 0 as the high-order bit.

An integer in Java is 32 bits wide, but has only 31 bits of value, so:

-1 = 11111111111111111111111111111111

1 = 00000000000000000000000000000001

It's not exactly a complement because 0 takes up a "positive" slot.

This is also why you'll see the full range of an integer expressed this way:

-2^31 to (2^31 - 1)

There's nothing intuitive about it; just an implementation detail that maximized memory efficiency and has stuck around.

This is probably more than you wanted, but I dig answering this question. Simple pleasures. You can of course search the web for "signed 2's complement" on google or whichever, but most matches are geared towards mathematics, not programming.

------------------

Michael Ernest, co-author of: [URL=http://www.amazon.com/exec/obidos/ASIN/0782128254/electricporkchop]The Complete Java

[This message has been edited by Michael Ernest (edited September 27, 2001).]

The high-order bit shows the sign of the value and is not part of the numeric value itself. Putting sign aside, -1 is then stored as the bitwise complement of +1. All positive integrals have 0 as the high-order bit.

An integer in Java is 32 bits wide, but has only 31 bits of value, so:

-1 = 11111111111111111111111111111111

1 = 00000000000000000000000000000001

It's not exactly a complement because 0 takes up a "positive" slot.

This is also why you'll see the full range of an integer expressed this way:

-2^31 to (2^31 - 1)

There's nothing intuitive about it; just an implementation detail that maximized memory efficiency and has stuck around.

This is probably more than you wanted, but I dig answering this question. Simple pleasures. You can of course search the web for "signed 2's complement" on google or whichever, but most matches are geared towards mathematics, not programming.

------------------

Michael Ernest, co-author of: [URL=http://www.amazon.com/exec/obidos/ASIN/0782128254/electricporkchop]The Complete Java

[This message has been edited by Michael Ernest (edited September 27, 2001).]

*Make visible what, without you, might perhaps never have been seen.*

- Robert Bresson

greg philpott

Ranch Hand

Posts: 73

posted 15 years ago

for some more information check out: http://www.yahooligans.com/School_Bell/Math/Numbers_and_Counting/Binary_Numbers/

posted 15 years ago

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<sup>7</sup>)</td><td >(2<sup>6</sup>)</td><td >(2<sup>5</sup>)</td><td >(2<sup>4</sup>)</td><td >(2<sup>3</sup>)</td><td >(2<sup>2</sup>)</td><td >(2<sup>1</sup>)</td><td >(2<sup>0</sup>)</td><td ></td></tr></table> Now strike out the ones which have~~(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 (2<sup>8</sup> i.e. 0 to 255). If we use it to represent signed numbers, we'll need to have leftmost bit (MSB)for sign determination. ~~(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 >

-128 + 64 + 32 + 16 + 8 + 4 + 3 + 2 + 1

-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).]~~
~~

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<sup>7</sup>)</td><td >(2<sup>6</sup>)</td><td >(2<sup>5</sup>)</td><td >(2<sup>4</sup>)</td><td >(2<sup>3</sup>)</td><td >(2<sup>2</sup>)</td><td >(2<sup>1</sup>)</td><td >(2<sup>0</sup>)</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 >

**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 ( - 2<sup>7</sup> ). 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 >

**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).]

Jody Frease

Greenhorn

Posts: 2

It is sorta covered in the JavaRanch Style Guide. |