.
......
CLEANCalculating, Legacy, Endianness
Addressing, Networks
Karst Koymans
Informatics InstituteUniversity of Amsterdam
(version 15.3, 2016/02/08 10:13:12)
Tuesday, February 9, 2016
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 1 / 34
...1 CalculatingCountingMultiplication
...2 Legacy
...3 EndiannessNumeral systemsIntermezzoBig-endian and little-endian legacy
...4 AddressingThings to know by heartConverting from decimal to binaryIPv4 addresses
...5 Networks
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 2 / 34
Calculating
Outline...1 Calculating
CountingMultiplication
...2 Legacy
...3 EndiannessNumeral systemsIntermezzoBig-endian and little-endian legacy
...4 AddressingThings to know by heartConverting from decimal to binaryIPv4 addresses
...5 Networks
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 3 / 34
Calculating Counting
Outline...1 Calculating
CountingMultiplication
...2 Legacy
...3 EndiannessNumeral systemsIntermezzoBig-endian and little-endian legacy
...4 AddressingThings to know by heartConverting from decimal to binaryIPv4 addresses
...5 Networks
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 4 / 34
Calculating Counting
The process of counting
Counting is a process that. . .. . . starts with 0 as the initial count. . . repeats until there is nothing left to count
. . . labels an element with the count n up till now
. . . updates the count to n + 1
The final result is a number n
which labels every element with a number < n
Hence n = {0, . . . , n − 1}
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 5 / 34
Calculating Counting
The legacy of counting
Mathematicians traditionally start counting at 1which is “wrong” (in a very objective manner1)
Suggested set-range operator “...”Ruby has this as a sequence-range operatorn = 0...nSo this includes the left hand side 0but excludes the right hand side nThis eliminates in many cases the awkward n − 1Could be overloaded for sets and sequences
1EWD831, http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDFKarst Koymans (UvA) CLEAN Tuesday, February 9, 2016 6 / 34
Calculating Multiplication
Outline...1 Calculating
CountingMultiplication
...2 Legacy
...3 EndiannessNumeral systemsIntermezzoBig-endian and little-endian legacy
...4 AddressingThings to know by heartConverting from decimal to binaryIPv4 addresses
...5 Networks
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 7 / 34
Calculating Multiplication
Legacy “bigendian” numbers.Space needed, but not present, at left margin for multiplication..
......
12345 12345
6789 6789
-----x -----x
11105 111105
8760 98760
415 86415
70 74070
-----+ --------+
10205 83810205
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 8 / 34
Calculating Multiplication
“Right”-endian numbers, in sequence order
123456789
x-----111105987608641574070+--------
83810205
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 9 / 34
Legacy
Outline...1 Calculating
CountingMultiplication
...2 Legacy
...3 EndiannessNumeral systemsIntermezzoBig-endian and little-endian legacy
...4 AddressingThings to know by heartConverting from decimal to binaryIPv4 addresses
...5 Networks
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 10 / 34
Legacy
Legacy
.Legacy..
......
The biggest obstacle and problem for progress and innovation is legacy
and the incapability of humans to cope with change.
.Suggestion..
......
Imagine we could change the world by writing numbers “the other way
around”. To support the difficult transition we could use 9876543210 a as
digits in place of 0123456789.aMaybe using a font that also distinguishes 0 and 8 from their mirror images.
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 11 / 34
Endianness
Outline...1 Calculating
CountingMultiplication
...2 Legacy
...3 EndiannessNumeral systemsIntermezzoBig-endian and little-endian legacy
...4 AddressingThings to know by heartConverting from decimal to binaryIPv4 addresses
...5 Networks
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 12 / 34
Endianness Numeral systems
Outline...1 Calculating
CountingMultiplication
...2 Legacy
...3 EndiannessNumeral systemsIntermezzoBig-endian and little-endian legacy
...4 AddressingThings to know by heartConverting from decimal to binaryIPv4 addresses
...5 Networks
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 13 / 34
Endianness Numeral systems
Mapping fixed length binary sequences to numbers.Definition..
......
A binary sequence b of length n is a map(ping) or function b : n → 2.
The “natural” assignment of a natural number to b is
n−1∑i=0
b(i)2i =∑
i∈0...nb(i)2i
.Theorem..
......
This correspondence is a 1-1, onto function between sequences
of length n and natural numbers smaller than 2n.
Because of “trailing zeroes” this does not extend to a 1-1 functionbetween sequences of arbitrary length and all natural numbers.
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 14 / 34
Endianness Numeral systems
Mapping arbitrary length binary sequences to numbers
By shifting the assignments of sequences of length n to the right by addingthe number 2n − 1 we get a 1-1 correspondence between arbitrary lengthbinary sequences and all natural numbers.
.Theorem..
......
The map b 7→∑
i∈0...n b(i)2i + (2n − 1) = ∑i∈0...n (b(i) + 1)2i
is a 1-1, onto function between 2<ω and N.
This construction leads to the “2-adic numeration system”, also calledbijective base-2 numeral system. It also works with bases k > 2.
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 15 / 34
Endianness Numeral systems
2-adic versus binary numbers
2-adic binary 0 1 2 3 4 5 6 7 8 9 10 11 12<> <> 01 0 0 12 1 1 211 00 0 312 01 1 421 10 2 522 11 3 6111 000 0 7112 001 1 8121 010 2 9122 011 3 10211 100 4 11212 101 5 12
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 16 / 34
Endianness Intermezzo
Outline...1 Calculating
CountingMultiplication
...2 Legacy
...3 EndiannessNumeral systemsIntermezzoBig-endian and little-endian legacy
...4 AddressingThings to know by heartConverting from decimal to binaryIPv4 addresses
...5 Networks
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 17 / 34
Endianness Intermezzo
Punycode bootstring encoding
Specified in RFC 3492
For internationalized domain names and usesa “little-endian” number representation for deltas (good!)a mixed-radix system with threshold valuesa generalisation of the k-adic numeration system
No “trailing zeroes” issueSelf-delimiting inside number sequencesAlso called “Generalized variable-length integers”
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 18 / 34
Endianness Intermezzo
Generalized variable-length integers
.Bootstring algorithm..
......
Let a be a positive natural number and ri, ti be sequences of radices andthresholds. Usually we demand that ri + ti = k, for some fixed “base” k, sothat we may use digits in 0...k (remember this means 0, . . . , k-1).Write A0 = a and start calculating inductively, given Ai. If Ai < ti, theprocess ends with last digit ai = Ai. Otherwise, use quotient withremainder to produce Bi and Ci such that Ai = Bi + riCi. Increase Bi untilit is bigger than the threshold, as follows:Calculate pi ≤ Ci such that ti ≤ Bi + ripi < ti + ri. ThenAi = (Bi + ripi) + ri(Ci − pi). The next digit in the expansion of a isai = Bi + ripi and the process continues with Ai+1 = Ci − pi.
Interesting cases are ti = 0, ri = k; ti = 1, ri = k − 1 and ti = k − 1, ri = 1,which give normal k-ary; k-adic and “unary” presentations.
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 19 / 34
Endianness Big-endian and little-endian legacy
Outline...1 Calculating
CountingMultiplication
...2 Legacy
...3 EndiannessNumeral systemsIntermezzoBig-endian and little-endian legacy
...4 AddressingThings to know by heartConverting from decimal to binaryIPv4 addresses
...5 Networks
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 20 / 34
Endianness Big-endian and little-endian legacy
Historic conventions.Writing numbers in “reverse” (right-to-left)........
The root of all evil and why we should go evil
.Increasing memory “normally” (left-to-right)..
......
Big-endian systems keep multi-byte numbers looking okMSB (Most Significant Byte) first
Little-endian systems make multi-byte numbers look weirdLSB (Least Significant Byte) firstEffectively also growing memory in reverse (to the left)
Giving at least consistency in appearance
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 21 / 34
Addressing
Outline...1 Calculating
CountingMultiplication
...2 Legacy
...3 EndiannessNumeral systemsIntermezzoBig-endian and little-endian legacy
...4 AddressingThings to know by heartConverting from decimal to binaryIPv4 addresses
...5 Networks
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 22 / 34
Addressing Things to know by heart
Outline...1 Calculating
CountingMultiplication
...2 Legacy
...3 EndiannessNumeral systemsIntermezzoBig-endian and little-endian legacy
...4 AddressingThings to know by heartConverting from decimal to binaryIPv4 addresses
...5 Networks
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 23 / 34
Powers of two (0-15)
20 = 1
21 = 2
22 = 4
23 = 8
24 = 16
25 = 32
26 = 64
27 = 128
28 = 256
29 = 512
210 = 1024
211 = 2048
212 = 4096
213 = 8192
214 = 16384
215 = 32768
Powers of two (16-31)2
216 = 65536
217 = 131072
218 = 262144
219 = 524288
220 = 1048576
221 = 2097152
222 = 4194304
223 = 8388608
224 = 16777216
225 = 33554432
226 = 67108864
227 = 134217728
228 = 268435456
229 = 536870912
230 = 1073741824
231 = 2147483648
2No need to know more than the first entry by heart ;)
Hexadecimal notation (with legacy binary and decimal)
0x0 = 0b0000 = 0 ( 0)
0x1 = 0b0001 = 1 ( 1)
0x2 = 0b0010 = 2 ( 2)
0x3 = 0b0011 = 3 ( 3)
0x4 = 0b0100 = 4 ( 4)
0x5 = 0b0101 = 5 ( 5)
0x6 = 0b0110 = 6 ( 6)
0x7 = 0b0111 = 7 ( 7)
0x8 = 0b1000 = 8 ( 8)
0x9 = 0b1001 = 9 ( 9)
0xa = 0b1010 = 10 ( 10)
0xb = 0b1011 = 11 ( 11)
0xc = 0b1100 = 12 ( 12)
0xd = 0b1101 = 13 ( 13)
0xe = 0b1110 = 14 ( 14)
0xf = 0b1111 = 15 ( 15)
Addressing Converting from decimal to binary
Outline...1 Calculating
CountingMultiplication
...2 Legacy
...3 EndiannessNumeral systemsIntermezzoBig-endian and little-endian legacy
...4 AddressingThings to know by heartConverting from decimal to binaryIPv4 addresses
...5 Networks
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 27 / 34
Addressing Converting from decimal to binary
The “big-endian” way
151 = 128 + 23= 128 + 16 + 7= 128 + 16 + 4 + 3= 128 + 16 + 4 + 2 + 1
151 = 1 · 128 + 0 · 64 + 0 · 32 + 1 · 16 + 0 · 8 + 1 · 4 + 1 · 2 + 1 · 1
151 = 100101112 = 0b10010111
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 28 / 34
Addressing Converting from decimal to binary
The “little-endian” way
151 = 1 + 2 · 75= 1 + 2 · (1 + 2 · 37)= 1 + 2 · (1 + 2 · (1 + 2 · 18))= 1 + 2 · (1 + 2 · (1 + 2 · (0 + 2 · 9)))= 1 + 2 · (1 + 2 · (1 + 2 · (0 + 2 · (1 + 2 · 4))))= 1 + 2 · (1 + 2 · (1 + 2 · (0 + 2 · (1 + 2 · (0 + 2 · 2)))))= 1 + 2 · (1 + 2 · (1 + 2 · (0 + 2 · (1 + 2 · (0 + 2 · (0 + 2 · 1))))))
151 = 100101112
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 29 / 34
Addressing IPv4 addresses
Outline...1 Calculating
CountingMultiplication
...2 Legacy
...3 EndiannessNumeral systemsIntermezzoBig-endian and little-endian legacy
...4 AddressingThings to know by heartConverting from decimal to binaryIPv4 addresses
...5 Networks
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 30 / 34
Addressing IPv4 addresses
IPv4 addresses.Definition (IPv4 address)........An IPv4 address is a mapping or function f : 32 → 2, where 2 = {0, 1}
The decimal dotted notation for IPv4 addresses can be seen asa base-256 numberwith a decimal notation for its “digits”
Other notations for IPv4 addresses might3 be useda.b.c with a, b 8-bits and c 16-bitsa.b with a 8-bits and b 24-bitsa with a 32-bits
3This is allowed by the POSIX API, see “man inet addr”.Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 31 / 34
Networks
Outline...1 Calculating
CountingMultiplication
...2 Legacy
...3 EndiannessNumeral systemsIntermezzoBig-endian and little-endian legacy
...4 AddressingThings to know by heartConverting from decimal to binaryIPv4 addresses
...5 Networks
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 32 / 34
Networks
IPv4 address classes
.Definition (IP address classes)..
......
Class A: f(0) = 0; network part is f(0), . . . , f(7)
Class B: f(0) = 1; f(1) = 0, network part is f(0), . . . , f(15)
Class C: f(0) = f(1) = 1; f(2) = 0, network part is f(0), . . . , f(23)
Class D: f(0) = f(1) = f(2) = 1; f(3) = 0 multicast
Class E: f(0) = f(1) = f(2) = f(3) = 1 reserved
The division into classes is mostly historic.Classful addressing has been replaced by classless addressing.
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 33 / 34
Networks
IPv4 subnetworks and CIDR notation
.Definition (IP address classes)..
......
Given an IP address f : 32 → 2 and a prefix length k ≤ 32 the notation f/k
denotes a network (prefix) consisting of all IP addresses g : 32 → 2 with
g ↾ 0...k = f ↾ 0...k.
Usually we demand that f ↾ k...32 has only 0s.
In other words, the first k bits in the sequence determine the network partand the remaing (32 − k) bits the host part.In case there are non-0 host bits in the notation f/k this denotes acontamination of a host with the network this host resides in.
Karst Koymans (UvA) CLEAN Tuesday, February 9, 2016 34 / 34