02 Bits Ints Part1
02 Bits Ints Part1
Announcements
Linux Boot Camp Thursday, 12:20 EDT
Everything is bits
Each bit is 0 or 1
By encoding/interpreting sets of bits in various ways
▪ Computers determine what to do (instructions)
▪ … and represent and manipulate numbers, sets, strings, etc…
Why bits? Electronic Implementation
Everything is bits
Each bit is 0 or 1
By encoding/interpreting sets of bits in various ways
▪ Computers determine what to do (instructions)
▪ … and represent and manipulate numbers, sets, strings, etc…
Why bits? Electronic Implementation
▪ Easy to store with bistable elements
▪ Reliably transmitted on noisy and inaccurate wires
0 1 0
1.1V
0.9V
0.2V
0.0V
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 6
Carnegie Mellon
char 1 1 1
short 2 2 2
int 4 4 4
long 4 8 8
float 4 4 4
double 8 8 8
pointer 4 8 8
char 1 1 1
short 2 2 2
int 4 4 4
long 4 8 8
float 4 4 4
double 8 8 8
pointer 4 8 8
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 14
Carnegie Mellon
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 → 0xBE 9 9 1001
~010000012 → 101111102
▪ A 10 1010
B 11 1011
▪ ~0x00 → 0xFF C 12 1100
▪ ~000000002 → 111111112 D 13 1101
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 → 0xBE 9 9 1001
~0100 000122→
~01000001
▪ →10111110
1011 11102 2 A 10 1010
B 11 1011
▪ ~0x00 → 0xFF C 12 1100
▪ ~0000
~00000000
000022→→11111111
1111 11112 2 D 13 1101
E 14 1110
▪ 0x69 & 0x55 → 0x41 F 15 1111
▪ 0110
01101001
100122&&01010101
0101 01012 2→→01000001
0100 0001
2 2
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
Throw away extra bits on right
▪ Argument x 10100010
Undefined Behavior
▪ Shift amount < 0 or ≥ word size
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 18
Carnegie Mellon
Encoding Integers
Unsigned Two’s Complement
w−1 w−2
B2U(X ) = xi 2 i
B2T(X) = − xw−1 2 w−1
+ xi 2 i
i=0 i=0
Sign Bit
▪ For 2’s complement, most significant bit indicates sign
▪ 0 for nonnegative
▪ 1 for negative 20
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon
-16 8 4 2 1
10 = 0 1 0 1 0 8+2 = 10
-16 8 4 2 1
-10 = 1 0 1 1 0 -16+4+2 = -10
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
Observations C Programming
▪ |TMin | = TMax + 1 ▪ #include <limits.h>
▪ Asymmetric range ▪ Declares constants, e.g.,
▪ UMax = 2 * TMax + 1 ▪ ULONG_MAX
▪ Question: abs(TMin)? ▪ LONG_MAX
▪ LONG_MIN
▪ Values platform specific
Quiz Time!
Check out:
https://canvas.cmu.edu/courses/23122/quizzes/61
559
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 32
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 00 < 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)2147483648U
(int) 2147483648U > signed
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 34
Carnegie Mellon
Summary
Casting Signed ↔ Unsigned: Basic Rules
Bit pattern is maintained
But reinterpreted
Can have unexpected effects: adding or subtracting 2w
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
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
w 38
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 41
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
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