Unsigned place value numbers require two idea:
A set of digits, each with an integer meaning between zero and . is called the base
of the place value number system.
We’ll use the digits 0 through 9, then switch to A though Z (either upper- or lower-case) for ten through thirty-five. For we don’t have a standard notation, though we’ll note one special notation for below.
A way of writing a sequence of digits from low-order
to high-order
. In English we use horizontal sequences with low-order on the right and high-order on the left.
If we need to refer to specific places we’ll use index 0 to refer to the low-order place, index 1 for the next place, and so on. When we need to reference a digit in symbolic form we’ll call it , where is the name of the number and is the index of the digit.
We’ll draw these like so:
The numeric meaning of an unsigned base- number is the sum of for all indices .
=
=
=
Finding a value in a given base cane be done by repeated division with remainder.
To convert 340 to base 3,
, the smallest power of which is greater than . Thus we’ll have 6 places:
Repeat for from 5 to 0, inclusive of both endpoints:
Thus is 110121 in base-3.
If then each base- digit maps to exactly base- digits.
Because , each digit of a base-16 number maps directly to 4 digits of a base-2 number.
We use this feature extensively in computing. Processors typically operate in base-2 (binary
), but we find base-2 hard to read (too many 0s and 1s and we get lost) so we write base-8 (octal
: ) or base-16 (hexadecimal
: instead. Memory, files, and network communication is typically defined in base-256 (bytes
) but we find base-256 hard to read (we only have 36 digits) so we write each byte in base-16 () using two characters for one digit.
This document is stored on a file and transmitted over a network in a byte-oriented format called HTML. The first eight bytes are 3C 21 44 4F 43 54 59 50.
I started writing this example 1705682415 seconds after the POSIX epoch. In base-256 that is
To add two numbers represented in the same base,
carry digitto 0
It is common to draw this entire process by putting the two numbers to add one atop the other, the carry digits above them both, and the result below them both.
To add base-10 numbers 340 and 173 we
We’d draw this like so:
To add base-2 numbers 100111 and 1011 we
We’d draw this like so:
There are several models of subtraction taught in schools, but the one that works best on computers is generally not one of them.
Define b’s complement of a digit to be . When working in base b we always use b’s complements and just call them the complement of the digit.
Complements in two example bases are shown below
Digit | two’s complement | ten’s complement |
---|---|---|
0 | 1 | 9 |
1 | 0 | 8 |
2 | 7 | |
3 | 6 | |
4 | 5 | |
5 | 4 | |
6 | 3 | |
7 | 2 | |
8 | 1 | |
9 | 0 |
With this definition, we can subtract by adding and the digit-by-digit complement of (including complimenting the extra-digit 0s) using an initial carry of instead of . We stop once we’ve used up all digits and the carry is 1.
To subtract base-10 numbers 340 and 124 we
If we’d not stop we still would get the same answer:
Signed place-value numbers in computers use a finite number of digits. They do addition and subtraction the same way they do for unsigned numbers, but they discard high-order digits that they can’t store.
In this model, is and is the complement of the digits of , plus 1.
The 6-digit base-10 value of −340 is 999659 + 1 = 999660.
The 8-digit base-2 value of −101101 is 11010010 + 1 = 11010011.
When using signed numbers like this, we also need to distinguish between positive and negative numbers. For addition, subtraction, and multiplication it doesn’t matter if a number if positive or negative, but for comparisons and some division algorithms it does. The most common approach is to make half of all numbers negative, meaning in particular that a number is negative if the highest-order digit is or larger.
For base-2 (binary) numbers, the most common in computing, this means a signed integer is negative if the highest-order bit is 1, and is non-negative if that bit is 0.
In C and C++, the signed char
and unsigned char
types are 8-digit base-2 numbers.1 Annoyingly, char
might be shorthand for signed char
on one machine and unsigned char
on another. If you are using char
as a number type, always include an explicit signedness indicator.
Let’s consider a few numbers’ base-2 representation:
base 10 | 8-digit base 2 |
---|---|
124 | |
225 | |
340 |
To make these into 8-digit numbers, empty boxes will be receive the digit 0 and digits that don’t have a box will be discarded. Thus, the following code stores the values indicated in the comments
signed char s124 = 124; // +124
unsigned char u124 = 124; // +124
signed char s225 = 225; // -31
unsigned char u225 = 225; // +225
signed char s340 = 340; // +84
unsigned char u340 = 340; // +84