Binary Data Representation
Lesson 8
1
Binary System Origins
2
Binary System Origins
3
Binary System Origins
4
The Big Questions
5
Sign-Magnitude — Ուղիղ Կոդ
6
One’s Complement — Հակադարձ Կոդ
10110110
-X=(2N-1)-X
28 - 1 = 100000000 - 1 = 11111111
X = 01001001: 11111111 - X =
-X = 10110110
7
One’s Complement — Հակադարձ Կոդ
8
Two’s Complement — Լրացուցիչ Կոդ
One’s:-X=2N-1-X
Two’s:-X=2N -X
9
Two’s Complement — Լրացուցիչ Կոդ
10
Isomorphic to�Cyclic group
ℤ / 2N
Two’s Complement essentially defines modular arithmetic with n = 2N
Two’s Complement — Լրացուցիչ Կոդ
11
Example: x, y > 0
x + (-y) = x + (2N - y)
= 2N + (x - y)
= 2N - (y - x)
= -(y - x)
= x - y
Example: x > 0
-(-x) = 2N - (-x)
= 2N - (2N - x)
= x
No special logic for negative numbers.
TC circuit
only knows
2N-X
Two’s Complement — Լրացուցիչ Կոդ
12
Comparing All Three
13
Sign-Magnitude One’s Complement Two’s Complement
Comparing All Three
14
Why does this work “best”
Why did this “win”?
Sign-Magnitude One’s Complement Two’s Complement
Because two’s complement has the best underlying mathematical / algebraic structure.
Fundamental Algebraic Structures
15
Monoid
Fundamental Algebraic Structures
16
Monoid
(a + b)c = ac + bc�c(a + b) = ca + cb
�
Fundamental Algebraic Structures
17
Monoid
a + (-a) = 0
�
(a + b)c = ac + bc�c(a + b) = ca + cb
�
Fundamental Algebraic Structures
18
Monoid
a + (-a) = 0
�
ab = ba
�
(a + b)c = ac + bc�c(a + b) = ca + cb
�
Fundamental Algebraic Structures
19
Monoid
a + (-a) = 0
�
ab = ba
�
ab = 0 ⇒ a or b = 0
�
(a + b)c = ac + bc�c(a + b) = ca + cb
�
Fundamental Algebraic Structures
20
Monoid
a + (-a) = 0
�
ab = ba
�
ab = 0 ⇒ a or b = 0
�
a * 1/a = 1
�
Fields have the “usual” numerical properties
by which we make sense of the world.
(a + b)c = ac + bc�c(a + b) = ca + cb
�
Bigger Universe of Algebraic Structures
21
XOR Swap
22
Regular (with temp)
Why does this work?
Which is faster?
XOR Swap Algebra
23
L1 follows from L4 because:
e = (ab)2 = abab
Multiply both sides by a, b:
ab = aababb
ab = ba
XOR Swap
24
XOR Swap
25
XOR Swap
26
XOR Swap
27
XOR Swap
28
Functions on Bits
unsigned int x;�unsigned int c = 0;
for (; x; x >>= 1)�{� c += x & 1;�}
29
Functions on Bits
unsigned int x;�unsigned int c = 0;
for (; x; c++)�{� x &= x - 1;�}
30
Functions on Bits
unsigned int x;�unsigned int c = 0;
for (; x; c++)�{� x &= x - 1; // because 11000000 - 1 = 10111111�}
&-ing these together eliminates the rightmost bit that was 1
31
Functions on Bits
bool opposite = ((x ^ y) < 0);
32
Functions on Bits
33
bool is = x && !(x & (x - 1));
x = 01000000� x-1 = 00111111
then
x & (x-1) = 0
See more at bit twiddling hacks
Part II
Multi-Byte Data Representation
Multi-Byte Data Representation
There are 00000010 00000000 kinds of people in the world:�Those who understand endianness,�and those who don't.
35
“Powerful you have become, the dark side I sense in you.”
Multi-Byte Data Representation
36
int main()
{
short a = 256; // 00000001'00000000
// What will these print and why?
cout << (((char*)&a)[0] == 0);
cout << (((char*)&a)[1] == 1);
}
Multi-Byte Data Representation
37
Multi-Byte Data Representation
38
Multi-Byte Data Representation
39
Multi-Byte Data Representation
Big-endian
40
Multi-Byte Data Representation
Little-endian
41
Multi-Byte Data Representation
Little-endian
42
Multi-Byte Data Representation
43
Little-Endian Advantages
44
Little-Endian Advantages
45
Little-Endian Advantages
46
Little-Endian Advantages
47
Little-Endian Advantages
48
Little-Endian Advantages
49
Big-Endian Advantages
50
Big-Endian Advantages
51
Big-Endian Advantages
52
Big-Endian Advantages
53
Big-Endian Advantages
54
Multi-Byte Data Representation
55
So what’s the verdict?
Multi-Byte Data Representation
56
So what’s the verdict?
“Agreement upon an order is more important
than the order agreed upon.”
Danny Cohen
Byte Swap
Unsigned int change_endianness(unsigned int value)�{� Unsigned int result = 0;�� result |= (value & 0x000000FF) << 24;� result |= (value & 0x0000FF00) << 8;� result |= (value & 0x00FF0000) >> 8;� result |= (value & 0xFF000000) >> 24;
return result;�}
57
Assembly
BSWAP eax
Hexspeak
58
Microsoft (Visual Studio Debugger)
59
Apple iOS Crash Reports
60
Other Hexspeak
61
Questions?
62