Computer Number Systems
All digital computers, from supercomputers to your smartphone, are electronic devices and ultimately can do one thing: detect whether an electrical signal is on or off. That basic information, called a bit (binary digit), has two values: a 1 (or true) when the signal is on, and a 0 (of false) when the signal is off. Larger values can be stored by a group of bits. [math][/math]
The number system is the system of naming or representing numbers. The number system we use in our daily life is known as Decimal number system because they are based on 10 different digits: 0, 1, 2, ..., 8, and 9. These numbers are called number with base-10. Base is generally written as subscript suffixed to a number. With base notation, the number "twelve thousand three hundred and forty-five" is written as $12345_{10}$. While we do not use the base-10 notation in our usual writing, we need to use it for other number systems. We will see the use of this base notation throughout this article to differentiate between numbers.
Different Number Systems
In computer science, apart from the decimal system, three additional number systems are used. They are binary, octal, and hexadecimal. Binary systems is for the number system with base 2. Octal stands for the number system with base 8 while hexadecimal stands for the number system with base 16.
As you might guess, binary deals only with 2 digits — 0 and 1. Octal has 8 digits and hexadecimal has 16 unique digits. The table below displays the Number Systems with Base, Digits Used, and Sample Representation:
Number System Base Digits Used Sample Representation Binary 8 0,1 $10110_{2}$ Octal 8 0,1,2,3,4,5,6,7 $75321_{8}$ Decimal 10 0,1,2,3,4,5,6,7,8,9 $97425_{10}$ or simply 97425 Hexadecimal 10 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F $54321_{16}$
The letters A, B, C, D, E, and F represent an unique digit each in hexadecimal system where A stands for 10, B for 11, C for 12, D for 13, E for 14 and F for 15.
Number of Bits to Hold All Digits
Since computer uses bits to store and process information, we need to understand the minimum requirement of bits for each number system.
As one bit can store either 0 or 1, it has a capacity of storing 2 values or $2^{1}$ values which is $2^{number\_of\_bits}$.
For 2 bits, the capacity increases to 4 different numbers which are 00, 01, 10, and 11. We can say 2 bits can store $2^{2}$ values. The capacity grows with the available bits in multiple of 2.
So to represent all Octal digits (8 digits), we need to allocate 3 bits as $2^3$ can accommodate 8 different values. And to assign all Hexadecimal digits (16 digits), we need to allocate 4 bits as $2^4$ = 16.
Need for Octal and Hexadecimal Numbers
While computer can stores the numbers in bits (binary format) only, what is the need for Octal and Hexadecimal number systems?
Because binary numbers could get very long even for small decimal numbers, they grouped 3 binary digits together to form one base 8 digit and called that octal and grouped 4 binary digits together to form other base 16 digit and called that hexadecimal. The following table shows equivalent values in these bases:
Three digits (0 for left-most) Four digits (needed for base 16) 0000 = 0 1000 = 8 0001 = 1 1001 = 9 0010 = 2 1010 = A (10) 0011 = 3 1011 = B (11) 0100 = 4 1100 = C (12) 0101 = 5 1101 = D (13) 0110 = 6 1110 = E (14) 0111 = 7 1111 = F (15)
As you can see, the largest value that can be represented with 3 binary digits (when the first digit is a 0) is 7 and the largest value with 4 bits is 15. However, because all digits must be single characters, the letters A – F are used to represent the values 10 to 15. Therefore, in base 8, the digits must be 0 to 7 and in base 16 the digits must be 0 – 9, A – F.
Formation of Numbers
The formation of any number goes by the digit multiplied by it's place value and adding them together. For example, we read and understand the number 12345 as twelve thousand three hundred and forty-five which has five digits: 1, 2, 3, 4, and 5. The number 12345 is completely different from the number 54321, which is also having the exact same 5 digits. This tells us that the digit with its place value give us the number. So, how to create/form a number in decimal system? Let's see using these 2 example numbers:
- $12345 = 1×10000 + 2×1000 + 3×100 + 4×10 + 5×1 = 1×10^{4}+ 2×10^{3}+ 3×10^{2}+ 4×10^{1}+ 5×10{0}.$
- $54321 = 5×10000 + 4×1000 + 3×100 + 2×10 + 1×1 = 5×10^{4}+ 4×10^{3}+ 3×10^{2}+ 2×10^{1}+ 1×10{0}.$
The expanded notation in the decimal number system above can represent different numbers by using powers of 10.
Since formation of a number follows the same rule in all number system, all other bases work the same way as follows:
- $1101_{2} = 1 × 2^3 + 1 × 2^2 + 0 × 2^1 + 1 × 2^0 = 8 + 4 + 0 + 1 = 13_{10}$
- $175_{8} = 1 × 8^2 + 7 × 8^1 + 5 × 8^0 = 1 × 64 + 7 × 8 + 5 × 1 = 64 + 56 + 5 = 125_{10}$.
- $A5E_{16} = 10 × 16^2 + 5 × 16^1 + 14 × 16^0 = 10 × 256 + 5 × 16 + 14 × 1 = 2560 + 80 + 14 = 2654_{10}$.
Grouping Binary Digits for Octal and Hexadecimal Numbers
An easy way to convert between bases 2, 8, and 16 is by grouping bits together and using the table above.
- $1001010110_{2} = \ \ 001\ \ \ 001\ \ \ 010\ \ \ 110_{2} = 1126_{8}$ and
- $1001010110_{2} = 0010\ 0101\ 0110_{2} \ \ \ \ \ \ \ \ \ = 256_{16}$
Notice that grouping is always done starting with the last digit.
Using Hexadecimal Numbers to Represent Colors
Computers use hexadecimal numbers to represent various colors in computer graphics because all computer screens use combinations of red, green, and blue light or RGB to represent thousands of different colors. Two digits are used for each so the hexadecimal number “#FF0000” represents the color red, “#00FF00” represents green, and “#0000FF” represents blue. The color black is “#000000” and white is “#FFFFFF”.
The hash tag or number sign is used to denote a hexadecimal number. $FF_{16} = F (15) × 16 + F (15) × 1 = 240 + 15 = 255_{10}$ so there are 0 to 255 or 256 different shades of each color or $256^{3} = 16,777,216$ different colors.
The following web site has nearly every color name, along with its hex code and decimal values:
For example “salmon” is “#FA8072” which represents the decimal numbers 250 (hex FA), 128 (hex 80), and 114 (hex 72).
Resources
Ryan's Tutorials covers this topic beautifully. Rather than trying to duplicate that work, we'll point you to the different sections:
- 1. Number Systems - An introduction to what numbers systems are all about, with emphasis on decimal, binary, octal, and hexadecimal. ACSL will typically identify the base of a number using a subscript. For example, $123_8$ is an octal number, whereas $123_{16}$ is a hexadecimal number.
- 2. Binary Conversions - This section shows how to convert between binary, decimal, hexadecimal and octal numbers. In the Activities section, you can practice converting numbers.
- 3. Binary Arithmetic - Describes how to perform various arithmetic operations (addition, subtraction, multiplication, and division) with binary numbers. ACSL problems will also cover basic arithmetic in other bases, such as adding and subtracting together 2 hexadecimal numbers. ACSL problems will not cover division in other bases.
- 4. Negative Numbers - ACSL problems will not cover how negative numbers are represented in binary.
- 5. Binary Fractions and Floating Point - The first part of this section is relevant to ACSL: fractions in other bases. ACSL will not cover floating point numbers in other basis. So, focus on the section Converting to a Binary Fraction, but keep in mind that ACSL problems may also cover octal and hexadecimal fractions.
The CoolConversion.com online calculator is another online app for practicing conversion from/to decimal, hexadecimal, octal and binary; this tool shows the steps that one goes through in the conversion.
Format of ACSL Problems
The problems in this category will focus on converting between binary, octal, decimal, and hexadecimal, basic arithmetic of numbers in those bases, and, occasionally, fractions in those bases.
To be successful in this category, you must know the following facts cold:
- The decimal value of each hex digit A, B, C, D, E, F
- The binary value of each hex digit A, B, C, D, E, F
- Powers of 2, up to 4096
- Powers of 8, up to 4096
- Powers of 16, up to 65,536
Sample Problems
Sample Problem 1
Solve for $x$ where $x_{16}=3676_8$.
Solution: One method of solution is to convert $3676_8$ into base 10, and then convert that number into base 16 to yield the value of $x$.
An easier solution, less prone to arithmetic mistakes, is to convert from octal (base 8) to hexadecimal (base 16) through the binary (base 2) representation of the number:
$$\begin{align} 3676_8 &= 011 ~ 110 ~ 111 ~ 110_2 & \text{convert each octal digit into base 2}\hfill\cr &= 0111 ~ 1011 ~ 1110_2 & \text{group by 4 bits, from right-to-left}\hfill\cr &= 7 ~ \text{B} ~ \text{E}_{16} & \text{convert each group of 4 bits into a hex digit}\cr \end{align}$$
Sample Problem 2
Solve for $x$ in the following hexadecimal equation: $ x= \text{F5AD}_{16} - \text{69EB}_{16}$
Solution: One could convert the hex numbers into base 10, perform the subtraction, and then convert the answer back to base 16. However, working directly in base 16 isn't too hard. As in conventional decimal arithmetic, one works from right-to-left, from the least significant digits to the most.
- The rightmost digit becomes 2, because D-B=2.
- The next column is A-E. We need to borrow a one from the 5 column, and 1A-E=C
- In the next column, 4-9=B, again, borrowing a 1 from the next column.
- Finally, the leftmost column, E-6=8
Combining these results of each column, we get a final answer of $8BC2_{16}$.
Sample Problem 3
How many numbers from 100 to 200 in base 10 consist of distinct ascending digits and also have distinct ascending hex digits when converted to base 16?
Solution: There are 13 numbers that have ascending digits in both bases from 100 to 200. They are (in base 10): 123 (7B), 124, 125, 126, 127 (7F), 137 (89), 138, 139 (8B), 156 (9C), 157, 158, 159 (9F), 189 (BD)
Video Resources
There are many YouTube videos about computer number systems. Here are a handful that cover the topic nicely, without too many ads:
Number Systems - Converting Decimal, Binary and Hexadecimal (Joe James)
An introduction to number systems, and how to convert between decimal, binary and hexadecimal numbers. | |
Lesson 2.3 : Hexadecimal Tutorial (Carl Herold)
The video focuses on hexadecimal numbers: their relationship to binary numbers and how to convert to decimal numbers. | |
Hexes and the Magic of Base 16 - Vaidehi Joshi - May 2017' (DonutJS)
A fun introduction to hexadecimal numbers, focusing a bit on using hex numbers for specifying RGB colors. | |
Collins Lab: Binary & Hex (Adafruit Industries)
A professionally produced video that explains the number systems, how and why binary numbers are fundamental to computer science, and why hexadecimal is important to computer programmers. |