0% found this document useful (0 votes)
7 views37 pages

class02_cs230s22

The document covers the representation of information in computers using bits, including binary number representation, bit-level manipulations, and integer encoding. It discusses machine word sizes, data types in C, boolean algebra, and bitwise operations, along with examples of byte ordering and numeric ranges. The content is aimed at providing foundational knowledge for understanding how computers process and manipulate data at the bit level.

Uploaded by

Jeeho Choe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views37 pages

class02_cs230s22

The document covers the representation of information in computers using bits, including binary number representation, bit-level manipulations, and integer encoding. It discusses machine word sizes, data types in C, boolean algebra, and bitwise operations, along with examples of byte ordering and numeric ranges. The content is aimed at providing foundational knowledge for understanding how computers process and manipulate data at the bit level.

Uploaded by

Jeeho Choe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

KAIST - CS230

Bits and Bytes (1/2)


Spring, 2022
Topics
 Representing information as bits
 Bit-level manipulations
 Integers

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…

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
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

http://parts.nowcomponents.com/parts/6502?gclid=CLacq8- CS230 S’22


YqLYCFUIl4godJ2cABg CS230 S’22
Encoding Byte Values
Byte = 8 bits
 Binary 000000002 to 111111112
0 0 0000
 Decimal: 010 to 25510 1 1 0001
 Hexadecimal 0016 to FF16 2 2 0010
3 3 0011
 Base 16 number representation 4 4 0100
 Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’ 5 5 0101
6 6 0110
 Write FA1D37B16 in C as 7 7 0111
» 0xFA1D37B 8 8 1000
9 9 1001
» Or 0xfa1d37b A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111

CS230 S’22
https://en.wikipedia.org/wiki/X86

Machine Words

Machine Has “Word Size”


 Nominal size of integer-valued data
 Including addresses

 Most current machines are 32 bits (4 bytes)


 Limits addresses to 4GB
 Becoming too small for memory-intensive applications

 High-end systems are 64 bits (8 bytes)


 Potentially address ≈ 1.8 X 1019 bytes

 Machines support multiple data formats


 Fractions or multiples of word size
 Always integral number of bytes

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)

C Data Type Typical 32-bit Typical 64-bit x86-64

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

Not Exclusive-Or (Xor)


 ~A = 1 when A=0  A^B = 1 when either A=1 or B=1, but not both

CS230 S’22
General Boolean Algebras
Operate on Bit Vectors
 Operations applied bitwise

01101001 01101001 01101001


& 01010101 | 01010101 ^ 01010101 ~ 01010101
01000001
01000001 01111101
01111101 00111100
00111100 10101010
10101010

All of the Properties of Boolean Algebra Apply

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

Examples (Char data type)


 ~0x41 | 0xBE
 ~010000012 |101111102
 ~0x00 | 0xFF
 ~000000002 | 111111112
 0x69 & 0x55 | 0x41
 011010012 & 010101012 | 010000012
 0x69 | 0x55 | 0x7D
 011010012 | 010101012 | 011111012
CS230 S’22
Contrast: Logic Operations in C
Contrast to Logical Operators
 &&, ||, !
 View 0 as “False”
 Anything nonzero as “True”
 Always return 0 or 1 Watch out for && vs. & (and || vs. |)…
 Early termination one of the more common oopsies in
C programming
Examples (char data type)
 !0x41 && 0x00
 !0x00 && 0x01
 !!0x41 && 0x01

 0x69 && 0x55 || 0x01


 0x69 || 0x55 && 0x01
 p && *p (avoids null pointer access)

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

Right Shift: x >> y Arith. >> 2 00011000


 Shift bit-vector x right y positions
 Throw away extra bits on right Argument x 10100010
 Logical shift
<< 3 00010000
 Fill with 0’s on left
 Arithmetic shift Log. >> 2 00101000
 Replicate most significant bit on left Arith. >> 2 11101000

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

Big Endian 0x100 0x101 0x102 0x103


01 23 45 67

Little Endian 0x100 0x101 0x102 0x103


67 45 23 01

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

