# Understanding Binary

Before you can understand the details of how IP addressing works, you need to understand how the binary numbering system works because binary is the basis of IP addressing.

Binary is a counting system that uses only two numerals: 0 and 1. In the
decimal system (with which most people are accustomed), you use 10
numerals: 0-9. In an ordinary decimal number - such as 3,482 - the rightmost
digit represents ones; the next digit to the left, tens; the next, hundreds; the
next, thousands; and so on. These digits represent powers of ten: first 10^{0}
(which is 1); next, 10^{1} (10); then 10^{2} (100); then 10^{3} (1,000); and so on.

In binary, you have only two numerals rather than ten, which is why binary numbers look somewhat monotonous, as in 110011, 101111, and 100001.

The positions in a binary number (called bits rather than digits) represent powers of two rather than powers of ten: 1, 2, 4, 8, 16, 32, and so on. To figure the decimal value of a binary number, you multiply each bit by its corresponding power of two and then add the results. The decimal value of binary 10111, for example, is calculated as follows:

1 x 20 = 1 x 1 = 1 + 1 x 21 = 1 x 2 = 2 + 1 x 22 = 1 x 4 = 4 + 0 x 23 = 0 x 8 = 0 + 1 x 24 = 1 x 16 =1623

Fortunately, converting a number between binary and decimal is something a computer is good at - so good, in fact, that you're unlikely ever to need to do any conversions yourself. The point of learning binary is not to be able to look at a number such as 1110110110110 and say instantly, "Decimal 7,606!".

Instead, the point is to have a basic understanding of how computers store information and - most important - to understand how the binary counting system works, which describe in the following section.

Here are some of the more interesting characteristics of binary and how the system is similar to and differs from the decimal system:

**In decimal, the number of decimal places allotted for a number determines how large the number can be.**If you allot six digits, for example, the largest number possible is 999,999. Because 0 is itself a number, however, a six-digit number can have any of 1 million different values.

Similarly, the number of bits allotted for a binary number determines how large that number can be. If you allot eight bits, the largest value that number can store is 11111111, which happens to be 255 in decimal.**To quickly figure how many different values you can store in a binary number of a given length, use the number of bits as an exponent of two.**An eight-bit binary number, for example, can hold 2^{8}values. Because 2^{8}is 256, an eight-bit number can have any of 256 different values. This is why a byte - eight bits - can have 256 different values.**This "powers of two" thing is why computers don't use nice, even, round numbers in measuring such values as memory or disk space.**A value of 1K, for example, is not an even 1,000 bytes: It's actually 1,024 bytes because 1,024 is 2^{10}. Similarly, 1MB is not an even 1,000,000 bytes but instead 1,048,576 bytes, which happens to be 2^{20}.

One basic test of computer nerddom is knowing your powers of two because they play such an important role in binary numbers. Just for the fun of it, but not because you really need to know, Table-1 lists the powers of two up to 32.

Table-1 also shows the common shorthand notation for various powers of two. The abbreviation K represents 2^{10}(1,024). The M in MB stands for 2^{20}, or 1,024K, and the G in GB represents 2^{30}, which is 1,024MB. These shorthand notations don't have anything to do with TCP/IP, but they're commonly used for measuring computer disk and memory capacities.

**Table-1 Powers of Two**

Power | Bytes | Kilobytes | Power | Bytes | K, MB, or GB |
---|---|---|---|---|---|

2^{1} | 2 | 2^{17} | 131,072 | 128K | |

2^{2} | 4 | 2^{18} | 262,144 | 256K | |

2^{3} | 8 | 2^{19} | 524,288 | 512K | |

2^{4} | 16 | 2^{20} | 1,048,576 | 1MB | |

2^{5} | 32 | 2^{21} | 2,097,152 | 2MB | |

2^{6} | 64 | 2^{22} | 4,194,304 | 4MB | |

2^{7} | 128 | 2^{23} | 8,388,608 | 8MB | |

2^{8} | 256 | 2^{24} | 16,777,216 | 16MB | |

2^{9} | 512 | 2^{25} | 33,554,432 | 32MB | |

2^{10} | 1,024 | 1K | 2^{26} | 67,108,864 | 64MB |

2^{11} | 2,048 | 2K | 2^{27} | 134,217,728 | 128MB |

2^{12} | 4,096 | 4K | 2^{28} | 268,435,456 | 256MB |

2^{13} | 8,192 | 8K | 2^{29} | 536,870,912 | 512MB |

2^{14} | 16,384 | 16K | 2^{30} | 1,073,741,824 | 1GB |

2^{15} | 32,768 | 32K | 2^{31} | 2,147,483,648 | 2GB |

2^{16} | 65,536 | 64K | 2^{32} | 4,294,967,296 | 4GB |

## Doing the logic thing

One of the great things about binary is that it's very efficient at handling special operations: namely, logical operations. Four basic logical operations exist although additional operations are derived from the basic four operations. Three of the operations - AND, OR, and XOR - compare two binary digits (bits). The fourth (NOT) works on just a single bit.

The following list summarizes the basic logical operations:

**AND:**An AND operation compares two binary values. If both values are 1, the result of the AND operation is 1. If one or both of the values are 0, the result is 0.**OR:**An OR operation compares two binary values. If at least one of the values is 1, the result of the OR operation is 1. If both values are 0, the result is 0.**XOR:**An XOR operation compares two binary values. If exactly one of them is 1, the result is 1. If both values are 0 or if both values are 1, the result is 0.**NOT:**The NOT operation doesn't compare two values. Instead, it simply changes the value of a single binary value. If the original value is 1, NOT returns 0. If the original value is 0, NOT returns 1.

**Table-2 summarizes how AND, OR, and XOR work.**

**Table-2 Logical Operations for Binary Values**

First Value Second Value AND OR XOR0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0

Logical operations are applied to binary numbers that have more than one binary digit by applying the operation one bit at a time. The easiest way to do this manually is to line the two binary numbers on top of one another and then write the result of the operation beneath each binary digit. The following example shows how you would calculate 10010100 AND 11011101:

10010100 AND1101110110010100

As you can see, the result is 10010100.

## Working with the binary Windows Calculator

The Calculator program that comes with all versions of Windows has a special Scientific mode that many users don't know about. When you flip the Calculator into this mode, you can do instant binary and decimal conversions, which can occasionally come in handy when you're working with IP addresses.

To use the Windows Calculator in Scientific mode, launch the Calculator by choosing Start → All Programs → Accessories → Calculator. Then, choose the View → Scientific command from the Calculator menu. The Calculator changes to a fancy scientific model.

You can select the Bin and Dec radio buttons to convert values between decimal and binary. For example, to find the binary equivalent of decimal 155, enter 155 and then select the Bin radio button. The value in the display changes to 10011011.

Here are a few other things to note about the Scientific mode of the Calculator:

- Although you can convert decimal values to binary values with the scientific Calculator, the Calculator can't handle the dotted-decimal IP address format that's described later in this tutorial. To convert a dotted-decimal address to binary, just convert each octet separately. For example, to convert 172.65.48.120 to binary, first convert 172; then convert 65; then convert 48; and finally, convert 120.
- The scientific Calculator has several features that are designed specifically for binary calculations, such as AND, XOR, NOT, NOR, and so on.
- The scientific Calculator can also handle hexadecimal conversions. Hexadecimal doesn't come into play when dealing with IP addresses, but it is used for other types of binary numbers, so this feature sometimes proves to be useful.
- Windows 7 does the scientific Calculator one step better by providing a Programmer mode which has even more features for working with binary numbers.