Set 12: Number Systems

Skill 12.1 Compare the decimal, binary, hexadecimal, and octal number systems
Skill 12.1 Concepts

In this lesson we will examine four different number systems: decimal, binary, hexadecimal, and octal. Each is explained below,

Base 10

The base 10 system is based on groups of 10.

There are 10 possible digits in this number system: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Base Position Baseposition Value Log10
10 0 100 1 0
10 1 101 10 1
10 2 102 100 2
10 3 103 1000 3
10 4 104 10000 4
10 5 105 100000 5
10 6 106 1000000 6
10 7 107 10000000 7
10 8 108 100000000 8
10 9 109 1000000000 9

Consider the decimal number 5,402. The position of each digit is shown below,

Digit 5 4 0 2
position 3 2 1 0

The numerical value for each positional value can be determined by mulitiplying the digit in each position by its base raised to the position. This concept is illustrated below.

Digit Position Digit x BasePosition Positional Value
2 0 2 x 100 2
0 1 0 x 101 0
4 2 4 x 102 400
5 3 5 x 103 5000

Adding the positional values returns the original number,

2 + 0 + 400 + 5000 = 5402

Binary

In the binary number system, there are only two possible values: 0 and 1.

Base Position Baseposition Value Log2
2 0 20 1 0
2 1 21 2 1
2 2 22 4 2
2 3 23 8 3
2 4 24 16 4
2 5 25 32 5
2 6 26 64 6
2 7 27 128 7
2 8 28 256 8
2 9 29 512 9

Consider the conversion of a binary number 1101 to decimal form. The position of each digit is shown below

Digit 1 1 0 1
position 3 2 1 0

Multiplying the digit in each position by the base raised to the position (Baseposition) the positional value can be determined for each digit.

Digit Position Digit x BasePosition Positional Value
1 0 1 x 20 1
0 1 0 x 21 0
1 2 1 x 22 4
1 3 1 x 23 8

Just as before, adding the positional values returns the value of the number in base 10,

1 + 0 + 4 + 8 = 13

Hexadecimal (Base 16)

The hexadecimal system is based on groups of 16.

There are 16 possible digits in this number system: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

NOTE that A = 10, B = 11, C = 12, D = 13, E = 14, F = 15

Base Position Baseposition Value Log16
16 0 160 1 0
16 1 161 16 1
16 2 162 256 2
16 3 163 4096 3
16 4 164 65536 4

Consider the conversion of a hexadecimal number 5C02 to decimal form. The position of each digit is shown below,

Digit 5 C 0 2
position 3 2 1 0

Multiplying the digit in each position by the base raised to the position (Baseposition) the positional value can be determined for each digit.

Digit Position Digit x BasePosition Positional Value
2 0 2 x 160 2
0 1 0 x 161 0
12 2 12 x 162 3072
5 3 5 x 163 20480

Just as before, adding the positional values returns the value of the number in base 10,

2 + 0 + 3072 + 20480 = 23554

Octal (Base 8)

The octal system is based on groups of 8

There are 16 possible digits in this number system: 0, 1, 2, 3, 4, 5, 6, 7

Base Position Baseposition Value Log8
8 0 80 1 0
8 1 81 8 1
8 2 82 64 2
8 3 83 512 3
8 4 84 4096 4

Consider the conversion of the octal number 5402 to decimal form. The position of each digit is shown below,

Digit 5 4 0 2
position 3 2 1 0

Multiplying the digit in each position by the base raised to the position (Baseposition) the positional value can be determined for each digit.

Digit Position Digit x BasePosition Positional Value
2 0 2 x 90 2
0 1 0 x 81 0
4 2 4 x 82 256
5 3 5 x 83 2560

Just as before, adding the positional values returns the value of the number in base 10,

2 + 0 + 256 + 2560 = 2818

Skill 12.1: Exercise 1
Skill 12.2 Convert a decimal number to binary, hexadecimal, or octal
Skill 12.2 Concepts

Recall that to obtain the last digit of a decimal number we can apply the modulus operator

int num = 1234;
int lastNumber = num%10;//returns 4

To return the second to last digit of a decimal number we first divide by 10, then apply the modulus operation

int num = 1234;
num = num/10;
int lastNumber = num%10;//returns 3

If we continue this process until no more digits are available, we can obtain each digit in the number. This idea can be applied to other number systems as well.

Binary

The below example illustrates how to convert a decimal number into a binary number. Consider the decimal number 147.

Base divisor Number divided Modulus base
2 147 1
2 73 1
2 36 0
2 18 0
2 9 1
2 4 0
2 2 0
2 1 1

Listing the numbers from bottom to to yields, 10010011 = 147

Hexadecimal

The below example illustrates how to convert a decimal number into a hexadecimal number. Consider the decimal number 3741.

Base divisor Number divided Modulus base
16 3741 13
16 233 9
16 14 14

Now list the numbers from bottom to top. Notice, when listing "14" we give its hex equivalent, E, and for "13" we give the hex equivalent, D. The resultant hex number is E9D = 3741.

Octal

The below example illustrates how to convert a decimal number into an octal number. Consider the octal number 251.

Base divisor Number divided Modulus base
8 251 3
8 31 7
8 3 3

Now list the numbers from bottom to top, the resultant octal number is 373 = 251.

Skill 12.2: Exercise 1
Skill 12.3 Determine the number of places to represent a number in decimal, binary, hexadecimal, or octal
Skill 12.3 Concepts

Recall that the base 10 system is based on groups of 10.

And, that there are 10 possible digits in this number system: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Base Position Baseposition Value Log10
10 0 100 1 0
10 1 101 10 1
10 2 102 100 2
10 3 103 1000 3
10 4 104 10000 4
10 5 105 100000 5
10 6 106 1000000 6
10 7 107 10000000 7
10 8 108 100000000 8
10 9 109 1000000000 9

From the above, it can be deduced that the number of postional values required to represent any decimal number can be determined as follows,

positions = log10(number)

int positions = (int)(Math.log10(number))+1;

Likewise, the positional value is,

positional value = value x 10position

int positionalValue = (int)(value*Math.pow(10, position));

This idea can be extended to other bases, base 2 for example,

positions = log2(number)+1

Java does not hava a built in log base 2 operation. However, the number of postions required to represent a number in any base can be determined as follows,

Consider the number 32, which is also equal to 25, so,

25 = 32

If we take the base 10 log of both sides,

Log10(25) = Log10(32)

5 Log10(2) = Log10(32)

rearranging,

5 = Log10(32)/Log10(2)

The above example illustrates that the number of positions required to represent the number 32 in base 2 is,

required positions = Log10(32)/Log10(2)+1

Generalizing this expression provides a way to obtain the number of positions required to represent a number in any base,

int positions = (int)((Math.log10(number))/(Math.log10(base)))+1;

Skill 12.3: Exercise 1