Binary Numbers: Difference between revisions

From SlackWiki
Jump to navigation Jump to search
(Initial import from webarchive - how to do exponents?)
 
m (Fixed some formatting)
 
Line 29: Line 29:
The Binary Number system works the same way, and is a pretty easy thing to learn, but may be confusing at first. The first thing you need to memorize is 2 raised to the powers, for instance:
The Binary Number system works the same way, and is a pretty easy thing to learn, but may be confusing at first. The first thing you need to memorize is 2 raised to the powers, for instance:
  2^0 2^1  2^2  2^3  2^4  2^5  2^6  2^7  2^8
  2^0 2^1  2^2  2^3  2^4  2^5  2^6  2^7  2^8
1  2   4   16  32 64 128 256
  1  2   4   8     16   32   64   128   256


Instead of a 'ones' place, a 'tens' place, a 'hundreds' place, and so on like in base 10, there is a 'ones' place (2^0), and 'twos' place (2^1), a 'fours' place (2^2), an 'eights' place (2^3), and so on...
Instead of a 'ones' place, a 'tens' place, a 'hundreds' place, and so on like in base 10, there is a 'ones' place (2^0), and 'twos' place (2^1), a 'fours' place (2^2), an 'eights' place (2^3), and so on...

Latest revision as of 13:40, 31 August 2009

Based on Sandman1's tutorial, with heavy edits and additions by rworkman.

You probably remember from learning numbers in elementary/primary school how you have the 'ones' place and the 'tens' place and the 'hundreds' place and so on... Let's take the number 4,728 for an example:

The digit in the 'ones' place is 8
The digit in the 'tens' place is 2
The digit in the 'hundreds' place is 7
The digit in the 'thousands' place is 4

The ones/tens/hundreds/etcetera come from the fact that in base 10, the digits of a number are laid out like this:

10^0 = 1 -----> 'ones' place
10^1 = 10 ----> 'tens' place
10^2 = 100 ---> 'hundreds' place
10^3 = 1000 --> 'thousands' place 
10^4 = 10000 -> 'ten-thousands' place
and so on...

So, in the number given above (4,728), we have:

8 'ones' -------> 8 x 1 = 8
2 'tens' -------> 2 x 10 = 20
7 'hundreds' ---> 7 x 100 = 700
4 'thousands' --> 4 x 1000 = 4000
Add up these totals:
8 + 20 + 700 + 4000 = 4728

The Binary Number system works the same way, and is a pretty easy thing to learn, but may be confusing at first. The first thing you need to memorize is 2 raised to the powers, for instance:

2^0 2^1  2^2  2^3   2^4   2^5   2^6   2^7   2^8
 1   2    4    8     16   32    64    128   256

Instead of a 'ones' place, a 'tens' place, a 'hundreds' place, and so on like in base 10, there is a 'ones' place (2^0), and 'twos' place (2^1), a 'fours' place (2^2), an 'eights' place (2^3), and so on...

Now we first want to be able to take a binary number and turn it into decimal format or base 10. So the first example:

1   1   1   1
*   *   *   *
8   4   2   1
^   ^   ^   ^
8 + 4 + 2 + 1 = 15

Now from the example above you see how the 8 is on the left and the 1 is on the right. This will always be that order. You see above that you multiply the 1's with the actual digit it's located in. Now this is all hard to explain so I am going to show you another example:

1   0   0   0   0
*   *   *   *   *
16  8   4   2   1
^   ^   ^   ^   ^
16 +0 + 0 + 0 + 0 = 16

Now from the example above you see how it is 16 because all of the other placements are 0's and everything multiplied by zero is zero. Now converting from decimal to binary is a bit trickier, here is a simple example:

20 is the number we will be converting. We first need to find the highest value of the binary numbers above so we can subtract from it. 32 is to high because it is a bigger than 20. So we will use 16. We put a 1 in the 16 place and subtract from 20:

20 - 16 = 4

Now the next power is the 2^3 and that is 8. Since 8 is bigger than 4 we can put a zero. So we now are at 22 which is 4. Well, 4 is not bigger than 4 because it is the same number, so we subtract 4 and put a 1 there.

4 - 4 = 0

Since we have nothing left we just fill the rest of the placements with zeros:

10100

Now you can double check the work and find out that it is 20. Now I will show one more example, this time I will convert 25 to binary. For this example I will start by putting a 1 in the 2^4's place and subtract:

25 - 16 = 9

Now we have 9, so we look at the next power and see if it can go into 9. Which the next number is 8 and we can so we put a 1 in the 2^3's place. Now we subtract again:

9 - 8 = 1

Now we have 1 left over, now we want to put this in 20's place. So we skip over the 2^2 and 2^1. The reason why we skip this is because 1 cannot go into 4 and 2 equally. Now we have this binary number:

11001

This leads to a discussion about ipv4 addressing. An ipv4 address consists of 32 bits. 8 bits = 1 byte ; therefore, an ipv4 address is 4 bytes long. For example, 192.168.10.100 is an ipv4 address. Each byte of the address is separated by a dot (.) - in other words, the first byte (or eight bits) of the address is 192, the next is 168, the next is 10, the last is 100. You're probably wondering where the "eight bits" comes into play, right?  :-)

In binary, 192 = 11000000 and 168=10101000 and 10 = 00001010 and 100 = 01100100 Work it out for yourself (based on the information above) if you don't believe me.

You've probably seen the CIDR notation before - maybe an address block was written like this: 192.168.10.1/24 What that tells you is that only the first 24 bits of the address are important. That one's easy, as 24 bits equals 3 bytes; therefore, only the first three octets are important. In other words, 192.168.10.1/24 would match any ip address from 192.168.10.1 through 192.186.10.255. That same pattern applies for a netmask of /8, /16, or /32. 192.0.0.0/8 would match any ip address that begins with 192. 192.168.0.0/16 would match any ip address that begins with 192.168. 192.168.10.100/32 will match *only* .100 -- this netmask means that *all* 32 bits are important; they must all match (as opposed to only 8 or 16 or 24 or whatever other number matching).

So what if the netmask doesn't work out to a nice and easy byte boundary? Well... let's look at our original ip address again.

192.168.10.100 = 11000000.10101000.00001010.01100100

Suppose you're given 192.168.10.100/29 and asked to calculate a valid range of ip addresses. This tells you that the first 29 bits are important, but the others can change and still match the netmask. The first 29 bits are: 11000000.10101000.00001010.01100*** The *** represents the three bits that can change. For those three bits, here are your options (I'm leaving a space between the 29th and 30th bit):

11000000.10101000.00001010.01100 000 = 192.168.10.96
11000000.10101000.00001010.01100 001 = 192.168.10.97
11000000.10101000.00001010.01100 010 = 192.168.10.98
11000000.10101000.00001010.01100 011 = 192.168.10.99
11000000.10101000.00001010.01100 100 = 192.168.10.100
11000000.10101000.00001010.01100 101 = 192.168.10.101
11000000.10101000.00001010.01100 110 = 192.168.10.102
11000000.10101000.00001010.01100 111 = 192.168.10.103

Typically, this would be denoted as 192.168.10.96/29 The next /29 block would be 192.168.10.104/29, and then 192.168.10.112/29, and so on.

Hopefully that helped a bit :) --rworkman