A Quick Lesson In Binary Coding

Binary

Binary (Photo credit: noegranado)

So, binary is actually a lot of fun and much easier to do than it may seem. In this quick tutorial I plan to show you: what Binary numerics or Base 2  and Base 10 or decimal numerics are, how to change binary into decimal/ decimal into binary, and a quick example of what an IPv4 address/ subnet would look like in binary.

What is Binary?When you are talking about numbers, Binary is a way of counting at a base of two (0 being off,1 and being on) instead of base 10 (0,1,2,3,4,5,6,7,8,9). It’s still used in modern computers, and more than likely will continue to be seen in computers to come.  Originally when computers were designed it was a more practical option, and if it works, why change it? Well here’s the thing we can’t change it. Circuits can only be in two states: ON and OFF. For a computer to interact with the circuitry and make your computer do what it does, it needs to tell it what to do, where to power things and where to turn them off. There is no state in between on and off (this doesn’t apply to quantum computing). So all that data that gets sent has to go through as binary and work it’s way out to be deciphered into a readable and recognizable output. We have to use binary, and that’s the  only practical option we have.

What is Decimal?It’s the numbers you are use to seeing: 23928108.

What the base means, I am no expert in math so explaining this would be much easier through example.

–In 10 Base you have a number selection of 0-9 (10 digits) You take the base of 10 and apply exponents: 10(which is the base)^x

10^0   10^1       10^2       10^3           10^4             10^5       10^6              10^7            10^8              10^9 etc…
1          10          100        1000          10000      100000      1000000      10000000    100000000     1000000000

So as a foundation to understanding decimal we’ll take a number:  1,253 this number is one-thousand two-hundred fifty three
1000 x 1= 1000
100 x 2= 200
10 x 5= 50
+     1 x 3= 3
1,253

Changing Decimal to Binary:
We’ll take that same number 1253 in binary that would be  1001110101

I’m sure at this point your thinking to yourself,”that does not explain how to change decimal into binary, what kind of scam is this?!”
First, you are right. It isn’t. I’m getting to that. Second, this is free, it is not a scam. Don’t start sending me money so you can call it one.

Back on topic: the same way we did base 10 we are going to take a look at base 2.

We have a number selection of 0-1 (2 digits) to get the decimals we are going to do the same thing we did with base ten, getting the exponents and placing a column value!

So bases of 2:
2^11   –  2^10  –  2^9  – 2^8  –  2^7  –  2^6  –  2^5  –  2^4  –  2^3 – 2^2  –  2^1 – 2^0
2048       1024     512     256     128      64        32        16         8        4          2        1

I stopped at 2048 for this; there is no need to go over the decimal amount that we are trying to convert.
first thing we need to do is take 1024 and subtract it from 1253, because that is the number we are converting.
We already know that 1024 goes into 1253 so we are going to place a 1 in that spot.
1253-1024=229 so a 1 goes where 2^10 is||you can’t subtract (while getting a negative number 512 or 256 from 229 so place a 0 in those spots)
you can subtract 128 from 229 so you would place a 1 in the spot that 128 goes.

SIMPLE TIP: If you can subtract a number from the remainder of the previous number place a 1 if you can’t place a 0 in each spot following until you can subtract the number then place a 1. So if you have to subtract, place a one, if you can’t place a 0. See the example below.

EXAMPLE:

1            0          0          1           1       1         0     0    1   0   1              therefore 1253= 1001110101
1253                               229    101    37                      5   3    1
-1024     512      256    -128     -64    -32      16     8  -4       -1
= 229                              =101   =47   =5                      =1     =0

Changing Binary into Decimal: THE EASY PART.
We are going to use the same number as above for this:    10011100101
This truly is easier than converting decimal to binary all you need to know is how to multiply by one and zero and add.

You lay out the columns and place the ones and zeros respectively. Each column gets a digit.
Any column that has a one you multiply that column by 1.
Any column that has a zero you multiply it by zero.
Once you’ve multiplied everything get all of the numbers remaining and add them together to get your decimal number.

1024    512    256     128    64    32    16    8   4   2   1
1          0        0            1      1       1      0     0    1   0  1
=1024                       =128 =64    =32              =4      =1

1024+128+64+32+4+1=1253

What an IP address and it’s subnet mask would look like would look like in Binary.

Example:
IP address:          192.168.0.1
Subnet Mask:    255.255.255.0
So as I’m sure you are familiar with this I am going to explain: an IP addres is 4 octets (8-bit) numbers that give your a network address there are a total of 32 bytes in the IPv4 addresses and subnets.
A bit in the most basic way is just a digit.

So when you turn these numbers into binary you only do one octet at a time:

IP address:     192(octet).168(octet).0(octet).1(octet)
Decimal: 192.168.0.1           Binary: 11000000.10101000.00000000.00000001
Decimal 255.255.255.0      Binary:   11111111.11111111.11111111.00000000

Changing 192.168.0.1 into binary:
Just like we did earlier set out the columns, it’s easier this time because we only have 8 bits(1s/0s) to worry about, on all of these.
1       1      0    0    0    0    0  0
192   64
-128   64   32   16   8   4    2   1
=64    =0
192= 11000000

1     0     1    0   1    0   0   0
168          40         8
128  64   32  16   8   4   2   1
40             8           0
168=10101000
So far we have 11000000.10101000

0 that’s easy it’s 0. But we need 8 bits for the address to work, so for any number in the 8 columns that doesn’t have a one we give a 0.
So far: 11000000.10101000.00000000

Here is where I can give you an example of what happens when you do not have enough to make 8 bits and what you do. (Hint you make it have 8 bits)

0        0       0      0      0      0    0      1
1
128   64    32   16     8      4     2     1
0

and the given ip adress is 11000000.10101000.00000000.00000001

This might come naturally to you it might not, it’s easier than it looks, and I hope I was able to teach you binary well. If you have any questions, please feel free to contact me or leave a comment.

On a related note, here is a video my wife showed me with the same concept of alternate base numbers, except base 12 instead of base 2… and it’s less convoluted because it’s Schoolhouse Rock!