Lab 4 - Binary and Hexadecimal
From SOFTICE
(diff) ←Older revision | Current revision | Newer revision→ (diff)
|
Pedagogical Objectives
- Introduce binary and hexadecimal numbering systems
- Instruct student on how to convert between binary, hexadecimal an decimal.
Developed by:
Synopsis
This page will discuss two types of numbering systems often used in the computer industry. These are binary (base 2) and hexadecimal (base 16).
[Briefing]
Even if you're not familiar with binary and hexadecimal, you probably already use at least two other numbering systems on a daily basis. These are decimal (base 10) and duodecimal (base 12).
Terms like base 2 or base 10, referr to the number of possible values for each digit. In the decimal math that we’re all familiar with, each digit has 10 possible values ranging from 0 to 9. Hence, decimal is base 10.
With the less common, but still widely used duodecimal systems, each digit has 12 possible values ranging from 0-11 or 1-12. Examples of duodecimal math include the hours in a day, months in a year, and items in a dozen and gross.
The term binary refers to a base 2 numbering system. Each digit has two possible values – 0 and 1. Every document you store, every program you run, and every email you read is just a string of 1s and 0s interpreted by your computer. Each digit is referred to as a bit (binary digit). Groups of 8 bits are called bytes or octets.
Hexadecimal (base 16) numbering is often used as shorthand for binary. Each hexadecimal digit can represent 4 binary digits. It is much easier to convert between hexadecimal and binary than decimal and binary, as we will see shortly.
Counting
In hexadecimal, the letters A-F are equal to decimal numbers 10-15. Decimal, binary, and hexadecimal all follow the same rule when you add one to a digit that is already set to its maximum value. When this happens, set that digit to 0 and add one to (increment) the next highest digit. The table below shows how to count in these numbering systems.
| Decimal | Binary | Hexadecimal |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 1 | 1 |
| 2 | 10 | 2 |
| 3 | 11 | 3 |
| 4 | 100 | 4 |
| 5 | 101 | 5 |
| 6 | 110 | 6 |
| 7 | 111 | 7 |
| 8 | 1000 | 8 |
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 11 | 1011 | B |
| 12 | 1100 | C |
| 13 | 1101 | D |
| 14 | 1110 | E |
| 15 | 1111 | F |
| 16 | 10000 | 10 |
Number of Values
A group of two bits has four possible values – 00, 01, 10 and 11. Finding the number of possible values a given number of bits can represent by listing every possible value can quickly become tedious. For example, there are 256 possible values in a byte, and 65,536 possible values in 2 bytes. Fortunately, there is an easier way. To find the number of possible values in a string of bits, calculate 2x, where x is the number of bits. The table below provides some examples:
| Bits | Formula | Possible Values |
|---|---|---|
| 1 | 21 | 2 |
| 2 | 22 | 4 |
| 3 | 23 | 8 |
| 4 | 24 | 16 |
| 5 | 25 | 32 |
| 6 | 26 | 64 |
| 7 | 27 | 128 |
| 8 | 28 | 256 |
To calculate the number of possible values in a string of hexadecimal digits, calculate 16x, where x is the number of digits. The table below provides some examples:
| Digits | Formula | Possible Values |
|---|---|---|
| 1 | 161 | 16 |
| 2 | 162 | 256 |
| 3 | 163 | 4096 |
[Solved]
Solved 4-1: Converting from Binary to Decimal
To convert a binary number into a decimal one, calculate the decimal equivalent of each bit and add them up. The decimal equivalent of a binary 0 is 0. The decimal equivalent of a binary 1 is 2x, where x is the bit’s position.
| Binary Value | Decimal Value |
|---|---|
| 1 | 1 |
| 10 | 2 |
| 100 | 4 |
| 1000 | 8 |
| 10000 | 16 |
| 100000 | 32 |
| 1000000 | 64 |
| 10000000 | 128 |
- Let’s convert 10010011 to decimal:
10010011=27+24+21+20 27+24+21+20=128+16+2+1 128+16+2+1=145
- Here’s another example:
00110100=25+24+22 25+24+22=32+16+4 32+16+4=52
Solved 4-2: Converting from Decimal to Binary
Two methods of converting a decimal integery to a binary one are presented here. You may use whichever you prefer.
Option 1: Divide by Two
You can convert an integer from decimal to binary by recursively dividing by two, recording the remainder at each step. Once you're finished, the remainders bottom to top make up the binary integer.
Let’s use this technique to convert 170 to a byte:
170 / 2 = 85, r 0
85 / 2 = 42, r 1
42 / 2 = 21, r 0
21 / 2 = 10, r 1
10 / 2 = 5, r 0
5 / 2 = 2, r 1
2 / 2 = 1, r 0
1 / 2 = 0, r 1
170 = 10101010
Here’s another example:
100 / 2 = 50, r 0
50 / 2 = 25, r 0
25 / 2 = 12, r 1
12 / 2 = 6, r 0
6 / 2 = 3, r 0
3 / 2 = 1, r 1
1 / 2 = 0, r 1
100 = 1100100
Option 2: Reversing the Binary to Decimal Conversion
Converting from decimal to binary is just a reversal of the binary to decimal conversion. Starting with the leftmost bit, compare the decimal value represented by each bit being set to 1 to the decimal number being converted. If the bit represents a larger number than the decimal being converted, set that bit to 1 and subtract the value it represents from the decimal number. If the decimal number is less than this value, set that bit to 0. Repeat this process for every bit, moving from left to right.
Let’s use this technique to convert 170 to a byte:
170=128+32+8+2 10101010
Here’s another example:
100=64+32+4 01100100
Solved 4-3: Converting Between Binary and Hexadecimal
Fortunately, converting between binary and hexadecimal is fairly straightforward. Each hexadecimal digit corresponds to 4 bits, so the table below can be used to perform all conversions.
| Binary Value | Hexadecimal Value |
|---|---|
| 0000 | 0 |
| 0001 | 1 |
| 0010 | 2 |
| 0011 | 3 |
| 0100 | 4 |
| 0101 | 5 |
| 0110 | 6 |
| 0111 | 7 |
| 1000 | 8 |
| 1001 | 9 |
| 1010 | A |
| 1011 | B |
| 1100 | C |
| 1101 | D |
| 1110 | E |
| 1111 | F |
Here are some examples:
0111|1110=7E
B7=1011|0111
If you’d rather not memorize a table, just remember that A-F in hexadecimal corresponds to 10-15 in decimal, and then perform a binary to decimal conversion.
[Exercises]
Complete the following exercises, recording your findings, and the procedures used to obtain them in your log.
Exercise 4-1: Converting from Binary to Decimal
1. Convert 10110010 to decimal.
2. Convert 11010001010110100 to decimal.
Exercise 4-2: Converting from Decimal to Binary
1. Convert 47 to binary.
2. Convert 106,514 to binary.
Exercise 4-3: Converting Between Binary and Hexadecimal
1. Convert 01100010 to hexadecimal.
2. Convert C2 to binary.
Exercise 4-4: Real World Conversions
1. Convert the following MAC address to binary and decimal, keeping a colon between each byte.
fe:fd:00:00:5c:a4
2. Convert the following MAC address to binary and decimal, keeping a colon between each byte.
00:40:63:C5:95:70
3. Convert the following IP address to binary and hexadecimal, keeping a dot between each byte.
131.247.168.48
4. Convert the following IP address to binary and hexadecimal, keeping a dot between each byte.
192.168.0.1
5. Convert the following subnet mask to binary, keeping a dot between each byte.
255.255.128.0
6. Convert the following subnet mask to binary, keeping a dot between each byte.
255.255.255.224

