Base

*b*representation of numbersEuclidean division algorithm

Base *b* representation is a way to write numbers using the digits {0, 1, …, (*b* − 1)}.

Common bases:

you use base 10 (

**decimal**) every day (digits are {0, 1, . . . , 9})base 2 (

**binary**) uses digits {0, 1}. It is convenient for digital logic, a digit (called a**bit**) can be represented using a single wire: the wire has high voltage for 1, low for 0. Binary numbers are often designated by a trailing b: for example 1101b.base 16 (

**hexadecimal**) uses the digits {0, 1, 2, . . . , 9,*A*,*B*,*C*,*D*,*E*,*F*}. it is useful becausee a single digit can be represented using 4 bits. Hex numbers are often written with a prefix of "0x": for example 0xFC39.base 8 (

**octal**) uses the digits {0, 1, 2, . . . , 7}, and is occasionally used when 3-bit numbers are useful.

A string of digits in base *b*, written (*a*_{n}*a*_{n − 1}. . . *a*_{3}*a*_{2}*a*_{1}*a*_{0})_{b}, represents the number *a*_{0}*b*^{0} + *a*_{1}*b*^{1} + *a*_{2}*b*^{2} + ⋯ + *a*_{n}*b*^{n}.

There are two ways to work with numbers in base b. The hard way: convert to base 10, apply the algorithms you already know for addition and multiplication, convert back.

The easy way: use the algorithms you already know for addition, multiplication, division, but remember that (10)_{b} stands for *b* and not 10.

We did examples with long addition. Long multiplication and division work the same way.

Euclidean division algorithm is actually a theorem. There is a close connection between proofs and algorithms, and you can extract an algorithm from the proof.

**Theorem (Euclidean division algorithm)**: For any numbers *a* and *b* ≠ 0, there exist unique numbers *q* and *r* satisfying

*a*=*q**b*+*r*- 0 ≤
*r*<*b*

**Proof (existence)**: To simplify, we'll only prove for *a* > 0 and *b* > 1. The cases for *a* = 0, *a* < 0, *b* = 1 and *b* < 0 are all straightforward extensions. I encourage you to check them on your own.

We'll prove the claim by induction on *a*. In the base case, when *a* = 1, we can simply choose *q* = 0 and *r* = 1. The two properties are immediately verified.

In the inductive case, assume that there exists *q*ʹ and *r*ʹ satisfying *a* = *q*ʹ*b* + *r*ʹ and 0 ≤ *r*ʹ < *b*. We wish to show that there are numbers *q* and *r* satisfying *a* + 1 = *q**b* + *r*.

We consider two cases separately:

*r*ʹ <*b*− 1. In this case, let*q*=*q*ʹ and let*r*=*r*ʹ + 1. It is easy to see that*a*+ 1 =*q*ʹ*b*+*r*ʹ + 1 =*q**b*+*r*

and since 0 ≤*r*ʹ <*b*− 1, we see 0 <*r*<*b*.*r*ʹ =*b*− 1. In this case, choose*q*=*q*ʹ + 1 and*r*= 0. Then*a*+ 1 =*q*ʹ*b*+*r*ʹ + 1 = (*q*− 1)*b*+ (*r*ʹ + 1) = (*q*− 1)*b*+*b*+ 0 =*q**b*+*r*

and clearly 0 ≤*r*<*b*.

**Proof (uniqueness)**: suppose there are two different pairs *q*, *r* and *q*ʹ, *r*ʹ satisfying properties 1 and 2. Then we have *a* = *q**b* + *r* = *q*ʹ*b* + *r*ʹ

so (*q* − *q*ʹ)*b* = *r*ʹ − *r*.

The possible values of the left hand side are . . . , − 2*b*, − *b*, 0, *b*, 2*b*, . . . , so the left hand side is either 0 or ≥ *b* or ≤ − *b*.

Recall that 0 ≤ *r* < *b* and 0 ≤ *r*ʹ < *b*. The biggest the right hand side can be is if *r*ʹ = *b* − 1 and *r* = 0, in this case the right hand side is *b* − 1. Similarly, smallest it can be is − (*b* − 1).

Thus the only way the left and right hand sides can be equal is if they are both 0. Thus *r* − *r*ʹ = 0 and *q*ʹ − *q* = 0, so *r* = *r*ʹ and *q* = *q*ʹ, as required.