The following are notes on radices.
Numerals
Recall that the division theorem provides the following:
division theorem. Let n,d∈Z with d>0. Then there exist unique integers q and r such that n=dq+r with 0≤r<d.
theorem. Let b be an integer greater than 1. Any n∈Z+ can be expressed uniquely in the form
n=akbk+ak−1bk−1+…+a1b+a∅, where k∈N and a0,a1,…,ak are natural numbers less than b and ak=0.
example.
(1101)2=1(23)+1(22)+0(21)+1(20)=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:
- n∈Z, the number to convert.
- b>1, the base.
- Output: A, an array of bases.
- q←n
- i←0
- A←[ ]
- while q=0 do
- ai←q rem b
- A⇚ai
- q←q ∣ b
- k++
- return 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. To convert to binary d2, we start by writing 1 if d is odd, and 0 if d is even. Then, compute the integer division of d. That is, ⌊d/2⌋. If the result is even, write 0. If the result is odd, write 1. We continue this process until we get to 1. For example, consider converting the integer 37:
d | remainder |
---|
37 | 1 |
⌊37/2⌋=18 | 0 |
⌊18/2⌋=9 | 1 |
⌊9/2⌋=4 | 0 |
⌊4/2⌋=2 | 0 |
⌊2/2⌋=1 | 1 |
Mentally, we might list: 101001. Simply reverse this sequence (binary digits are read from right to left): 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:
power | decimal |
---|
20 | 1 |
21 | 2 |
22 | 4 |
23 | 8 |
24 | 16 |
25 | 32 |
26 | 64 |
27 | 64 |
28 | 128 |
29 | 512 |
210 | 1024 |
211 | 2048 |
212 | 4096 |
Below are the definitions of bitwise operations.
bitwise operations. Let (n)2=(an−1,an−1,…,a2,a1,a0) and (m)2=(bn−1,bn−1,…,b2,b1,b0) be binary integers, such that each ai and bi is a bit (1 or 0 but not both), for all 0≤i≤n, where i,n∈N. The following definitions apply for each bit a and b.
~aa | ba & ba^b=1−a≡not a=max(a,b)≡a or a=min(a,b)≡a and b=∣a−b∣≡a xor b Given A,B∈2N, the operations are defined as follows:
~aa | ba & ba^b=1−a≡not a=max(a,b)≡a or a=min(a,b)≡a and b=∣a−b∣≡a xor b The bitwise operators apply to bits (base expansions) of integers, not the integers themselves.
Binary Set Union
The A | B operation is equivalent to A∪2B (the binary union of A and B).
Binary Intersection
The A & B operation is equivalent to A∩2B (the binary intersection of A and B).
Binary Set Minus
Given two binary sets A and B, the binary set minus (A∖2B) is expressed as A & (~B).
Binary Remainder
Given binary variables a and n, the binary remainder of a and n can be computed as follows:
a rem n≡a & (n−1). example. 6 & (2−1)=6 rem 2=0.
example. 7 & (2−1)=7 rem 2=1.
At Least One is False
The expression below returns true if at least one of a and 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."
¬(a∧b). Must Both Be False
Given two binary variables a and b, the expression below returns true if both a and b are false.
[(a | b)=0]. At Least One is False
Given two variables a and b, the expression below returns true if exactly one of the values is false.
[(¬a)^(¬b)].