Numeral Systems

Intro to Programming Logic COMP-1029

Red River College

Introduction

Dechexbin

Numeral Systems

  • a.k.a. System of Numeration

  • A system of writing (a set of symbols – a mathematical notation) for representing numbers

  • We will look at the following numeral systems:

    • Decimal (Base-10)
    • Binary (Base-2)
    • Octal (Base-8)
    • Hexadecimal (Base-16)

Objectives

By the end of this unit, you should be able to:

Numeral Systems

Arabic_numerals-en
  • A numeral system represents numbers using symbols known as numerals.

  • The numerals you are accustomed to using are known as hindu-arabic, hindu, or arabic numerals: (0,1,2,3,4,5,6,7,8,9)

  • All numeral systems have what is called a base or a radix.

    • The base tells us how many numerals may be used to represent numbers in the system.
  • The numeral system you are most used to is the decimal (base-10) numeral system.

    • e.g. The decimal number thirty-seven is represented by 37, which is made up of two numerals: 3 and 7

    • Base ten numbers can also be represented with other numerals, Roman numerals for example.

Decimal (Base-10)

Europeanformofarabiandigits

The decimal numeral system is the system humans use for counting and commerce. Counting in groups of ten makes sense to us as we have ten fingers (digits). Decimal is therefore a base-10 system, using the numerals (0,1,2,3,4,5,6,7,8,9).

Each of these Hindu-Arabic numerals has a face value. By this I mean that each symbol has an intrinsic, built-in number value, from zero to nine. The symbol ‘1’ represents the concept of one, the symbol ‘2’ represents the concept of two, etc.

But what happens when we need to count over nine?

Place Value

The decimal number that follows 9 is 10, and similarly 99 is followed by 100.

Notice that we are now using groups of numerals, written from left to right, to represent numbers.

The position of these numerals determines their worth beyond their face-value. In positional numeral systems like decimal this positional worth is called a place-value.

The place-value is the numerals face-value multiplied by the numerals place-value-weight.

Decimal Place Value

Decimal_place_value

Let’s expand the decimal number 34,125:

ten-thousands: 3 * 10<sup>4</sup> = 3 * 10,000 = 30,000
thousands:     4 * 10<sup>3</sup> = 4 * 1,000 = 4,000
hundreds:      1 * 10<sup>2</sup> = 1 * 100 = 100
tens:          2 * 10<sup>1</sup> = 2 * 10 = 20
ones:          5 * 10<sup>0</sup> = 5 * 1 = 5

Place Value Table

An easy way to expand a number is to use a Place Value Table. As an example, we will use the decimal number 34,125:

Numeral Position   |  5   |  4   |  3   |  2   |  1   |  0   |
                   -------------------------------------------
Place-Value-Weight | 10^5 | 10^4 | 10^3 | 10^2 | 10^1 | 10^0 |
                   -------------------------------------------
Decimal Number     |      |  3   |  4   |  1   |  2   |  5   |

To expand the number, we work through the columns from left to right, multiplying the face-value of each numeral with its place-value-weight. We then add all those products together.

34,125 = (3 * 104) + (4 * 103) + (1 * 102) + (2 * 101) + (5 * 100)

34,125 = (3 * 10,000) + (4 * 1,000) + (1 * 100) + (2 * 10) + (5 * 1)

34,125 = 30,000 + 4,000 + 100 + 20 + 5

Obvious, right? But what about numeral systems with other bases?

Note: The ^ symbol used above means “to the power of”. So, for example, the 10^5 = 105.

Binary (Base-2)

Binary_green
  • The binary numeral system is the base-2 system; it uses only two symbols: 0 and 1 (in Hindu-Arabic numerals)

    • A single binary digit is known as a bit
    • Four bits is called a nibble, i.e. 1010
    • Eight bits is an octet, i.e. 11001010
    • Most often, a byte is also eight bits.
  • Binary is the numeral system of modern computers

  • Computers use binary because it is easily implemented in digital electronic circuitry

    • In memory, voltage is used to represent the two values (i.e. +5 V and almost 0 V)
    • Magnetic media (i.e. hard disk drives) represents the two values using two polarity states (positive and negative)
    • On optical media (i.e. CD-ROMs) they are stored as minuscule bumps on the aluminum layer of the disk

Binary Conversion

To convert between binary and decimal we will return to our place value table. This time the place-value-weights are all factors of 2.

Numeral Position   |  5   |  4   |  3   |  2   |  1   |  0   |
                   -------------------------------------------
Place-Value-Weight |  2^5 |  2^4 |  2^3 |  2^2 |  2^1 |  2^0 |
                   -------------------------------------------
Place-Value-Weight |   32 |   16 |    8 |    4 |    2 |    1 |

The place-value-weights double as move column to column from right to left.

