# Non-decimal Numbers

We are used to decimal numbers in our daily life. However, digital computers are based entirely on electrical currents being "on" or "off". If there is a voltage in a certain circuit, then it is considered "on", and if not, it is considered "off". While there are variations in the way this is implemented in computer hardware, the point is that everything in computing is based on the notion that there are only two basic states for a system. For more complicated systems, computers combine several of these on/off systems, hence all digital computers are based on numbers that are best expressed in powers of two. The result is that there are many instances in which humans refer to computer operations directly in base two, eight, or sixteen, without converting to decimal notation. The most basic of these uses binary notation.

Before we begin, we should make note of some nomenclature. In computing it is common to talk about "bits". By this, we mean a single one of the on/off systems mentioned above. Thus, a bit may be considered to be any logical object that can take on values of zero or one, and no others.

Recall that in binary (base two) representations for numbers
we have only two possible
single-digit numbers: 0 and 1. We use the same place value for binary number
that we do in the decimal case. Thus the representation for two already is a
two-digit number in binary: 10 = 1x2 + 0x1. Similarly, we can see that a three
digit binary number ABC= Ax2^{2} + Bx2^{1} + Cx2^{0}.
More particularly, we could count
from zero to eight in binary as follows: after 0 we have

- 1
- 10
- 11
- 100
- 101
- 110
- 111
- 1000

Thus we see that we can represent sixteen numbers with four binary digits (sometimes
called *bits*), thirty-two with 5 digits, sixty-four with 6 digits, one
hundred twenty-eight with seven, and two hundred fifty-six number using 8 binary
digits. You can see that the largest block of memory or storage addressable
using sixteen bits is 65536.

If we look carefully at this, we can see it as an example of the general rule
for representations of number in terms of the integer base b. We could write
the number n in the base b using the notation n_{b}. Note that

- 0
_{b}= 0 x b^{0} - 1
_{b}= 1 x b^{0} - ...
- (b-1)
_{b}= (b-1) x b^{0} - 10
_{b}= 1 x b^{1}+ 0 x b^{0} - 11
_{b}= 1 x b^{1}+ 1 x b^{0} - ...
- 101
_{b}= 1 x b^{2}+ 0 x b^{1}+ 1 x b^{0}

And so on.

Such representations are useful in many situations. For example, some situations in which the natural division of numbers is into collections of three bits call for octal notation, that is, base eight. It is so common to use hexadecimal notation that it deserves special emphasis. Hexadecimal notation is a base sixteen representation for numbers. Thus, two hexadecimal digits suffice to represent 256 numbers, or eight bits worth of information. Since we do not have enough arabic numerals to make sixteen hexadecimal digits, we use letters instead. Thus in "hex" one counts from zero to sixteen as

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10.

It follows that 30 hex is 48 decimal, A0 hex is 160, and FF is 255. The beauty of using a base that is a power of two is that conversion to and from binary becomes very easy. For example, each hex digit can be represented by four binary digits. When converting hex to binary, all we have to do is convert one digit at a time. For example

F0_{16} = 1111 0000_{2}

A8E_{16} = 1010 1000 1110_{2}

1B8C_{16} = 0001 1011 1000 1100_{2}.

Each hex digit can simply be replaced in the conversion by its binary representation - the place holding takes care of the rest. Trust me: it is not so easy in base 7.

This also allows us to regroup digits to convert between e.g. hex and octal.

F0_{16} = 1111 0000_{2} = 11 110 000_{2} = 360_{8}

A8E_{16} = 1010 1000 1110_{2} = 101 010 001 110_{2} = 5216_{8}

We shall revisit this subject when we discuss HTML color specifications, ethernet addresses, file permission masks, and other topics.

**ICE (In-Class Exercise)**

- What is the decimal representation for the following hexadecimal numbers?
- F0
- 9F
- 8
- 10
- 101
- What is the decimal representation for the following binary numbers?
- 1
- 10
- 101
- 1010
- What is the octal representation for the following binary numbers?
- 1
- 10
- 1010
- 10111
- 100100
- What is the binary representation for the following octal numbers?
- 1
- 10
- 42
- 755

- What is the hexadecimal representation for the following decimal numbers?
- 8
- 15
- 16
- 128
- 170
- 255

The last test will take place at the final exam time on
Tuesday, 12 December, from 1:30-3:30. It will be written as a one-hour (not
50 minute) exam, but you may have the full two hours for it.
In other respects it will be very like the other tests, but
comprehensive - it will emphasize Python, but cover all the
topics we have seen.
There is a
Sample Exam, but be aware that things will have changed somewhat
with the advent of ChatGPT.

The scores are posted on the Info tab at
My.math.
Notify the instructor of discrepancies immediately.