X=[0,0,…,0 – 1,1,…1] X=[1,0,…,0 – 0,1,…1]


X=[0– 255] when w=8 X=[-128 – 127] when w=8

C short 2 bytes long


short int x = 5;
short int y = -5;

Decimal Hex Binary


x 5 05 00000101
y -5 FB 11111011

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

Weight 15213 -15213


1 1 1 1 1
2 0 0 1 2
4 1 4 0 0
8 1 8 0 0
16 0 0 1 16
32 1 32 0 0
64 1 64 0 0
128 0 0 1 128
256 1 256 0 0
512 1 512 0 0
1024 0 0 1 1024
2048 1 2048 0 0
4096 1 4096 0 0
8192 1 8192 0 0
16384 0 0 1 16384
-32768 0 0 1 -32768
CS230 S’22
Sum 15213 -15213
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

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

Maintain Same Bit Pattern

Unsigned Two’s Complement


U2T
ux U2B X B2T x

Maintain Same Bit Pattern

Mappings between unsigned and two’s complement


numbers:
Keep bit representations and reinterpret CS230 S’22
Mapping Signed ↔ Unsigned
Bits Signed Unsigned
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 T2U 5
0110 6 6
U2T
0111 7 7
1000 -8 8
1001 -7 9
1010 -6 10
1011 -5 11
1100 -4 12
1101 -3 13
1110 -2 14
1111 -1 15CS230 S’22
Mapping Signed ↔ Unsigned
Bits Signed Unsigned
0000 0 0
0001 1 1
0010 2 2
0011
0100
3
4
= 3
4
0101 5 5
0110 6 6
0111 7 7
1000 -8 8
1001 -7 9
1010 -6 10
+/- 16
1011 -5 11
1100 -4 12
1101 -3 13
1110 -2 14
1111 -1 15
CS230 S’22
Relation between Signed & Unsigned

Two’s Complement Unsigned


T2U
x T2B B2U ux
X

Maintain Same Bit Pattern

w–1 0
ux + + + ••• + + +
x - + + ••• + + +

Large negative weight


becomes
Large positive weight

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;

 Implicit casting also occurs via assignments and procedure calls


tx = ux;
uy = ty; CS230 S’22
Casting Surprises
Expression Evaluation
 Ifthere 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 0U0U == unsigned
-1 -1 00 < signed
-1 -1 0U0U > unsigned
2147483647
2147483647 -2147483648
-2147483647-1 > signed
2147483647U
2147483647U -2147483648
-2147483647-1 < 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
CS230 S’22
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 CS230 S’22
Sign Extension Example
short int x = 15213;
int ix = (int) x;
short int y = -15213;
int iy = (int) y;

Decimal Hex Binary


x 15213 3B 6D 00111011 01101101
ix 15213 00 00 3B 6D 00000000 00000000 00111011 01101101
y -15213 C4 93 11000100 10010011
iy -15213 FF FF C4 93 11111111 11111111 11000100 10010011

Converting from smaller to larger integer data type


C automatically performs sign extension

CS230 S’22
Unsigned Addition
u •••
Operands: w bits
+ v •••
True Sum: w+1 bits u+v •••

Discard Carry: w bits UAddw(u , v) •••

Standard Addition Function


 Ignores carry output

Implements Modular Arithmetic


s = UAddw(u , v) = u + v mod 2w

CS230 S’22
Visualizing (Mathematical) Integer
Addition
Integer Addition Add4(u , v)

 4-bit integers u, v Integer Addition

 Compute true sum


Add4(u , v)
 Values increase 32

linearly with u and v 28

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

Wraps Around Overflow

 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) •••

TAdd and UAdd have Identical Bit-Level Behavior


 Signed vs. unsigned addition in C:
int s, t, u, v;
s = (int) ((unsigned) u + (unsigned) v);
t = u + v
 Will give s == t

CS230 S’22
TAdd Overflow
Functionality True Sum

 True sum requires 0 111…1 2w–1


PosOver
w+1 bits TAdd Result
 Drop off MSB 0 100…0 2w –1–1 011…1

 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)

You might also like