Ch 1. Number Systems
1.3 Hexadecimal, Binary Conversions and Arithmetic Operations
If you are working with low-level data in computing (e.g., assembly language), you need to understand how to convert between number systems and perform arithmetic operations. This section will teach you how to convert between binary and hexadecimal, and how to perform addition and subtraction.
To convert a binary number to hexadecimal, the following method can be used:
- Pad the binary number with leading zeros so that it can be grouped into sets of four.
- Starting from the right, group the bits into sets of four.
- Convert each 4-bit group into its hexadecimal equivalent.
- Combine the hexadecimal digits.
One hexadecimal digit corresponds to 4 bits.
Example 1.8
Let’s convert the binary number 111010111 to hexadecimal. First, we pad the number with three leading zeros so that it has 12 digits (a multiple of four).
000111010111
Then starting from the right, we group the bits into sets of four.
0001 1101 0111
Now we convert each 4-bit group into its hexadecimal equivalent.
0001 in binary = [latex]0 \cdot 2^3 + 0 \cdot 2^2 + 0 \cdot 2^1 + 1 \cdot 2^0 = 0 + 0 + 0 + 1 = 1 \rightarrow[/latex] 1
1101 in binary = [latex]1 \cdot 2^3 + 1 \cdot 2^2 + 0 \cdot 2^1 + 1 \cdot 2^0 = 8 + 4 + 0 + 1 = 13 \rightarrow[/latex] D
0111 in binary = [latex]0 \cdot 2^3 + 1 \cdot 2^2 + 1 \cdot 2^1 + 1 \cdot 2^0 = 0 + 4 + 2 + 1 = 7 \rightarrow[/latex] 7
Combining the hexadecimal digits, we have [latex]1D7_{16}[/latex].
To convert a hexadecimal number to binary, the following method can be used:
- Break the hexadecimal number into individual digits.
- Convert each digit to its 4-bit equivalent.
- Combine the binary groups.
Each hexadecimal digit can be directly replaced with its 4-bit equivalent.
Example 1.9
Let’s convert the hexadecimal number [latex]3F_{16}[/latex] to binary. First, we break the hexadecimal number into individual digits.
[latex]3F_{16} \rightarrow[/latex] 3 and F
Then, we convert each digit to its 4-bit equivalent.
3 in decimal = 3 → Binary: 0011
F in hexadecimal = 15 → Binary: 1111
Finally, we combine the binary groups, to obtain [latex]001111111_2[/latex].
>Binary arithmetic follows the same principles as decimal arithmetic but uses only two digits: 0 and 1. The addition rules are
- 0 + 0 = 0
- 0 + 1 = 1
- 1 + 0 = 1
- 1 + 1 = 10 (0 with a carry of 1)
To perform binary addition, the following method can be used:
- Align the two binary numbers.
- Perform the addition starting from the rightmost bit to the leftmost bit.
- Check your work using decimals.
Example 1.10
Perform binary addition for 1011 and 1101.
First align the two binary numbers.
1011
+ 1101
-------
Then perform the addition starting from the rightmost bit to the leftmost bit.
1 + 1 = 10 → write 0, carry 1
1 + 0 + 1 (carry) = 10 → write 0, carry 1
1 + 1 + 1 (carry) = 11 →write 1, carry 1
1 → write it as the leftmost bit
Carry: 1 1 1 0
↓ ↓ ↓ ↓
1 0 1 1
+ 1 1 0 1
-------------
1 1 0 0 0
The final answer is [latex]1011_2 + 1101_2 = 11000_2[/latex]. Check your work using decimals.
[latex]1011_2[/latex] = [latex]11_{10}[/latex]
[latex]1101_2[/latex] = [latex]13_{10}[/latex]
[latex]11 + 13 = 24 \rightarrow[/latex] [latex]11000_2[/latex] = [latex]24_{10}[/latex]
The subtraction rules are
- 0 - 0 = 0
- 1 - 0 = 1
- 1 - 1 = 0
- 0 - 1 = 1 (borrow 1)
To perform binary subtraction, the following method can be used:
- Align the two binary numbers.
- Perform the subtraction starting from the rightmost bit to the leftmost bit.
- Check your work using decimals.
Example 1.11
Perform binary subtraction for 1010 and 0011.
First, align the two binary numbers.
1010
- 0011
-------
Then perform the subtraction starting from the rightmost bit to the leftmost bit.
0 - 1 → borrow → becomes 2 - 1 = 1
1 (after borrow) - 1 = 0 → but we borrowed again → becomes 1
0 - 0 = 0 → but we borrowed again → becomes 1
1 - 0 = 1
Borrow: 1 1
↓ ↓
Minuend: 1 0 1 0
Subtrah: 0 0 1 1
----------------
0 1 1 1
The final answer is [latex]1010_2 - 0011_2 = 0111_2[/latex]. Check your work using decimals.
[latex]1010_2 = 10_{10}[/latex]
[latex]0011_2 = 3_{10}[/latex]
[latex]10 - 3 = 7 \rightarrow 0011_2 = 7_{10}[/latex]
When you are programming and debugging, you often need to use hexadecimal arithmetic. While it follows base-16 rules, it is usually performed by converting to binary or decimal first, using the following method:
- Convert from hexadecimal to decimal.
- Add the decimal values.
- Convert the result back to hexadecimal.
Example 1.12
Add the hexadecimal numbers A316 and 1F16. First, convert from hexadecimal to decimal.
[latex]A3_{16}[/latex] = [latex](A \cdot 16^1) + (3 \cdot 16^0) = (10 \cdot 16) + (3 \cdot 1) = 160 + 3 = 163_{10}[/latex]
[latex]1F_{16}[/latex] = [latex](1 \cdot 16^1) + (F \cdot 16^0) = (1 \cdot 16) + (15 \cdot 1) = 16 + 15 = 31_{10}[/latex]
Then, add the decimal values.
[latex]163 + 31 = 194[/latex]
Finally, convert the result back to hexadecimal.
[latex]194 \div 16 = 12[/latex] remainder [latex]2 \rightarrow[/latex] 12 = C in hex, 2 remains 2
So, [latex]194_{10}[/latex] = [latex]C2_{16}[/latex]. The final answer is [latex]A3_{16} + 1F_{16} = C2_{16}[/latex].
Hexadecimal, binary conversions, and arithmetic operations have IT applications in memory addressing (0xFFEE), colour codes (#FF5733), and bitwise operations (logic gates).
Real-World Example 1.3: Network Packet Header Calculation
Suppose you are a network engineer tasked with analyzing packet headers in a router’s memory. Each packet header contains a checksum field used for error detection. You need to verify the checksum by adding two hexadecimal values stored in memory.
Suppose the first value in memory is [latex]7A_{16}[/latex] and the second value in memory is [latex]B4_{16}[/latex]. Then
[latex]7A_{16} + B4_{16} = 122_{10} + 180_{10} = 302_{10}[/latex]
and
[latex]302 \div 16 = 18[/latex] remainder [latex]14 \rightarrow[/latex] 18 = [latex]12_{16}[/latex], 14 = [latex]E_{16}[/latex]
So, the checksum is [latex]12E_{16}[/latex].
If the received checksum field in the packet header is also [latex]12E_{16}[/latex], then we can conclude that no transmission error has occurred. Otherwise, data corruption during transmission may have occurred.