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= Ax22 + Bx21 + Cx20. More particularly, we could count from zero to eight in binary as follows: after 0 we have
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 nb. Note that
- 0b = 0 x b0
- 1b = 1 x b0
- (b-1)b = (b-1) x b0
- 10b = 1 x b1 + 0 x b0
- 11b = 1 x b1 + 1 x b0
- 101b = 1 x b2 + 0 x b1 + 1 x b0
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
F016 = 1111 00002
A8E16 = 1010 1000 11102
1B8C16 = 0001 1011 1000 11002.
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.
F016 = 1111 00002 = 11 110 0002 = 3608
A8E16 = 1010 1000 11102 = 101 010 001 1102 = 52168
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?
- What is the decimal representation for the following binary numbers?
- What is the octal representation for the following binary numbers?
- What is the binary representation for the following octal numbers?
- What is the hexadecimal representation for the following decimal numbers?
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.