Note: The bottom two rows of the table represent the same information in different ways.

Resources

Binary Conversion

To use the table for conversion purposes we add another row to hold the numerals of our binary number.

Numeral Position   |  5   |  4   |  3   |  2   |  1   |  0   |
                   -------------------------------------------
Place-Value-Weight |  2^5 |  2^4 |  2^3 |  2^2 |  2^1 |  2^0 |
                   -------------------------------------------
Place-Value-Weight |   32 |   16 |    8 |    4 |    2 |    1 |
                   -------------------------------------------
Binary Number      |      |      |      |      |      |      |

Let’s use this table to convert the binary number 101101 into decimal:

Numeral Position   |    5 |    4 |  3   |  2   |    1 |    0 |
                   -------------------------------------------
Place-Value-Weight |  2^5 |  2^4 |  2^3 |  2^2 |  2^1 |  2^0 |
                   -------------------------------------------
Place-Value-Weight |   32 |   16 |    8 |    4 |    2 |    1 |
                   -------------------------------------------
Binary Number      |    1 |    0 |    1 |    1 |    0 |    1 |

Now we work our way left to right (just like we did when we expanded our decimal number) multiplying the face-values of our binary numerals with their place-value-weights. Finally, we sum all those products.

  • 101101 [binary] = (32 x 1) + (16 x 0) + (8 x 1) + (4 x 1) + (2 x 0) + (1 x 1) [decimal]
  • 101101 [binary] = (32) + (0) + (8) + (4) + (0) + (1) [decimal]
  • 101101 [binary] = 45 [decimal]

Note: If our binary number contained more numerals we would have to add extra columns to the left-side of our chart. Can you figure out the place-value-weights of the next three columns to the left?

Reverse Binary Conversion

We can also use the place value weights from the Place Value Table to convert from decimal to binary.

Let’s try the decimal number 54.

We start with the left most column and ask if we can use each weight to build the number 54. Each time we say yes, we’ll subtract that weight from our number.

QuestionAnswerReason
Do we need a 32?yes!That leaves us with 54-32 = 22
Do we need a 16?yes!That leaves us with 22-16 = 6
Do we need an 8?no!We only have 6 remaining.
Do we need a 4?yes!That leaves us with 6-4 = 2
Do we need a 2?yes!That leaves us with 2-2 = 0
Do we need a 1?no!We have no more left.

So that’s (yes,yes,no,yes,yes,no) or if we replace yes by 1 and no by 0: 110110

Note: If we wanted to convert a decimal number larger that 63 we would need to add another column to the left of our table, with a place-weight-value of 64. The larger the number, the more columns required.

Octal (Base-8)

Oct
  • Octal is the base-8 numeral system
  • Eight digits are used: 0 through 7
  • Like hexadecimal, octal is a way of representing binary data (but using 3 bits per character instead of 4)
  • Octal is used in certain computing applications
  • One application is the Unix chmod command which is used to assign file permissions
  • Each place is a power with base 8
  • 4218 = (4 X 82) + (2 X 81) + (1 X 80) = 27310

Resources

Octal Conversion

To convert between decimal and octal we will again return to our Place Value Table. This time the place-value-weights are all factors of 8.

Numeral Position   |  5   |  4   |  3   |  2   |  1   |  0   |
                   -------------------------------------------
Place-Value-Weight |  8^5 |  8^4 |  8^3 |  8^2 |  8^1 |  8^0 |
                   -------------------------------------------
Place-Value-Weight |32768 | 4096 |  512 |   64 |    8 |    1 |
                   -------------------------------------------
Octal Number       |      |      |      |      |      |      |

Let’s use this table to convert the octal number 2376 into decimal:

Numeral Position   |  5   |  4   |  3   |  2   |  1   |  0   |
                   -------------------------------------------
Place-Value-Weight |  8^5 |  8^4 |  8^3 |  8^2 |  8^1 |  8^0 |
                   -------------------------------------------
Place-Value-Weight |32768 | 4096 |  512 |   64 |    8 |    1 |
                   -------------------------------------------
Octal Number       |      |      |    2 |    3 |    7 |    6 |

Now we work our way left to right multiplying the face-values of our octal numerals with their place-value-weights. Finally, we sum all those products.

  • 2376 [octal] = (512 x 2) + (64 x 3) + (8 x 7) + (1 x 6) [decimal]
  • 2376 [octal] = (1024) + (192) + (56) + (6) [decimal]
  • 2376 [octal] = 1278 [decimal]

Reverse Octal Conversion

We can also use the place value weights from the Place Value Table to convert from decimal to octal.

Let’s try the decimal number 2547.

We start with the left most column. For each column we ask how many of that weight can evenly fit into our number. We then subtract the remainder from our number.

