Skip to content

Numeral Systems

In computers, besides binary, octal and hexadecimal are also commonly used.

Binary

Binary is the base system used in internal computer operations. In this system, there are only two digits: \(0\) and \(1\). All internal computer operations (including bitwise operations) are performed based on binary.

However, representing numbers in binary can make them very long. Therefore, for convenience of representation, binary numbers are often converted to octal or hexadecimal.

Octal

In octal, there are eight digits: \(0,1,2,3,4,5,6,7\).

Typically, octal numbers are represented in the form oxx (where o is the prefix for octal, and xx represents the octal number).

Hexadecimal

In hexadecimal, there are sixteen digits: \(0,1,2,3,4,5,6,7,8,9,A(10),B(11),C(12),D(13),E(14),F(15)\).

Compared to binary, the biggest advantage of hexadecimal is that it represents numbers with shorter lengths. One hexadecimal digit can represent 4 binary digits.

Typically, hexadecimal numbers are represented in the form 0xdbf (where 0x is the prefix for hexadecimal numbers).

Conversion Between Bases

Decimal to Binary/Octal/Hexadecimal

Here, binary is used as an example for demonstration. The principles for other bases are similar.

For the integer part, repeatedly divide the decimal number by 2 until the quotient is 0. Then, from bottom to top, take all the remainders as the integer part of the binary number. For the fractional part, multiply it by 2, take the integer part of the result, and then repeat the calculation with the resulting fractional part until the fractional part becomes 0. Then, from top to bottom, take all the integer parts obtained from the calculations as the fractional part of the binary number.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Convert 35.25 to binary
Integer part:
35/2=17  ......1
17/2=8   ......1
8/2=4    ......0
4/2=2    ......0
2/2=1    ......0
1/2=0    ......1
Fractional part:
0.25*2=0.5     0
0.5*2=1        1

Thus, \(35.25 = (100011.01)_2\)

Binary/Octal/Hexadecimal to Decimal

Again, binary is used as an example.

To convert a binary number to decimal, simply multiply the value of each digit by \(2^i\), where \(i\) is the position of the digit, starting from 0 for the units place.

1
2
3
Convert 11010.01(2) to decimal
11010.01(2)=1*2^4+1*2^3+0*2^2+1*2^1+0*2^0+0*2^(-1)+1*2(-2)
        =26.25

Thus, \((11010.01)_2 = (26.25)_{10}\)

Conversion Between Binary/Octal/Hexadecimal

One octal digit can be represented by 3 binary digits (since \(2^3 = 8\)), and one hexadecimal digit can be represented by 4 binary digits (since \(2^4 = 16\)), and vice versa.