02 Bits Bytes Ints
02 Bits Bytes Ints
14-513 18-613
Analog Computers
Before digital computers there were analog computers.
https://www.daycounter.com/Calculators/Voltage-Summer/Voltage-
Summer-Calculator.phtml
https://www.quora.com/What-is-the-most-basic-voltage-adder-circuit-
without-a-transistor-op-amp-and-any-external-supply
Now we can easily interpret, communicate, and duplicate signals well enough to know
what they mean.
0 1 0
1.1V
0.9V
0.2V
0.0V
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 5
Carnegie Mellon
Binary Representation
Binary representation leads to a simple binary, i.e. base-2,
numbering system
▪ 0 represents 0
▪ 1 represents 1
▪ Each “place” represents a power of two, exactly as each place in our
usual “base 10”, 10-ary numbering system represents a power of 10
By encoding/interpreting sets of bits in various ways, we
can represent different things:
▪ Operations to be executed by the processor, numbers, enumerable
things, such as text characters
As long as we can assign it to a discrete number, we can
represent it in binary
Binary Representation:
Simple Numbers
For example, we can count in binary, a base-2 numbering
system
▪ 000, 001, 010, 011, 100, 101, 110, 111, …
▪ 000 = 0*22 + 0*21 + 0*20 = 0 (in decimal)
▪ 001 = 0*22 + 0*21 + 1*20 = 1 (in decimal)
▪ 010 = 0*22 + 1*21 + 0*20 = 2 (in decimal)
▪ 011 = 0*22 + 1*21 + 1*20 = 3 (in decimal)
▪ Etc.
For reference, consider some base-10 examples:
▪ 000 = 0*102 + 0*101 + 0*100
▪ 001 = 0*102 + 0*101 + 1*100
▪ 357 = 3*102 + 5*101 + 7*100
Hexadecimal
Hexadecimal 0016 to FF16
0 0 0000
▪ Base 16 number representation 1 1 0001
▪ Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’ 2 2 0010
3 3 0011
4 4 0100
5 5 0101
Consider 1A2B in Hexadecimal: 6 6 0110
▪ 1*163 + A*162 + 2*161 + B*160 7 7 0111
8 8 1000
▪ 1*163 + 10*162 + 2*161 + 11*160 = 6699 (decimal) 9 9 1001
A 10 1010
B 11 1011
C 12 1100
▪ The C Language prefixes hexadecimal numbers with “0x” D 13 1101
so they aren’t confused with decimal numbers E 14 1110
F 15 1111
▪ Write FA1D37B16 in C as
▪ 0xFA1D37B
▪ 0xfa1d37b 15213: 0011 1011 0110 1101
3 B 6 D
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 9
Carnegie Mellon
Boolean Algebra
Developed by George Boole in 19th Century
▪ Algebraic representation of logic
▪ Encode “True” as 1 and “False” as 0
And Or
◼ A&B = 1 when both A=1 and B=1 ◼ A|B = 1 when either A=1 or B=1
▪ 01101001 { 0, 3, 5, 6 }
▪ 76543210
▪ 01010101 { 0, 2, 4, 6 }
▪ 76543210
Operations
▪ & Intersection 01000001 { 0, 6 }
▪ | Union 01111101 { 0, 2, 3, 4, 5, 6 }
▪ ^ Symmetric difference 00111100 { 2, 3, 4, 5 }
▪ ~ Complement 10101010 { 1, 3, 5, 7 }
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 13
Carnegie Mellon
Bit-Level Operations in C
Operations &, |, ~, ^ Available in C 0 0 0000
▪ Apply to any “integral” data type 1 1 0001
2 2 0010
▪ long, int, short, char, unsigned 3 3 0011
▪ View arguments as bit vectors 4 4 0100
5 5 0101
▪ Arguments applied bit-wise 6 6 0110
7 7 0111
Examples (Char data type) 8 8 1000
▪ ~0x41 → 0xBE 9 9 1001
A 10 1010
~010000012 → 101111102
▪ B 11 1011
▪ ~0x00 → 0xFF C 12 1100
D 13 1101
▪ ~000000002 → 111111112 E 14 1110
▪ 0x69 & 0x55 → 0x41 F 15 1111
▪ 011010012 & 010101012 → 010000012
▪ 0x69 | 0x55 → 0x7D
▪ 011010012 | 010101012 → 011111012
Bit-Level Operations in C
Operations &, |, ~, ^ Available in C 0 0 0000
▪ Apply to any “integral” data type 1 1 0001
2 2 0010
▪ long, int, short, char, unsigned 3 3 0011
▪ View arguments as bit vectors 4 4 0100
5 5 0101
▪ Arguments applied bit-wise 6 6 0110
7 7 0111
Examples (Char data type) 8 8 1000
▪ ~0x41 → 0xBE 9 9 1001
A 10 1010
~010000012 → 101111102
▪ B 11 1011
▪ ~0x00 → 0xFF C 12 1100
D 13 1101
▪ ~000000002 → 111111112 E 14 1110
▪ 0x69 & 0x55 → 0x41 F 15 1111
▪ 011010012 & 010101012 → 010000012
▪ 0x69 | 0x55 → 0x7D
▪ 011010012 | 010101012 → 011111012
Bit-Level Operations in C
Operations &, |, ~, ^ Available in C
0 0 0000
▪ Apply to any “integral” data type 1 1 0001
▪ long, int, short, char, unsigned 2 2 0010
3 3 0011
▪ View arguments as bit vectors 4 4 0100
5 5 0101
▪ Arguments applied bit-wise 6 6 0110
Examples (Char data type) 7 7 0111
8 8 1000
▪ ~0x41 → 1011 11100xBE~010000012 → 101111102 9 9 1001
▪ ~0x00 → 1111 11110xF~000000002 → 1111112 A 10 1010
B 11 1011
▪ 0x69 & 0x55: 0x69 | 0x55: C 12 1100
0110 1001 0110 1001 D 13 1101
E 14 1110
& 0101 0101 | 0101 0101 F 15 1111
----------------- -----------------
0100 0001011010 0111 1101011010012 012 & 010101012 → 010000012
0x7D
▪ 011010012 | 010101012 → 011111012
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 16
Carnegie Mellon
Shift Operations
Left Shift: x << y Argument x 01100010
▪ Shift bit-vector x left y positions << 3 00010000
– Throw away extra bits on left Log. >> 2 00011000
▪ Fill with 0’s on right
Arith. >> 2 00011000
Right Shift: x >> y
▪ Shift bit-vector x right y positions
Argument x 10100010
Throw away extra bits on right
▪
▪ Logical shift << 3 00010000
▪ Fill with 0’s on left Log. >> 2 00101000
▪ Arithmetic shift Arith. >> 2 11101000
▪ Replicate most significant bit on left
Undefined Behavior
▪ Shift amount < 0 or ≥ word size
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 18
Carnegie Mellon
0 1
▪ 2 bit number line
00 01 10 11
▪ 3 bit number line
0 1 2 3 4 5 6 7
-4 -3 -2 -1 0 1 2 3
A B C D E F G H
Overflow
Let’s consider a simple 3 digit number line:
0 1 2 3 4 5 6 7
000 001 010 011 100 101 110 111
Modulus Arithmetic
Let’s explore this idea of overflow some more
▪ 111 + 001 = 1 000 = 000
▪ 111 + 010 = 1 001 = 001
▪ 111 + 011 = 1 010 = 010
▪ 111 + 100 = 1 011 = 011
▪ …
▪ 111 + 110 = 1 101 = 101
▪ 111 + 111 = 1 110 = 110
A Magic Trick!
Let’s just start with three ideas:
▪ 1 should be represented as 1
▪ -1 + 1 = 0
▪ We want addition to work in the familiar way, with simple rules.
Negative Numbers
Well, if 111 is -1, what is -2?
▪ -1 - 1
▪ 111 – 001 = 110
-2 + 5 should be 3, right?
▪ 110 + 101 = 1 011 = 011
So if x + (Complement(x) + 1) = 0
▪ Complement(x) + 1 must equal –x
Sign Bit
▪ For 2’s complement, most significant bit indicates sign
▪ 0 for nonnegative, 1 for negative
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 33
Carnegie Mellon
Numeric Ranges
Unsigned Values
Two’s Complement Values
▪ UMin = 0
▪ TMin = –2w–1
000…0
100…0
▪ UMax = 2w –1
▪ TMax = 2w–1 – 1
111…1
011…1
▪ Minus 1
111…1
Values for W = 16
Decimal Hex Binary
UMax 65535 FF FF 11111111 11111111
TMax 32767 7F FF 01111111 11111111
TMin -32768 80 00 10000000 00000000
-1 -1 FF FF 11111111 11111111
0 0 00 00 00000000 00000000
Quiz Time!
Check out:
https://canvas.cmu.edu/courses/40739/quizzes/123408
w–1 0
ux + + + ••• + + +
x - + + ••• + + +
Conversion Visualized
2’s Comp. → Unsigned
▪ Ordering Inversion UMax
▪ Negative → Big Positive UMax – 1
TMax + 1 Unsigned
TMax TMax Range
2’s Complement 0 0
Range –1
–2
TMin
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 39
Carnegie Mellon
Casting Surprises
Expression Evaluation
▪ If there is a mix of unsigned and signed in single expression,
signed values implicitly cast to unsigned
▪ Including comparison operations <, >, ==, <=, >=
▪ Examples for W = 32: TMIN = -2,147,483,648 , TMAX = 2,147,483,647
Constant1 Constant2 Relation Evaluation
0 0 0U 0U == unsigned
-1 -1 0 0 < signed
-1 -1 0U 0U > unsigned
2147483647
2147483647 -2147483647-1
-2147483648 > signed
2147483647U
2147483647U -2147483647-1
-2147483648 < unsigned
-1 -1 -2 -2 > signed
(unsigned)-1
(unsigned) -1 -2 -2 > unsigned
2147483647
2147483647 2147483648U
2147483648U < unsigned
2147483647
2147483647 (int) (int)
2147483648U
2147483648U > signed
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 41
Carnegie Mellon
Summary
Casting Signed Unsigned: Basic Rules
Bit pattern is maintained
But reinterpreted
Sign Extension
Task:
▪ Given w-bit signed integer x
▪ Convert it to w+k-bit integer with same value
Rule:
▪ Make k copies of sign bit:
▪ X’ = xw–1 ,…, xw–1 , xw–1 , xw–2 ,…, x0
k copies of MSB w
X •••
•••
X’
••• •••
k w
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 44
Carnegie Mellon
-16 8 4 2 1 -16 8 4 2 1
10 = 0 1 0 1 0 -10 = 1 0 1 1 0
-32 16 8 4 2 1 -32 16 8 4 2 1
10 = 0 0 1 0 1 0 -10 = 1 1 0 1 1 0
Truncation
Task:
▪ Given k+w-bit signed or unsigned integer X
▪ Convert it to w-bit integer X’ with same value for “small enough” X
Rule:
▪ Drop top k bits:
▪ X’ = xw–1 , xw–2 ,…, x0
k w
X ••• •••
•••
X’ •••
w
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 46
Carnegie Mellon
2 = 0 0 0 1 0 10 = 0 1 0 1 0
-8 4 2 1 -8 4 2 1
2 = 0 0 1 0 -6 = 1 0 1 0
2 mod 16 = 2 10 mod 16 = 10U mod 16 = 10U = -6
-16 8 4 2 1 -16 8 4 2 1
-6 = 1 1 0 1 0 -10 = 1 0 1 1 0
-8 4 2 1 -8 4 2 1
-6 = 1 0 1 0 6 = 0 1 1 0
-6 mod 16 = 26U mod 16 = 10U = -6 -10 mod 16 = 22U mod 16 = 6U = 6
Summary:
Expanding, Truncating: Basic Rules
Expanding (e.g., short int to int)
▪ Unsigned: zeros added
▪ Signed: sign extension
▪ Both yield expected result
Unsigned Addition
Operands: w bits u •••
+v •••
True Sum: w+1 bits
u+v •••
Discard Carry: w bits UAddw(u , v) •••
▪ If true sum ≥ 2w
▪ At most once UAdd4(u , v)
True Sum 16
14
2w+1 Overflow 12
10
8
2w 6 12
14
4 10
8
2
6 v
0 0 4
Modular Sum 0
2
4
6
2
u 8
10
12
14
0
Values
▪ 4-bit two’s comp. TAdd4(u , v)
▪ Range from -8 to +7
Wraps Around
8
▪ If sum 2w–1 6
Becomes negative
▪
4
2
▪ At most once 0
6
-2 4
▪ If sum < –2w–1 -4 2
0
-6
▪ Becomes positive -8
-2
▪ At most once
-8
-6
-4 -6
-4
v
-2
0 -8
2
u 4
6 PosOver
Multiplication
Goal: Computing Product of w-bit numbers x, y
▪ Either signed or unsigned
Result: Same as computing ideal, exact result x*y and keeping
w lower bits.
Ideal,exact results can be bigger than w bits
▪ Worst case is up to 2w bits
▪ Unsigned, because all bits are magnitude
▪ Signed, but only for Tmin*Tmin, because anything added to Tmin
reduces its magnitude and Tmax is less than Tmin.
So, maintaining exact results…
▪ would need to keep expanding word size with each product computed
▪ Impossible in hardware (at least without limits), as all resources are finite
▪ In practice, is done in software, if needed
▪ e.g., by “arbitrary precision” arithmetic packages
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 55
Carnegie Mellon
Round-toward-0 Divide
Quotient of Negative Number by Power of 2
▪ Want x / 2k (Round Toward 0)
▪ Compute as (x+(2k-1))/ 2k
▪ In C: (x + (1<<k)-1) >> k
▪ Biases dividend toward 0
Case 1: No rounding k
Dividend: u 1 ••• 0 ••• 0 0
+2k –1 0 ••• 0 0 1 ••• 1 1
1 ••• 1 ••• 1 1 Binary Point
Divisor: / 2k 0 ••• 0 1 0 ••• 0 0
u / 2k 0 •••
1 1 1 1 ••• . 1 ••• 1 1
Incremented by 1
Biasing adds 1 to final result
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 60
Carnegie Mellon
Byte Ordering
So, how are the bytes within a multi-byte word ordered in
memory?
Conventions
▪ Big Endian: Sun (Oracle SPARC), PPC Mac, Internet
Least significant byte has highest address
▪
▪ Little Endian: x86, ARM processors running Android, iOS, and Linux
▪ Least significant byte has lowest address
Deciphering Numbers
▪ Value: 0x12ab
▪ Pad to 32 bits: 0x000012ab
▪ Split into bytes: 00 00 12 ab
▪ Reverse: ab 12 00 00
Questions?
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 65