03 Bits Ints Part2
03 Bits Ints Part2
Assignment Announcements
Lab 0 available via Autolab.
▪ Due Fri, June 4, 11:59pm
▪ No grace days
▪ No late submissions
▪ Just do it!
Lab 1 available via Autolab
▪ Released
▪ Due Fri, June 4, 11:59pm
▪ Read instructions carefully: writeup, bits.c, tests.c
Quirky software infrastructure
▪
▪ Based on lectures 2, 3, and 4 (CS:APP Chapter 2)
▪ After today’s lecture you will know everything for the integer
problems
▪ Floating point covered Tue, June 1
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 3
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
-16 8 4 2 1
-10 = 1 0 1 1 0 -16+4+2 = -10
Truncation
Misunderstanding integers
can lead to the end of the
world as we know it!
Thule (Qaanaaq), Greenland
US DoD “Site J” Ballistic
Missile Early Warning
System (BMEWS)
10/5/60: world nearly ends
Missile radar echo: 1/8s
BMEWS reports: 75s echo(!)
1000s of objects reported
NORAD alert level 5:
▪ Immediate incoming nuclear
attack!!!!
Unsigned Addition
Operands: w bits u •••
+v •••
True Sum: w+1 bits
u+v •••
Discard Carry: w bits UAddw(u , v) •••
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
TAdd Overflow
Functionality True Sum
▪ True sum requires w+1 0 111…1 2w–1
PosOver
bits TAdd Result
▪ Drop off MSB 0 100…0 2w –1–1 011…1
▪ Treat remaining bits as
2’s comp. integer 0 000…0 0 000…0
1 000…0 NegOver
–2w
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
▪ At most once -8
-6
-4 -6
-4
v
-2
0 -8
2
u 4
6 PosOver
Characterizing TAdd
Positive Overflow
Functionality TAdd(u , v)
▪ True sum requires w+1 bits
>0
▪ Drop off MSB v
▪ Treat remaining bits as 2’s <0
comp. integer
<0 >0
u
Negative Overflow
Multiplication
Goal: Computing Product of w-bit numbers x, y
▪ Either signed or unsigned
But, exact results can be bigger than w bits
▪ Unsigned: up to 2w bits
Result range: 0 ≤ x * y ≤ (2w – 1) 2 = 22w – 2w+1 + 1
▪
▪ Two’s complement min (negative): Up to 2w-1 bits
▪ Result range: x * y ≥ (–2w–1)*(2w–1–1) = –22w–2 + 2w–1
▪ Two’s complement max (positive): Up to 2w bits, but only for (TMinw)2
▪ Result range: x * y ≤ (–2w–1) 2 = 22w–2
Unsigned Multiplication in C
u •••
Operands: w bits
* v •••
True Product: 2*w bits u · v ••• •••
UMultw(u , v) •••
Discard w bits: w bits
Signed Multiplication in C
u •••
Operands: w bits
* v •••
True Product: 2*w bits u · v ••• •••
TMultw(u , v) •••
Discard w bits: w bits
Multiplication
Goal: Computing Product of w-bit numbers x, y
▪ Either signed or unsigned
But, exact results can be bigger than w bits
▪ Unsigned: up to 2w bits
Result range: 0 ≤ x * y ≤ (2w – 1) 2 = 22w – 2w+1 + 1
▪
▪ Two’s complement min (negative): Up to 2w-1 bits
▪ Result range: x * y ≥ (–2w–1)*(2w–1–1) = –22w–2 + 2w–1
▪ Two’s complement max (positive): Up to 2w bits, but only for (TMinw)2
▪ Result range: x * y ≤ (–2w–1) 2 = 22w–2
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 27
Carnegie Mellon
x 10011101
+ ~x 0 1 1 0 0 0 1 0
-1 11111111
x = 15213
Decimal Hex Binary
x 15213 3B 6D 00111011 01101101
~x -15214 C4 92 11000100 10010010
~x+1 -15213 C4 93 11000100 10010011
y -15213 C4 93 11000100 10010011
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 28
Carnegie Mellon
x = TMin
Decimal Hex Binary
x -32768 80 00 10000000 00000000
~x 32767 7F FF 01111111 11111111
~x+1 -32768 80 00 10000000 00000000
Multiplication:
▪ Unsigned/signed: Normal multiplication followed by truncate,
same operation on bit level
▪ Unsigned: multiplication mod 2w
▪ Signed: modified multiplication mod 2w (result in proper range)
Even better
size_t i;
for (i = cnt-2; i < cnt; i--)
a[i] += a[i+1];
▪ Data type size_t defined as unsigned value with length = word size
Quiz Time!
Check out:
https://canvas.cmu.edu/courses/23122/quizzes/61
569
•••
Machine Words
Any given computer has a “Word Size”
▪ Nominal size of integer-valued data
▪ and of addresses
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
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
Decimal: 15213
Representing Integers Binary: 0011 1011 0110 1101
Hex: 3 B 6 D
Printf directives:
%p: Print pointer
%x: Print Hexadecimal
Representing Pointers
int B = -15213;
int *P = &B;
Representing Strings
char S[6] = "18213";
Strings in C
▪ Represented by array of characters
▪ Each character encoded in ASCII format IA32 Sun
Standard 7-bit encoding of character set
▪ 31 31
▪ Character “0” has code 0x30 38 38
– Digit i has code 0x30+I 32 32
▪ man ascii for code table 31 31
▪ String should be null-terminated 33 33
▪ Final character = 0
00 00
Compatibility
▪ Byte ordering not an issue
Deciphering Numbers
▪ Value: 0x12ab
▪ Pad to 32 bits: 0x000012ab
▪ Split into bytes: 00 00 12 ab
▪ Reverse: ab 12 00 00
Summary
Representing information as bits
Bit-level manipulations
Integers
▪ Representation: unsigned and signed
▪ Conversion, casting
▪ Expanding, truncating
▪ Addition, negation, multiplication, shifting
Representations in memory, pointers, strings
Summary
Integer C Puzzles
x < 0 ((x*2) < 0)
ux >= 0
x & 7 == 7 (x<<30) < 0
ux > -1
x > y -x < -y
x * x >= 0
Initialization x > 0 && y > 0 x + y > 0
x >= 0 -x <= 0
int x = foo();
x <= 0 -x >= 0
int y = bar(); (x|-x)>>31 == -1
unsigned ux = x; ux >> 3 == ux/8
unsigned uy = y; x >> 3 == x/8
x & (x-1) != 0