class02_cs230s22
class02_cs230s22
CS230 S’22
class02.ppt
–2– CS230 S’21
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…
0.2V
0.0V
CS230 S’22
For example, can count in binary
Base 2 Number Representation
Represent 1521310 as 111011011011012
Represent 1.2010 as 1.0011001100110011[0011]…2
Represent 1.5213 X 104 as 1.11011011011012 X 213
CS230 S’22
Binary Adder Implementation
0001
+0101
0110
Half Adder Full Adder
A B S C
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
CS230 S’22
https://en.wikipedia.org/wiki/X86
Machine Words
CS230 S’22
Word-Oriented Memory
Organization 32-bit 64-bit Bytes Addr.
Words Words
0000
Addr
0001
Addresses Specify Byte =
0000
?? 0002
Locations Addr
0003
=
Address of first byte in 0000
?? 0004
word Addr
=
0005
Addresses of successive 0004
?? 0006
words differ by 4 (32-bit) or 0007
8 (64-bit) 0008
Addr
=
0009
0008
?? 0010
Addr
= 0011
0008
?? 0012
Addr
=
0013
0012
?? 0014
0015
CS230 S’22
Data Representations
Sizes of C Objects (in Bytes)
char 1 1 1
short 2 2 2
int 4 4 4
long 4 8 8
float 4 4 4
double 8 8 8
long double − − 10/16
pointer 4 8 8
CS230 S’22
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
CS230 S’22
General Boolean Algebras
Operate on Bit Vectors
Operations applied bitwise
CS230 S’22
Example: Representing &
Manipulating Sets
Representation
Width w bit vector represents subsets of {0, …, w–1}
aj = 1 if j ∈ A
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 }
CS230 S’22
Bit-Level Operations in C
Operations &, |, ~, ^ Available in C
Apply to any “integral” data type
long, int, short, char, unsigned
View arguments as bit vectors
Arguments applied bit-wise
CS230 S’22
Shift Operations
Left Shift: x << y Argument x 01100010
Shift bit-vector x left y positions
<< 3 00010000
» Throw away extra bits on left
Fill with 0’s on right Log. >> 2 00011000
Undefined Behavior
Shift amount < 0 or ≥ word size
CS230 S’22
Byte Ordering
How should bytes within multi-byte
word be ordered in memory? Liliput and Blefuscu
Conventions
Sun’s, Mac’s are “Big Endian” machines
Least significant byte has highest address
Alphas, PC’s are “Little Endian” machines
Least significant byte has lowest address
Note:
Alpha and PowerPC can run in either mode.
Byte ordering is determined when the chip
is powered up.
Problematic when transferring binary data over the network
between machines with different byte ordering.
CS230 S’22
Byte Ordering Example
Big Endian
Least significant byte has highest address
Little Endian
Least significant byte has lowest address
Example
Variable x has 4-byte representation 0x01234567
Address given by &x is 0x100
CS230 S’22
Representing Integers
X=[xw-1, xw-2,…,x0]
Unsigned Two’s Complement Sign
Bit
w−1 w−2
i w−1
B2U(X ) = ∑ xi ⋅2 B2T (X ) = − xw−1 ⋅2 + ∑ xi ⋅2 i
i=0 i=0
Sign Bit
For 2’s complement, most significant bit indicates sign
0 for nonnegative / 1 for negative
CS230 S’22
Two-complement Encoding Example
x = 15213: 00111011 01101101
y = -15213: 11000100 10010011
Other Values
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
CS230 S’22
Values for Different Word Sizes
W
8 16 32 64
UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615
TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807
TMin -128 -32,768 -2,147,483,648 -9,223,372,036,854,775,808
Observations C Programming
|TMin | = TMax + 1 #include <limits.h>
Asymmetric range Declares constants, e.g.,
UMax = 2 * TMax + 1 ULONG_MAX
LONG_MAX
LONG_MIN
Values platform specific
CS230 S’22
Unsigned & Signed Numeric Values
X B2U(X) B2T(X) Equivalence
0000 0 0 Same encodings for
0001 1 1 nonnegative values
0010 2 2
0011 3 3 Uniqueness
0100 4 4 Every bit pattern represents
0101 5 5 unique integer value
0110 6 6 Each representable integer
0111 7 7 has unique bit encoding
1000 8 –8
1001 9 –7 ⇒ Can Invert Mappings
1010 10 –6 U2B(x) = B2U-1(x)
1011 11 –5 Bit pattern for unsigned
1100 12 –4 integer
1101 13 –3
1110 14 –2
T2B(x) = B2T-1(x)
Bit pattern for two’s comp
1111 15 –1
integer CS230 S’22
Mapping Between Signed &
Unsigned
Two’s Complement Unsigned
T2U
x T2B B2U ux
X
w–1 0
ux + + + ••• + + +
x - + + ••• + + +
CS230 S’22
Conversion Visualized
2’s Comp. → Unsigned
UMax
Ordering Inversion
UMax – 1
Negative → Big Positive
TMax + 1 Unsigned
TMax TMax Range
2’s Complement 0 0
Range –1
–2
TMin
CS230 S’22
Signed vs. Unsigned in C
Constants
By default are considered to be signed integers
Unsigned if have “U” as suffix
0U, 4294967259U
Casting
Explicit casting between signed & unsigned same as U2T and
T2U
int tx, ty;
unsigned ux, uy;
tx = (int) ux;
uy = (unsigned) ty;
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 CS230 S’22
Sign Extension Example
short int x = 15213;
int ix = (int) x;
short int y = -15213;
int iy = (int) y;
CS230 S’22
Unsigned Addition
u •••
Operands: w bits
+ v •••
True Sum: w+1 bits u+v •••
CS230 S’22
Visualizing (Mathematical) Integer
Addition
Integer Addition Add4(u , v)
24
Forms planar 20
surface 16
14
12 12
8 10
8
4
0
6 v
4
0
2 2
4
u 6
8
10
12
0
14
CS230 S’22
Visualizing Unsigned Addition
If true sum ≥ 2w
UAdd4(u , v)
At most once
True Sum 16
14
2w+1
Overflow 12
10
8
2w 6 12
14
10
4
2
6
8
v
0 0 4
Modular Sum 0
2
4 2
u 6
8
10
12
0
14
CS230 S’22
Two’s Complement Addition
Operands: w bits u •••
+ v •••
True Sum: w+1 bits u+v •••
Discard Carry: w bits TAddw(u , v) •••
CS230 S’22
TAdd Overflow
Functionality True Sum
Treat remaining
bits as 2’s comp. 0 000…0 0 000…0
integer
1 011…1 –2w –1 100…0
1 000…0 NegOver
–2w
CS230 S’22
Visualizing 2’s Complement
Addition
NegOver
Values
4-bit two’s comp. TAdd4(u , v)
Range from -8 to +7
Wraps Around 8
If sum ≥ 2w–1 6
4
Becomes negative 2
At most once 0
60111 (7)
+ 0010 (2)
-2 4
If sum < –2w–1 -4 2
0
= (0)1001 (-7)
Becomes positive -6
-2
-8 -4 v
At most once -8
-6
-4 -6
-2
0 -8
u 2
4
6 PosOver
1000 (-8) 1000 (-8) 0111 (7)
+ 1110 (-2) + 1111 (-1) + CS230
0001 S’22
(1)
= (1)0110 (6) = (1)0111 (7) = (0)1000 (-8)