QuestionAnswerReason
How many 32768s fit in 2547?noneThat still leaves us with 2547
How many 4096s fit in 2547?noneThat still leaves us with 2547
How many 512s fit in 2547?4That leaves us with 2547 - (512 x 4) = 499
How many 64s fit in 499?7That leaves us with 499 - (64 x 7) = 51
How many 8s fit in 516That leaves us with 51 - (8 x 6) = 3
How many 1s fit in 33That leaves us with 3 - (1 x 3) = 0

So that’s 4763 [octal]

Hexadecimal (Base-16)

Hexadecimal Conversion

To convert between decimal and hexadecimal we will again return to our Place Value Table. This time the place-value-weights are all factors of 16.

Numeral Position   |   5   |  4   |  3   |  2   |  1   |  0   |
                   --------------------------------------------
Place-Value-Weight |  16^5 | 16^4 | 16^3 | 16^2 | 16^1 | 16^0 |
                   --------------------------------------------
Place-Value-Weight |1048576|65536 | 4096 |  256 |   16 |    1 |
                   --------------------------------------------
Hexadecimal Number |       |      |      |      |      |      |

Let’s use this table to convert the hexadecimal number 3D2A into decimal:

Numeral Position   |   5   |  4   |  3   |  2   |  1   |  0   |
                   --------------------------------------------
Place-Value-Weight |  16^5 | 16^4 | 16^3 | 16^2 | 16^1 | 16^0 |
                   --------------------------------------------
Place-Value-Weight |1048576|65536 | 4096 |  256 |   16 |    1 |
                   --------------------------------------------
Hexadecimal Number |       |      |    3 |    D |    2 |    A |

Now we work our way left to right multiplying the face-values of our hexadecimal numerals with their place-value-weights. Finally, we sum all those products.

  • 3D2A [hex] = (4096 x 3) + (256 x D) + (16 x 2) + (1 x A) [decimal]
  • 3D2A [hex] = (4096 x 3) + (256 x 13) + (16 x 2) + (1 x 10) [decimal]
  • 3D2A [hex] = (12,288) + (3,328) + (32) + (10) [decimal]
  • 3D2A [hex] = 15,658 [decimal]

Resources

Reverse Hexadecimal Conversion

We can also use the place value weights from the Place Value Table to convert from decimal to hexadecimal.

Let’s try the decimal number 6082.

We start with the left most column. For each column we ask how many of that weight can evenly fit into our number. We then subtract the remainder from our number.

QuestionAnswerReason
How many 1048576s fit in 6082?noneThat still leaves us with 6082
How many 65536s fit in 6082?noneThat still leaves us with 6082
How many 4096s fit in 6082?1That leaves us with 6082 - (4096 x 1) = 1986
How many 256s fit in 1986?7That leaves us with 1986 - (256 x 7) = 194
How many 16s fit in 19412 (C)That leaves us with 194 - (16 x 12) = 2
How many 1s fit in 22That leaves us with 2 - (1 x 2) = 0

So that’s 17C2 [hexadecimal]

Conversion Among Bases

It’s simple to convert Octal or Hexadecimal to Binary (and vis-versa). Each Octal numeral represent groups of three binary bits. While each Hexadecimal numeral represents groups of four binary bits.

The Octal number 137 converted to binary:

The Hex numer E30A converted to binary:

Let’s work in reverse and convert the binary numeral 110011110101 into both octal and hexadecimal.

Octal:

  • Group the bits into groups of three from right to left: (110)(011)(110)(101)
  • Convert each group of bits into an octal numeral: (6)(3)(6)(5)
  • 110011110101 [binary] = 6365 [octal]

Hex:

  • Group the bits into groups of four from right to left: (1100)(1111)(0101)
  • Convert each group of bits into a hex numeral: (C)(F)(5)
  • 110011110101 [binary] = CF5 [hexadecimal]

Note: If the left-most group does contain enough bits you can pad it with zeros. For example:

  • 11011 [binary] = ? [hex]
  • (0001)(1011) [binary] = (1)(B) [hex]
  • 11011 [binary] = 1B [hex]

Resources

A Numeral Chart

DecimalBinaryOctalHexadecimal
0000
1111
21022
31133
410044
510155
611066
711177
81000108
91001119
10101012A
11101113B
12110014C
13110115D
14111016E
15111117F
16100002010

Binary Finger Dance

Behold, nerdy fingers:

Resources

Mechanical Binary Computer

Think back to the previous two modules (the history of computing and how computer’s work) and then enjoy this video of the Digi-Comp II, a billiard ball computer.

What's Next?

In the next module we will explore the basic building blocks of microprocessors, electronic logic gates.

Logic gates allow processors to perform the simple binary calculations that form the basis of all machine instructions.

We will also look at the mathematical Boolean logic that powers these gates.