For those visual learners in the group, here's a chart to help convert from decimal to binary.

```-------------------------------------------------------
| 256 | 128 |  64 |  32 |  16 |   8 |   4 |   2 |   1 |
-------------------------------------------------------
|     |     |     |     |     |     |     |     |     |
-------------------------------------------------------
```

The values in the top row are powers of 2 taken out to the eigth power, which is as far as you'll need to go to work with IPv4 IP addresses. If you have to go further than that, may God have mercy on your soul.

In the binary numbering system, the only digits you use are 0 and 1. So, to use this chart you'll take your decimal number, say, 150. 150 is smaller than 256, so you put a 0 in the 256 place. Then you move on to the next column. 150 is bigger than 128, so you put a 1 in the 128 place and you subtract 128 from 150, which equals 22. 22 is smaller than 64 and 32, so you put zeros in those columns. 22 is bigger than 16, so you put a 1 in the 16 column and subtract 16 from 22, which equals 6. 6 is smaller than 8 so you put a 0 in the eight column. 6 is bigger than 4 so you put a 1 in the 4 column and subract 4 from 6, which equals 2. 2 equals 2, so you put a 1 in the 2 column and subtract 2 from 2, which equals 0, which means you're done. You then put a 0 in the 1 column, because you didn't use it. The resulting chart looks like this:

```-------------------------------------------------------
| 256 | 128 |  64 |  32 |  16 |   8 |   4 |   2 |   1 |
-------------------------------------------------------
|  0  |  1  |  0  |  0  |  1  |  0  |  1  |  1  |  0  |
-------------------------------------------------------
```

The resulting number in binary is 010010110.

The E2 Offline Scratchpad rocks for making charts :)

Converting from one base to another is an exercise in dividing by a power of that base and taking the remainder. This is true for both sides of the '.' (or comma if you are European) - be it a decimal point or a binary point or any other base. This encoding closely matches how we think of numbers:
• least significant value on the right
• increasing significance moves from right to left
• each place (digit, bit, or what have you) is one power higher than the one immediately to the right of it
• The 0th power is immediately to the right of the base point.
For the number 410.25 in base 10, this can be seen written as:
(4*10^2) + (1*10^1) + (0*10^0) + (2*10^-1) + (5*10^-2)
Binary has the same set of powers, though instead of using powers of 10, the powers of 2 are used. In base 2, the above number would be written as: "110011010.01". This corresponds to:
(1*2^8) + (1*2^7) + (0*2^6) + (0*2^5) + (1*2^4) + (1*2^3) + (0*2^2) + (1*2^1) + (0*2^0) + (0*2^-1) + (1*2^-2)

Before going too far, one should realize that binary described above is not the only encoding that is used for binary. It is however, the most commonly used (a close second being the IEEE floating point format). Other binary encodings include:

# So, how do you convert to binary coded decimal?

To begin with, select the power of 2 that is one less than the power of two greater than the number. 2^9 is 512; 512 > 410.25. Thus, in this case, start with 2^8 (256). One can start at a higher power if desired - this will have no real effect upon the outcome (and at times may be required; for example if trying to fill 16 bits worth of values when only needing 10 of them). Just as the base 10 number '042' is the same as '42', the number '00101010' is the same as '101010'. For simplicity most people don't write thousands of '0's to the left of the number.

Most people are familiar with long devision and going 'down' to worth the math. Here, instead of writing:

```        1 r 154.25
+-------
256 | 410.25
```
The values shall be moved around slightly. The remainder will be written under the number, and the result (1 or 0) shall be written off to the right.
```256 | 410.25   = 1
+-------
128 | 154.25   = 1
+-------
64 |  26.25   = 0
+-------
32 |  26.25   = 0
+-------
16 |  26.25   = 1
+-------
8 |  10.25   = 1
+-------
4 |   2.25   = 0
+-------
2 |   2.25   = 1
+-------
1 |   0.25   = 0
+-------
```
At this point we have converted the 410 to binary - just take the numbers from top down and write them from left to right. This does not have to stop with integers, and certainly may continue. Understanding binary fractions becomes very important when converting a number into floating point.
```0.50 |  0.25  = 0
+------
0.25 |  0.25  = 1
+------
0.00
```
When going beyond the binary point, we are working with 1/(2^n) - with numbers such as 1/2 (0.5), 1/4 (0.25), 1/8 (0.125), 1/256 (0.00390625). Fractions that may seem perfectly rational in base 10 become difficult to work with in base 2 (or vice versa). The number 0.3 in base 10 becomes 0.0100110101... (the last bit there is 2^10, or 1/1024).

# Negative Binary Numbers

For negative numbers, the representation in binary uses the a fixed width number. For an 8 bit width, 42 becomes '0010 1010' and 13 becomes '0000 1101'. The leftmost bit becomes the sign bit (realizing this now only has 7 bits left to represent the number). In these cases, the digit left most digit is represented as -1^n * (2^7 + ... ) where n is the value of the left most digit.

The simplest form (but least useful) is to represent -13 as '1000 1101'. While this seems to make sense, it becomes very difficult to do math upon this number. Instead a system called "Twos Complement" is used in which one inverts the digits and then adds 1.

```0000 1101  (13)
1111 0010  (half way there)
1111 0011  (-13)
```
The leftmost bit still shows that it is negative. When adding 42 and -13 (which an be thought of as subtracting 13 from 42) the reason for this becomes clear:
```11    1    (carry)
0010 1010  ( 42)
1111 0011  (-13)
----------  +
0001 1101  ( 29)
```

For more on this representations of negative binary numbers, see twos complement and ones complement.

Log in or register to write something here or to contact authors.