University › Science and Engineering › Digital Electronics
Number Systems and Conversions: Binary, Octal, Hexadecimal
Convert decimal, binary, octal and hexadecimal numbers in both directions and verify results by reverse conversion. Bit count, range and overflow.
Learning Outcomes
- It can convert decimal, binary, octal, and hexadecimal numbers in various ways and verify the result by reverse conversion.
- You can calculate the range of values that a binary number can represent based on its number of bits and determine the overflow condition.
- For an application that compares BCD with pure binary representation and requires decimal places, they can select the appropriate representation.
Topic Explanation
Why do digital circuits keep numbers in binary?
The basic building block of a digital circuit is the transistor, which acts like a switch. The transistor is either in the cut-off region or in the saturation region; the circuit produces two stable states. We refer to these two states as 0 and 1. In a binary system, information is carried not by the exact value of the voltage, but by the range in which it falls. Low voltage means logical 0, and high voltage means logical 1. Even if the voltage deviates slightly, the information is not corrupted as long as it still remains within the same range. In an analog system, a small deviation directly translates to an error. Therefore, digital systems are naturally more resistant to noise.
A single 0/1 digit is called a bit (binary digit). Eight bits make up one byte, which is the smallest unit of data that most processors can address. Our everyday use of decimals is based on base 10; digital hardware, on the other hand, uses base 2. Knowing how to convert between the two is the first prerequisite for being able to read what is inside the hardware. When reading a memory address, a color code, or an error message, we often encounter hexadecimal notation; This representation is a human-readable abbreviation of binary.
Positional notation: base and place value
All positional number systems are based on the same rule: each digit is multiplied by a power of the base, and the results are summed. The first digit on the right has the value b^0, the one to its left has the value b^1, and the one further to the left has the value b^2. We can write the decimal number 2026 as 2·10^3 + 0·10^2 + 2·10^1 + 6·10^0. The same rule applies to base 2. The digit weights of the binary number 1101 are 8, 4, 2, and 1 from left to right; 1·8 + 1·4 + 0·2 + 1·1 = 13.
The general expression is of the form N = Σ d_i · b^i, where d_i is the digit, b is the base, and i is the digit position. In the octal system, the base is 8 and the digits are 0–7, while in the hexadecimal system, the base is 16 and the digits are 0–9 and A–F. The letter A represents 10, B 11, C 12, D 13, E 14, and F 15. In hexadecimal, after F, a new digit is used; 10₁₆ means 16 in decimal, not 10 in decimal. This confusion is the most common pitfall for new learners.
Octal and hexadecimal: shorthand for binary
Octal and hexadecimal systems exist for humans, not for hardware. Since 2^3 = 8, every three binary digits correspond exactly to one octal digit. Since 2^4 = 16, every four binary digits correspond to one hexadecimal digit. Grouping is always done starting from the binary point; if complete groups are not formed, a zero is added to the far left. For example, the binary representation of the decimal number 45 is 101101. If we divide it into groups of three, we get 101 101 → 55₈, and if we divide it into groups of four, we get 0010 1101 → 2D₁₆. Both represent the same number.
The reverse conversion is also based on the same grouping. When writing the number 377₈, we convert each octal digit into three bits: 3→011, 7→111, 7→111; when combined, this becomes 011111111, and by dropping the leading zero, it becomes 11111111₂. FF₁₆, on the other hand, also gives 11111111₂ with F→1111 and F→1111. This is why hexadecimal is used in memory dumps, register values, and network addresses: it provides short and readable groupings instead of long bit strings.
Decimal to binary: repeated division
To convert a decimal integer to binary, the number is divided by two until it equals 1, and the remainder of each division is noted. The remainders are arranged in reverse order of reading; that is, the last remainder becomes the leftmost digit. Let's take the number 45 as an example: 45 ÷ 2 = 22, remainder 1; 22 ÷ 2 = 11, remainder 0; 11 ÷ 2 = 5, remainder 1; 5 ÷ 2 = 2, remainder 1; 2 ÷ 2 = 1, remainder 0; 1 ÷ 2 = 0, remainder 1. When the remainders are read from bottom to top, we get 101101₂.
The result obtained should always be verified by performing the inverse operation. 101101₂ = 1·32 + 0·16 + 1·8 + 1·4 + 0·2 + 1·1 = 32 + 8 + 4 + 1 = 45. If the verification step is skipped, reading the remainders in reverse order or performing a division incorrectly will not be noticed. The same method is applied by dividing by eight for base 8 and by sixteen for base 16; 45 ÷ 16 = 2 remainder 13 (D), 2 ÷ 16 = 0 remainder 2 → The result 2D₁₆ matches the previous grouping result.
Binary to decimal: sum of place values
To convert a binary number to decimal, it is sufficient to add the weights of each digit that is 1. For 101101₂, the digits that are 1 have weights of 32, 8, 4, and 1; the sum is 45. The same addition is performed for octal and hexadecimal numbers, except that the weights are powers of 8 and 16. Let's calculate the number 2A3₁₆ as an example: 2·16² + 10·16¹ + 3·16⁰ = 2·256 + 10·16 + 3 = 512 + 160 + 3 = 675. Note that the letter A is taken as 10 here.
Octal 17₈ equals 1·8 + 7 = 15. Octal 17₈ and decimal 17 are not the same number; the meaning of a number written without information about the base is ambiguous. Therefore, in technical documents, the base is usually indicated with a subscript or a prefix: 0b101101 is binary, 0o55 is octal, and 0x2D is hexadecimal. The 0x prefix is a common notation in software applications and indicates a hexadecimal number.
Bit count, value range and overflow
An n-bit binary word has 2^n different values. In an unsigned interpretation, these values are ordered between 0 and 2^n − 1. Four bits cover the range 0–15, eight bits 0–255, and ten bits 0–1023. If an application needs to store a thousand distinct values, nine bits are not enough (2^9 = 512), but ten bits are sufficient (2^10 = 1024). In hardware design, bit width is determined by this calculation; unnecessary width increases cost and power consumption, while insufficient width leads to overflow.
Overflow occurs when one is added to the highest value. When 1 is added to the four-bit number 1111 (15), the result is 10000; only 0000 is retained in the four-bit register, and the leftmost 1 is discarded as an overflow bit. Timer counters use this very feature: the mod-16 counter automatically returns to 0 after 15. In signed number representation, the leftmost bit carries the sign, and the range is from −2^(n−1) to 2^(n−1)−1; an eight-bit signed number ranges from −128 to +127.
BCD (8421) versus pure binary representation
In the BCD (Binary Coded Decimal) method, each digit of the decimal number is encoded separately into four bits. The number 45 is written as 0100 0101 in BCD, because 4→0100 and 5→0101. The pure binary representation of the same number is 101101. The bit sequences of the two representations are different and should not be confused. In the BCD method, the values 1010–1111 of the four bits are invalid; that is, six of the sixteen codes are wasted. On the other hand, keeping the digits separate is very convenient in circuits that require decimal output, such as clocks, calendars, and calculators.
If we write the number 99 in BCD, it becomes 1001 1001, and if we write it in pure binary, it becomes 1100011. When designing a BCD adder, if the sum of each digit exceeds 9, a correction is made by adding six; otherwise, the result will be an invalid BCD code. For example, the operation 9 + 1 yields 1001 + 0001 = 1010; this is invalid. When 0110 is added, 10000 is obtained, i.e., 0001 0001 (11 decimal) in BCD. This correction rule is the most important feature that distinguishes BCD circuits from pure binary adders.
Base conversion of fractional numbers
With integer division, the fractional part is converted by multiplication. We multiply the fractional part by two, take the resulting integer as a bit, and multiply the remaining fraction by two again. For the number 0.625: 0.625·2 = 1.25 → bit 1 and the remainder 0.25; 0.25·2 = 0.5 → bit 0; 0.5·2 = 1.0 → bit 1, and the process ends. The result is 0.101₂. Check: 1/2 + 0/4 + 1/8 = 0.5 + 0.125 = 0.625. Verification is done in the same way.
Some decimal fractions cannot be written as finite numbers in binary. The number 0.1 repeats in binary as 0.0001100110011…; a small error occurs when flooring or truncation is performed. This situation creates a problem of precise measurement in embedded systems. Fixed-point representation is used as a solution; for example, 8 of the 16 bits can be allocated to the integer part and 8 to the fractional part. Then the smallest resolution is 1/256, and the number 0.1 is rounded to one of the values 0.100390625 or 0.099609375.
Verifying conversions and common mistakes
The safest habit of the person performing the conversion is to reverse the result. When converting from binary to hexadecimal, grouping should be done from the binary point, and any incomplete groups on the left should be filled in with zeros. Grouping from the right only works for integers; for fractional parts, separate grouping is required in both directions from the decimal point. Another mistake is to think of the letters A–F in hexadecimal as random symbols instead of their decimal equivalents. Thinking of a value with the prefix 0x as decimal is in the same category.
The table below summarizes commonly used equivalents. Each row in the table can be verified in both directions; for example, the equation 11111111₂ = 255₁₀ = FF₁₆ = 377₈ shows that four different representations express the same number. Instead of memorizing these equivalences, applying digit weights allows you to use the same method for every new value encountered. Ultimately, conversion is a matter of calculation, not guesswork.
| Decimals | Duality | Eighth | Hexadecimal |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 10 | 1010 | 12 | A |
| 15 | 1111 | 17 | F |
| 16 | 10000 | 20 | 10 |
| 45 | 101101 | 55 | 2D |
| 255 | 11111111 | 377 | FF |
Key Points
- In positional notation, the weight of a digit is the power of the base; the first digit on the right has a value of b^0.
- Three binary digits correspond to one octal digit, and four binary digits correspond to one hexadecimal digit.
- n bits have a value of 2^n; the unsigned range is between 0 and 2^n − 1.
- BCD also encodes each decimal digit into four bits and does not use the codes 1010–1111.
Worked Examples
Worked Example
Worked Example
Worked Example
Worked Example
Worked Example
Common Mistakes
- Reading the remainders during division instead of arranging them in reverse order and producing an incorrect binary result.
- Thinking that octal 17 and decimal 17 are the same number; ignoring the base system.
- To think that BCD 45 and binary 45 are the same bit string; to confuse the two representations.
- Reversing the result of the conversion, passing validation, and not noticing the overflow error.
Tips
- In hexadecimal, the letter A means 10, and the letter F means 15; 10₁₆ is 16 in decimal.
- Grouping from binary to hexadecimal starts at the binary point; leading incomplete groups are padded with zeros.
- In fractional binary numbers, bits are generated from left to right by multiplication.
- Decimals such as 0.1 cannot be written finitely in binary; fixed-point representation is required.
Practice Questions
- 1. What is a single 0/1 digit called in the binary system?
- 2. How many different values can an 8-bit unsigned word have?
- 3. What is the binary equivalent of the decimal number 13?
- 4. What is the decimal equivalent of the binary number 1011?
- 5. What is the decimal value of the digit A in hexadecimal?
- 6. What is the decimal equivalent of the hexadecimal number 2A3?
- 7. What is the decimal equivalent of the octal number 17?
- 8. What is the binary equivalent of the decimal number 45?
- 9. How many bits are in a byte?
- 10. What is the hexadecimal equivalent of the binary number 11111111?
- 11. What is the octal equivalent of the binary number 11111111?
- 12. What is the decimal value of the hexadecimal digit F?
- 13. How many octal digits do three binary digits correspond to?
- 14. How many hexadecimal digits do four binary digits correspond to?
- 15. What is the largest value of an unsigned 10-bit word?
- 16. What is the minimum number of bits required to represent 1000 different values?
- VOLUME 17 What is the hexadecimal representation of the decimal number 16?
- 18. What is the BCD (8421) representation of the number 45?
- 19. What is the pure binary representation of the number 99?
- 20. What is the binary equivalent of 0.625?
- 21. What is the binary equivalent of 0.5?
- 22. Which of the following decimals cannot be written as a finite number in binary?
- 23. In a positional number system, what is the weight of the rightmost digit?
- 24. If 1 is added to the 4-bit number 1111, what remains in the 4-bit register?
- 25. What is the smallest value of an eight-bit signed number?
- 26 What is the largest value of an eight-bit signed number?
- 27 What is the hexadecimal equivalent of the binary number 101010?
- 28 What is the decimal equivalent of the hexadecimal number 10?
- 29 What is the decimal equivalent of the hexadecimal number FF?
- 30 What is the decimal equivalent of the octal number 377?
- 31 What is the decimal equivalent of the binary number 1010?
- 32 Which of the following is a four-bit code that is invalid in BCD code?
- 33 In fixed-point representation, if 4 out of 8 bits are allocated to the fractional part, what is the resolution?
- 34 What is the decimal equivalent of the binary number 0.101?
- 35 If the weight of a digit is 2⁵ = 32, what is the position of this digit from the right?
- 36 What is the binary equivalent of the decimal number 31?
- 37 What is the binary equivalent of the decimal number 64?
- 38 What is the decimal equivalent of the hexadecimal number 1F?
- 39 What is the decimal equivalent of the octal number 10?
- 40 What is the decimal equivalent of the binary number 1000000?
- 41 What is the most reliable way to verify the outcome of a conversion?
Source: Inspired by open curricula of Turkish universities; the explanation is written originally.
Kaynak: Türkiye üniversitelerinin açık ders planlarından esinlenilmiştir; anlatım özgün olarak yazılmıştır.