Radices

The following are notes on radices.

Numerals

Recall that the division theorem provides the following:

division theorem. Let n,dZ{n,d \in \uint} with d>0.{d \gt 0.} Then there exist unique integers q{q} and r{r} such that n=dq+r{n = dq + r} with 0r<d.{0 \le r \lt d.}

theorem. Let b{b} be an integer greater than 1. Any nZ+{n \in \pint} can be expressed uniquely in the form

n=akbk+ak1bk1++a1b+a, n = a_kb^k + a_{k-1}b^{k-1} + \ldots + a_1b + a_\nil,

where kN{k \in \nat} and a0,a1,,ak{a_0, a_1, \ldots, a_k} are natural numbers less than b{b} and ak0.{a_k \neq 0.}

example. (1101)2=1(23)+1(22)+0(21)+1(20)=8+4+0+1=13.{(1101)_2 = 1(2^3) + 1(2^2) + 0(2^1) + 1(2^0) = 8 + 4 + 0 + 1 = 13.}

In the example above, the base is 2. We say that 2 is the radix of the binary system.

Base Conversion Algorithm

Below is an algorithm for converting to different bases.

base-conversion

  • Input:
    • nZ{n \in \uint}, the number to convert.
    • b>1{b \gt 1}, the base.
  • Output: A,{A,} an array of bases.
  1. qn{\let{q}{n}}
  2. i0{\let{i}{0}}
  3. A[ ]{\let{A}{\ix{~}}}
  4. while q0{q \neq 0} do
    1. aiq rem b{\let{a_i}{q} \rem b}
    2. Aai{A \Lleftarrow a_i}
    3. qq  b{\let{q}{q} \dv b}
    4. k++{k \inc}
  5. return A{A}

The algorithm above gives us a quick way to convert numbers to binary in our heads. For example, suppose we're given a decimal d.{d.} To convert to binary d2,{d_2,} we start by writing 1{1} if d{d} is odd, and 0{0} if d{d} is even. Then, compute the integer division of d.{d.} That is, d/2.{\floor{d/2}.} If the result is even, write 0.{0.} If the result is odd, write 1.{1.} We continue this process until we get to 1.{1.} For example, consider converting the integer 37:{37:}

d{d}remainder
37{37}1{1}
37/2=18{\floor{37/2}=18}0{0}
18/2=9{\floor{18/2}=9}1{1}
9/2=4{\floor{9/2}=4}0{0}
4/2=2{\floor{4/2}=2}0{0}
2/2=1{\floor{2/2}=1}1{1}

Mentally, we might list: 101001.{101001.} Simply reverse this sequence (binary digits are read from right to left): 100101.{100101.} There's nothing magical here. All we're doing is computing the remainder, but in a cognitively easier way (for most people, determining if a number is odd or even is faster than thinking about remainders). The same goes for converting numbers in other bases. Simply determine if the integer quotient at each step is a multiple of the base.

Binary Arithmetic

It's helpful to know the basic powers of two for this section:

powerdecimal
20{2^0}1
21{2^1}2
22{2^2}4
23{2^3}8
24{2^4}16
25{2^5}32
26{2^6}64
27{2^7}64
28{2^8}128
29{2^9}512
210{2^{10}}1024
211{2^{11}}2048
212{2^{12}}4096

Below are the definitions of bitwise operations.

bitwise operations. Let (n)2=(an1,an1,,a2,a1,a0){(n)_2 = (a_{n-1},a_{n-1},\ldots,a_2,a_1,a_0)} and (m)2=(bn1,bn1,,b2,b1,b0){(m)_2 = (b_{n-1},b_{n-1},\ldots,b_2,b_1,b_0)} be binary integers, such that each ai{a_i} and bi{b_i} is a bit (1 or 0 but not both), for all 0in,{0 \le i \le n,} where i,nN.{i,n \in \nat.} The following definitions apply for each bit a{a} and b.{b.}

~a=1anot aa | b=max(a,b)a or aa & b=min(a,b)a and ba^b=aba xor b \eqs{ \bnot a &= 1 - a \equiv \df{not}~ a \\ a \bor b &= \max{(a,b)} \equiv a ~\df{or} ~ a \\ a \band b &= \min{(a,b)} \equiv a ~\df{and} ~ b \\ a \bxor b &= \abs{a-b} \equiv a ~\df{xor} ~ b \\ }

Given A,B2N,{A, B \in 2^{\nat},} the operations are defined as follows:

~a=1anot aa | b=max(a,b)a or aa & b=min(a,b)a and ba^b=aba xor b \eqs{ \bnot a &= 1 - a \equiv \df{not}~ a \\ a \bor b &= \max{(a,b)} \equiv a ~\df{or} ~ a \\ a \band b &= \min{(a,b)} \equiv a ~\df{and} ~ b \\ a \bxor b &= \abs{a-b} \equiv a ~\df{xor} ~ b \\ }

The bitwise operators apply to bits (base expansions) of integers, not the integers themselves.

Binary Set Union

The A | B{A \bor B} operation is equivalent to A2B{A \cup_{2} B} (the binary union of A{A} and B{B}).

Binary Intersection

The A & B{A \band B} operation is equivalent to A2B{A \cap_{2} B} (the binary intersection of A{A} and B{B}).

Binary Set Minus

Given two binary sets A{A} and B,{B,} the binary set minus (A2B{A \rid_{2} B}) is expressed as A & (~B).{A \band (\bnot B).}

Binary Remainder

Given binary variables a{a} and n,{n,} the binary remainder of a{a} and n{n} can be computed as follows:

a rem na & (n1). a \rem n \equiv a \band (n-1).

example. 6 & (21)=6 rem 2=0.{6 \band (2-1)=6 \rem 2=0.}

example. 7 & (21)=7 rem 2=1.{7 \band (2-1) = 7 \rem 2=1.}

At Least One is False

The expression below returns true if at least one of a{a} and b{b} is 0. Note the different symbols, these are logical operators, not bitwise. The phrase "at least one of" can always be translated in terms of "nand."

¬(ab). \neg(a \land b).

Must Both Be False

Given two binary variables a{a} and b,{b,} the expression below returns true if both a{a} and b{b} are false.

[(a | b)=0]. [(a \bor b)=0].

At Least One is False

Given two variables a{a} and b,{b,} the expression below returns true if exactly one of the values is false.

[(¬a)^(¬b)]. [(\neg a) \bxor (\neg b)].