0% found this document useful (0 votes)
36 views

Lecture 3 - Bits, Bytes, and Integers

Uploaded by

splendiferousbee
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)
36 views

Lecture 3 - Bits, Bytes, and Integers

Uploaded by

splendiferousbee
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/ 78

Carnegie Mellon

198:331 Intro to Computer Organization Lecture 3

Bits, Bytes, and Integers

Lecture 3
198:331 Introduction to Computer Organization

Instructor:
Michael A. Palis
[email protected]

1
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Lecture Outline
 Representing information as bits
 Bit-level manipulations
 Integers
● Representation: unsigned and signed
● Conversion, casting
● Expanding, truncating
● Addition, subtraction, multiplication, division
 Character Data

2
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

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
3
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Data Representation
 Data represented as sequences of bits but require different sizes
 Sizes are multiples of 1 byte = 8 bits
 Size of representation (in bytes) depends on machine and compiler

Typical 32-bit Typical 64-bit


C data type x86-64
machine machine
char 1 1 1
short int 2 2 2
int 4 4 4
long int 4 8 8
pointer 4 8 8
float 4 4 4
double 8 8 8
long double − − 10/16

4
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Number Systems
 Positional base b number system
● Base (or radix) b number is written as (dn-1 dn-2 … d1 d0)b
● Each digit di ∈ {0, 1, ..., b-1}
● Decimal value of (dn-1 dn-2 … d1 d0)b is

𝑛−1
෍ 𝑑𝑖 × 𝑏𝑖
𝑖=0

● Leftmost digit dn-1 is referred to as the most significant digit


because it carries the largest weight (bn-1)
● Similarly, rightmost digit d0 is the least significant digit

5
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Commonly Used Bases


 Base 10 or decimal
● Each digit di ∈ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
● Example: 374510 = (3×103) + (7×102) + (4×101) + (5×100)
● C declaration: unsigned int x = 3745;

 Base 8 or octal
● Each octal digit di ∈ {0, 1, 2, 3, 4, 5, 6, 7}
● Example: 72168 = (7×83) + (2×82) + (1×81) + (6×80) = 372610
● C declaration: unsigned int y = 07216; // prepend a zero

 Base 16 or hexadecimal
● Each hex digit di ∈ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}
where: A = 10, B = 11, C = 12, D = 13, E = 14, F = 15
● Example: 2F3A16 = (2×163) + (15×162) + (3×161) + (10×160) = 12,09010
● C declaration: unsigned int z = 0x2F3A; // prepend “zero-x”

6
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Binary Numbers
 Base 2 or binary
● Example: 101102 = (1×24) + (0×23) + (1×22) + (1×21) + (0×20) = 2210
● Regardless of the base in which a number is expressed at the
program level, it is represented at the machine level in base 2 or
binary – as a sequence of bits (0’s and 1’s)
● Sorry, C does not allow writing integer constants in binary!

 How, then, do you convert from base b to binary?


● unsigned int x = 43; // decimal
● unsigned int y = 07206; // octal
● unsigned int z = 0x2F3A; // hexadecimal
What are the binary representations of the above numbers (assuming
they are unsigned, i.e., non-negative) integers?

7
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Converting Decimal to Binary


 Algorithm
1. Divide decimal number N by 2 producing a quotient Q and a remainder R.
2. If Q ≠ 0, set N to Q and repeat 1.
3. Read off sequence of remainders in reverse order; the result is the base 2
equivalent of the decimal number.
 Example: 4310 = ?2
Q R
43 ÷ 2 = 21 1
21 ÷ 2 = 10 1
10 ÷ 2 = 5 0 Read off remainders
5÷2= 2 1 in reverse order
2÷2= 1 0
4310 = 1010112
1÷2= 0 1

Stop when quotient = 0


8
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Converting Between Hex and Binary


 Hex to Binary
● Convert each hex digit to its 4-bit binary equivalent
● Concatenate the resulting binary strings
● Example: 2AF816 = 0010 1010 1111 10002
= 101010111110002 (drop leading 0’s)
 Binary to Hex
● Group binary number into 4-bit blocks from right to left (append
0’s to left if necessary)
● Convert each 4-bit block into its hex equivalent
● Example: 101010111110002 ➜ 0010 1010 1111 1000 = 2AF816

2 A F 8

 How about Octal ⬌Binary? Group into 3-bit blocks (exercise)


9
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Lecture Outline
 Representing information as bits
 Bit-level manipulations
 Integers
● Representation: unsigned and signed
● Conversion, casting
● Expanding, truncating
● Addition, subtraction, multiplication, division
 Character Data

10
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Boolean Algebra
 Developed by George Boole in 19th Century
● Algebraic representation of logic
► Encode “TRUE” as 1 and “FALSE” as 0

 Boolean Operations
● NOT, AND, OR, Exclusive-OR (in C, ~, &, | and ^, respectively)

NOT AND OR Exclusive OR (XOR)

~A = 1 A&B = 1 when A|B = 1 when A^B = 1 when either


when A=0 both A=1 and B=1 either A=1 or B=1 A=1 or B=1, but not both

11
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Operations on Bit Vectors


 Boolean operations can be applied to bit vectors
● Operations applied bitwise

01101001 01101001 01101001


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

12
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Example: Sets as Bit Vectors


 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 }
13
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

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 bitwise
 Examples (Char data type)
● ~0x41 ➙ 0xBE
► ~010000012 ➙ 101111102

● 0x69 & 0x55 ➙ 0x41


► 011010012 & 010101012 ➙ 010000012

● 0x69 | 0x55 ➙ 0x7D


► 011010012 | 010101012 ➙ 011111012

● 0x69 ^ 0x55 ➙ 0x3C


► 011010012 ^ 010101012 ➙ 001111002

14
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Contrast: Logic Operations in C


 C Logical Operators
● Logical AND (&&), Logical OR (||), Logical NOT (!)
● View 0 as FALSE, anything nonzero as TRUE
● Always return 0 or 1 Watch out for && vs. &
(and || vs. |)…
 Examples (char data type) one of the more
common oopsies in
● !0x41 ➙ 0x00 C programming
● !0x00 ➙ 0x01
● 0x00 && 0x55 ➙ 0x00
● 0x00 || 0x55 ➙ 0x01
 Second argument not evaluated if result of expression can
be determined from first argument
● a && 5/a (will not cause a division by zero)
● p && *p (will not cause dereferencing a null pointer)

15
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

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
● Logical shift << 3 00010000
► Fill with 0’s on left
Log. >> 2 00101000
● Arithmetic shift
► Replicate most significant bit on left Arith. >> 2 11101000

 Undefined Behavior
● Shift amount < 0 or ≥ word size

16
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Lecture Outline
 Representing information as bits
 Bit-level manipulations
 Integers
● Representation: unsigned and signed
● Conversion, casting
● Expanding, truncating
● Addition, subtraction, multiplication, division
 Character Data

17
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Binary Representation of Integers


 Unsigned integers
● Positive integers and zero: {0, 1, 2, …}
● Example C declaration: unsigned int x = 43;
● Binary representation using w bits
► Convert to binary

► If less than w bits, left-fill with zeros

► If more than w bits, then “overflow”: integer cannot be represented

using w bits
● Example: unsigned int x = 43;
► 4310 = 1010112
► In C, unsigned int is 32 bits (4 bytes)
► 32-bit representation

00000000000000000000000000101011 = 0x0000002B

left-fill with zeros


18
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Binary Representation of Integers


 Signed integers
● Entire set of integers (positive, negative, zero)
● That is, integers from the set {…, -2, -1, 0, 1, 2, …}
● Example C declaration
int x = -43; // drop keyword “unsigned”

 Binary representation
● Sign-magnitude
● Ones’ complement
● Two’s complement – dominantly used today

19
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Binary Representation of Signed Integers


 Representation in Sign-Magnitude
● Leftmost bit = sign bit: 0 means positive, 1 means negative
● Remaining bits = magnitude: binary representation of the number
 Example
● int x = 43, y = -43;
► For both x and y: 4310 = 1010112 ➙ magnitude = 101011
► Sign bit: 0 for x, 1 for y
► 32-bit representation

x 00000000000000000000000000101011 = 0x0000002B

y 10000000000000000000000000101011 = 0x8000002B

magnitude
sign bit
(left-fill with zeros if needed)

20
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Binary Representation of Signed Integers


 Representation in Ones’ Complement
● Ones’ complement bitwise operation (equivalent to “~” in C):
► Complement each bit: change 0 to 1; change 1 to 0
► Example: ones’-complement(1011) = 0100
● Ones’ complement representation
► Positive integers: straight binary with a 0 in the leftmost bit position

► Negative integers: take the ones’ complement of the corresponding

positive integer
 Example
● int x = 43, y = -43;
● 32-bit representation
x 00000000000000000000000000101011 = 0x0000002B

y 11111111111111111111111111010100 = 0xFFFFFFD4

21
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Binary Representation of Signed Integers


 Representation in Two’s Complement
● Two’s complement bitwise operation:
► Take the ones’ complement, then add 1 to the result

► Example: two’s-complement(1011) = 0100 + 1 = 0101

● Two’s complement representation


► Positive integers: straight binary with a 0 in the leftmost bit position

► Negative integers: take the two’s complement of the corresponding

positive integer
 Example
● int x = 43, y = -43;
● 32-bit machine representation
x 00000000000000000000000000101011 = 0x0000002B

y 11111111111111111111111111010101 = 0xFFFFFFD5

22
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Binary Representation of Signed Integers


 Common features of three representations
● Positive integers have the same binary representation
● Negation (i.e., taking the negative of a number) is well-defined:
► Sign-magnitude: –x is defined as flipping the sign bit of x

► Ones’ complement: –x is defined as ones’-complement(x)

► Two’s complement: –x is defined as two’s-complement(x)

● Leftmost bit indicates sign: 0 means positive, 1 means negative

 Advantage of two’s complement representation


● In two’s complement, every integer has a unique representation
(bijection)
● Two’s complement arithmetic (add, multiply, etc.) is as easy as
unsigned arithmetic – can use the same arithmetic hardware
● Sign-magnitude and ones’ complement do not have the above
properties

23
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Binary Representation of Integers for n = 4


Sign- Ones' Two's
Binary Unsigned
magnitude complement complement

0000 0 0 0 0 Sign-magnitude
0001 1 1 1 1 and ones’
0010 2 2 2 2 complement
0011 3 3 3 3 each have two
0100 4 4 4 4 distinct
0101 5 5 5 5 representations for
0110 6 6 6 6 zero
0111 7 7 7 7
1000 8 −0 −7 −8
1001 9 −1 −6 −7
1010 10 −2 −5 −6
1011 11 −3 −4 −5
1100 12 −4 −3 −4
1101 13 −5 −2 −3
1110 14 −6 −1 −2
1111 15 −7 −0 −1

24
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Binary Representation of Integers for n = 4


Sign- Ones' Two's
Binary Unsigned
magnitude complement complement

0000 0 0 0 0 In two’s
0001 1 1 1 1 complement,
0010 2 2 2 2 every binary
0011 3 3 3 3 pattern represents
0100 4 4 4 4 a unique integer
0101 5 5 5 5 value
0110 6 6 6 6
… a property it
0111 7 7 7 7
shares with
1000 8 −0 −7 −8
unsigned
1001 9 −1 −6 −7 representation
1010 10 −2 −5 −6
1011 11 −3 −4 −5 Unsigned and
1100 12 −4 −3 −4 two’s complement
1101 13 −5 −2 −3 arithmetic can use
1110 14 −6 −1 −2 the same
hardware
1111 15 −7 −0 −1

25
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Properties of Integer Encodings


 Given a bit vector 𝑋 = 𝑥𝑤−1 , 𝑥𝑤−2 , … , 𝑥0 , what is its decimal
value if it encodes:
● an unsigned integer?
● a signed integer in two’s complement form?
 Answer:
● As an unsigned integer, the decimal value is given by:
𝑤−1
𝐵2𝑈 𝑋 = ෍ 𝑥𝑖 ∙ 2𝑖
𝑖=0
● As a signed integer in two’s complement form, the decimal value is
given by:
𝑤−2
𝐵2𝑇 𝑋 = −𝑥𝑤−1 ∙ 2𝑤−1 + ෍ 𝑥𝑖 ∙ 2𝑖
𝑖=0

26
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Properties of Integer Encodings


Unsigned Signed Two’s Complement
𝑤−1 𝑤−2
𝐵2𝑈 𝑋 = ෍ 𝑥𝑖 ∙ 2𝑖 𝐵2𝑇 𝑋 = −𝑥𝑤−1 ∙ 2𝑤−1 + ෍ 𝑥𝑖 ∙ 2𝑖
𝑖=0 𝑖=0

 Example sign bit


𝑿= 𝟏𝟎𝟏𝟏
Weights in
Unsigned 23 22 21 20 B2U(1011) = 1∙23 + 0∙22 + 1∙21 + 1∙20 = 1110

Weights in
−23 22 21 20 B2T(1011) = 1∙(−23) + 0∙22 + 1∙21 + 1∙20 = −510
Signed

Note: if sign bit = 1, it contributes


−2w-1 to the signed integer value

27
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Properties of Integer Encodings


B2U(X) B2T(X)
X (Unsigned) (Signed 2’s
 Equivalence
Complement) ● Same encodings for nonnegative
0000 0 0 values
0001 1 1
 Uniqueness
0010 2 2
0011 3 3 ● Every bit pattern represents unique
0100 4 4 integer value
0101 5 5 ● Each representable integer has
0110 6 6 unique bit encoding
0111 7 7   Can Invert Mappings
1000 8 −8
● U2B(X) = B2U-1(X)
1001 9 −7
1010 10 −6
►Bit pattern for unsigned integer
1011 11 −5 ● T2B(X) = B2T-1(X)
1100 12 −4 ► Bit pattern for signed 2’s comp.

1101 13 −3 integer
1110 14 −2 ● Discussed earlier (decimal to binary
1111 15 −1 conversion)

28
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Numeric Ranges
 Unsigned  Signed 2’s Complement
● 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 FF FF 11111111 11111111
0 00 00 00000000 00000000

29
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

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

30
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Lecture Outline
 Representing information as bits
 Bit-level manipulations
 Integers
● Representation: unsigned and signed
● Conversion, casting
● Expanding, truncating
● Addition, subtraction, multiplication, division
 Character Data

31
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Mapping Between Signed and Unsigned

2’s Complement T2U Unsigned


x T2B B2U u
X
Maintain Same Bit Pattern

Unsigned U2T 2’s Complement


u U2B B2T x
X
Maintain Same Bit Pattern

 Mappings between unsigned and two’s complement numbers:


Keep bit representations and reinterpret
32
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Mapping Between Signed and Unsigned


Signed 2’s
Binary Unsigned
Complement

0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 T2U 5
0110 6 6
0111 7 U2T 7
1000 −8 8
1001 −7 9
1010 −6 10
1011 −5 11
1100 −4 12
1101 −3 13
1110 −2 14
1111 −1 15

33
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Mapping Between Signed and Unsigned


Signed 2’s
Binary Unsigned
Complement

0000 0 0
0001 1 1
0010 2 2
0011 3 = 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 −8 8
1001 −7 9
1010 −6
+/- 16 10
1011 −5 11
1100 −4 12
1101 −3 13
1110 −2 14
1111 −1 15

34
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Relation Between Signed and Unsigned


2’s Complement T2U Unsigned
x T2B B2U u
X
Maintain Same Bit Pattern
w–1 0
x 1 ++ ••• +++
If sign(x) = 1, effect is
to add 2w to obtain u

u +++ ••• +++

𝑥 + 2𝑤 , 𝑥<0
𝑇2𝑈 𝑥 = ቊ
𝑥, 𝑥≥0

35
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Relation Between Signed and Unsigned


Unsigned U2T 2’s Complement
u U2B B2T x
X
Maintain Same Bit Pattern
w–1 0
u 1 ++ ••• +++
If msb(u) = 1, effect is
to subtract 2w to obtain x

x −++ ••• +++

𝑢, 𝑢 < 2𝑤−1
𝑈2𝑇 𝑢 = ቊ
𝑢 − 2𝑤 , 𝑢 ≥ 2𝑤−1

36
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Signed vs. Unsigned in C


 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;
● In expressions containing signed and unsigned, signed values are
implicitly cast to unsigned (!)
uy = ux + ty;
● Can have unexpected effects

37
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Casting Example
 C Program
void main()
{
int tx, ty = -16;
unsigned int ux = 2, uy;

uy = ux + ty; // ty converted to unsigned then added to ux


tx = uy; // uy converted to signed and assigned to tx

printf("tx = %8x (hex) = %i (dec)\n", tx, tx);


printf("ty = %8x (hex) = %i (dec)\n", ty, ty);
printf("ux = %8x (hex) = %u (dec)\n", ux, ux);
printf("uy = %8x (hex) = %u (dec)\n", uy, uy);
}

 Output
tx = fffffff2 (hex) = -14 (dec)
ty = fffffff0 (hex) = -16 (dec)
ux = 2 (hex) = 2 (dec)
uy = fffffff2 (hex) = 4294967282 (dec)

38
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Lecture Outline
 Representing information as bits
 Bit-level manipulations
 Integers
● Representation: unsigned and signed
● Conversion, casting
● Expanding, truncating
● Addition, subtraction, multiplication, division
 Character Data

39
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Expanding Integers
 Given a w-bit integer x, convert it to a w’-bit integer with
same value, where w’ > w

 Unsigned Integer 11011 = 2710


● Zero-extension
► Append 0’s to left
00000 11011 = 2710

 Signed Integer 11011 = −1110


● Sign-extension
► Copy sign bit (msb) to left
11111 11011 = −1110

40
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Truncating Integers
 Given w-bit integer x, convert it to a k-bit integer, k < w
● Retain k rightmost bits and delete the rest
● Does not guarantee that value is preserved
 Unsigned Integer
10010 01011 = 58710
● Truncation could result in overflow
► 587 not representable with 5 bits

● Result is actually x mod 2k 01011 = 1110


► 11 = 587 mod 2
5

 Signed Integer
● Truncation could result in overflow 10010 01011 = −43710
change in sign
● Result is actually U2T(x mod 2k) 01011 = 1110

41
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

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 80 00 11000100 10010011
iy -15213 FF FF 11111111 11111111 11000100 10010011

 Converting from smaller to larger data type


● C automatically performs zero-extension for unsigned and sign-
extension for signed

42
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Expanding, Contracting: Basic Rules


 Expanding (e.g., short int to int)
● Unsigned: zeros added
● Signed: sign extension
● Both yield expected result

 Truncating (e.g., unsigned to unsigned short)


● Unsigned/signed: bits are truncated
● Result reinterpreted
● Unsigned: mod operation
● Signed: similar to mod
● For small numbers yields expected behavior

43
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Lecture Outline
 Representing information as bits
 Bit-level manipulations
 Integers
● Representation: unsigned and signed
● Conversion, casting
● Expanding, truncating
● Addition, subtraction, multiplication, division
 Character Data

44
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Unsigned Addition
 Recall addition in decimal
1 1 1 carry digits

4 9 7
+ 9 2 8
1 4 2 5 sum digits

 Similarly in binary
carry bits 1 1 1 1 1 (decimal)
0 1 1 0 1 1 = 2 7
+ 0 0 1 1 0 1 = 1 3
sum bits 1 0 1 0 0 0 4 0

45
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Unsigned Addition
 Unsigned Overflow
● Adding two w-bit unsigned integers may produce a result that
requires more than w bits. This is called overflow
● For unsigned addition, overflow occurs when there is a carry out of
the most significant bit position

1 1 1

0 1 1 0 1 1 = 2 7
+ 1 0 1 0 0 0 = 4 0
True sum: w+1 bits 1 0 0 0 0 1 1 6 7
Sum stored: w bits 0 0 0 0 1 1 3

Carry out of msb indicates


unsigned overflow

46
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Unsigned Addition
Operands: w bits u •••
+v •••
True Sum: w+1 bits
u+v •••
Discard Carry: w bits UAddw(u , v) •••

 C addition operator ( e.g., a + b )


● Ignores carry out – just keeps w bits of sum
● Implements modular arithmetic
► UAddw(u , v) = (u + v) mod 2
w

 Hardware sets a flag (CF) when a carry out is generated


● But it’s up to the program to check
● Compilers (e.g., gcc) provide built-in functions to check for overflow but
program has to call them

47
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Signed 2’s Complement Addition


Operands: w bits u •••
+ v •••
True Sum: w+1 bits
u+v •••
Discard Carry: w bits TAddw(u , v) •••

 Same C addition operator as unsigned ( + )


● Identical bit-level behavior as unsigned addition: carry out of msb
is also discarded
discard 1 1
110101 = -11
+011010 = 26
001111 15

● However, overflow condition is different


48
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Signed 2’s Complement Addition


 Signed Overflow
● Overflow occurs when the two operands have the same sign and
the result has the opposite sign (sign reversal)
1 11 1 1 1
00 1 0 1 1 = 11 110101 = -11
+ 01 1 0 1 0 = 26 +100110 = -26
10 0 1 0 1 ≠ 37 011011 ≠ -37

Sign reversal indicates


signed overflow

● Overflow cannot occur when operands have opposite sign


► Let a and b have opposite sign, and WLOG a < b. Then a ≤ a + b ≤ b

► Since a and b are each representable using w bits, then so is any

number in between

49
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Subtraction
 Signed Subtraction
● Reduces to signed addition since A – B ≡ A + (–B)
● Therefore, to compute A – B:
► Compute –B = two’s complement(B)

► Add –B to A

 Unsigned Subtraction
● Uses same algorithm except first check that A ≥ B
► If A < B result will be negative but will be interpreted as a (large)
unsigned number

 C subtraction operator ( e.g., a - b )


● Used for both signed and unsigned

50
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Lecture Outline
 Representing information as bits
 Bit-level manipulations
 Integers
● Representation: unsigned and signed
● Conversion, casting
● Expanding, truncating
● Addition, subtraction, multiplication, division
 Character Data

51
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Unsigned Multiplication
 Recall multiplication in decimal

1 3 5 multiplicand
× 4 2 3 multiplier
4 0 5
2 7 0 partial products

5 4 0
5 7 1 0 5 product

52
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Unsigned Multiplication
 Similarly in binary
1 1 0 1 = 1310
× 0 1 0 1 = 510
1 1 0 1
0 0 0 0
1 1 0 1
0 0 0 0
1 0 0 0 0 0 1 = 6510

● The product of two w-bit numbers results can be as long as 2w bits


► No overflow can occur so long as the product is stored using 2w bits
● More about this later

53
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Unsigned Multiplication
 Observe that each partial product is simply either:
● multiplicand shifted left i times (if i-th multiplier bit = 1), or
● zero (if multiplier bit = 0)

1 1 0 1
× 0 1 0 1
1 1 0 1 (multiplicand × 1) shift left 0
0 0 0 0 (multiplicand × 0) shift left 1
1 1 0 1 (multiplicand × 1) shift left 2
0 0 0 0 (multiplicand × 0) shift left 3
1 0 0 0 0 0 1

54
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Unsigned Multiplication
 Unsigned Multiplication Algorithm

1. Initialize 2w-bit product to 0


2. For i = 0 to w-1 do the following:
a. If i-th bit of multiplier = 1, add multiplicand to product
b. Left shift multiplicand left by 1 bit
3. Product holds multiplicand × multiplier

55
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Unsigned Multiplication in C
u •••
Operands: w bits
* v •••
True Product: 2*w bits u ·v ••• •••
UMultw(u , v) •••
Discard w bits: w bits

 C multiplication operator ( e.g., a * b )


● Ignores high order w bits – up to programmer to check for
overflow
● Implements modular arithmetic: UMultw(u, v) = (u · v) mod 2w
 x86-64
● Has unsigned multiply instruction that computes and stores entire
2w bits of product
● But you need to write your own routine to utilize this instruction

56
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Signed Multiplication
 If binary numbers represent 2’s complement signed integers,
algorithm doesn’t work

1 1 0 1 = -310
× 0 1 0 1 = 510
1 1 0 1
0 0 0 0
1 1 0 1
0 0 0 0
1 0 0 0 0 0 1 ≠ -1510

 In the example above, product is two’s complement


representation of -6310 which is the wrong result
● Correct result is −1510

57
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Signed Multiplication
 Why doesn’t the algorithm work?
1 1 0 1 = -310
× 0 1 0 1 = 510
This is a positive
number (+1310 )! 0 0 0 1 1 0 1
0 0 0 0 0 0
0 1 1 0 1
0 0 0 0
1 0 0 0 0 0 1 ≠ -1510

 Because, implicitly, the partial products formed from the shifted


multiplicand are positive numbers even though the multiplicand is
a negative number

58
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Signed Multiplication
 To correct the problem, the multiplicand should be sign-
extended to 2w bits before forming partial products.
1 1 1 1 1 1 0 1 = -310

Sign-extend × 0 1 0 1 = 510
1 1 1 1 1 1 0 1
0 0 0 0 0 0 0
1 1 1 1 0 1
0 0 0 0 0
1 1 1 1 0 0 0 1 = -1510

● Carry out of the (2w)-th bit is discarded in each addition because we’re
performing two’s complement addition. (That is, we keep only 2w least
significant bits of product.)

59
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Signed Multiplication
 Unfortunately, the tweak does not work if the multiplier is
negative:
1 1 1 1 1 1 0 1 = -310
× 1 0 1 1 = -510
1 1 1 1 1 1 0 1
1 1 1 1 1 0 1
0 0 0 0 0 0
1 1 1 0 1
1 1 0 1 1 1 1 1 ≠ 1510

 Why? Because the bits of the multiplier no longer specify


appropriately shifted copies of the multiplicand.

60
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Signed Multiplication
 Dealing with a negative multiplier
● Let A = multiplicand and B = multiplier
● Rewrite the multiplier as B = ±B* where B* is the magnitude of B
● If B= +B* (i.e., B is positive) then the multiplication algorithm gives the
correct product because A × B = A × B*
● If B = –B* (i.e., B is negative) the multiplication algorithm gives the
incorrect product because it actually computes:
A × two’s complement(B*) = A × (2w – B*) = (A × 2w) – (A × B*)
● If B = –B* the product should be A × B = A × (– B*) = – (A × B*)
 Key Observations
● Subtracting (A × 2w) from (A × 2w) – (A × B*) yields – (A × B*) which is
the correct product
● (A × 2w) is equal to A shifted to the left w times
● Subtracting (A × 2w) is same as adding two’s complement of (A × 2n)

61
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Signed Multiplication
 Signed Two’s Complement Multiplication Algorithm
1. Set-up:
a. Initialize 2w-bit product to 0
b. Sign-extend multiplicand to 2w bits
2. For i = 0 to w-1 do the following:
a. If i-th bit of multiplier = 1, add multiplicand to product
b. Left shift multiplicand left by 1 bit
Only step added to
3. If MSB of multiplier = 1, add two’s complement unsigned multiplication
of multiplicand to product algorithm. ➞ Can
merge two algorithms
4. Product holds multiplicand × multiplier into a single one that
works for both signed
and unsigned.

62
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Signed Multiplication
 Algorithm Illustration:

1 1 1 1 1 1 0 1 = -310
After shifting left,
multiplicand = 11010000. × 1 0 1 1 = -510
Since MSB(multiplier) = 1: 1 1 1 1 1 1 0 1
-Take two’s complement 1 1 1 1 1 0 1 0
of multiplicand
11010000 → 00110000 0 0 0 0 0 0 0 0
- Add it to product 1 1 1 0 1 0 0 0
0 0 1 1 0 0 0 0
0 0 0 0 1 1 1 1 = 1510

63
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Signed Multiplication in C
u •••
Operands: w bits
* v •••
True Product: 2*w bits u ·v ••• •••
TMultw(u , v) •••
Discard w bits: w bits

 Same C multiplication operator as unsigned ( * )


● As in unsigned multiplication ignores high order w bits – up to
programmer to check for overflow
 x86-64
● Has signed multiply instruction that computes and stores entire 2w
bits of product
● But you need to write your own routine to utilize this instruction

64
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Power-of-2 Multiply with Shift


 Shift Operation
● u << k gives u * 2k for both signed and unsigned
● Example (w = 8)
► Unsigned

u = 000010112 (1110) u << 3 = 010110002 (8810 = 1110 × 23)


► Signed
v = 111101102 (−1010) u << 2 = 110110002 (−4010 = −1010 × 22)

 Multiplying by a constant 4010 = 0 0 1 0 1 0 0 0


● u * 40 = (u * 8)+ (u * 32)= (u * 23)+ (u * 25)
● u * 40 =(u << 3)+(u << 5) 25 23
● In most machines, shift and add are faster than multiply
► Compiler generates this code automatically

● See textbook for a more general treatment

65
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Lecture Outline
 Representing information as bits
 Bit-level manipulations
 Integers
● Representation: unsigned and signed
● Conversion, casting
● Expanding, truncating
● Addition, subtraction, multiplication, division
 Character Data

66
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Unsigned Integer Division


 Recall unsigned division in decimal:

2 5 quotient
divisor 1 4 3 5 4
- 2 8 dividend
7 4
- 7 0
4 remainder

 Satisfies the following property:

dividend = (quotient × divisor) + remainder

67
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Unsigned Integer Division


 Similarly in binary:

0 1 1 1 = 710
610 = 1 1 0 1 0 1 1 1 0 = 4610
- 1 1 0
1 0 1 1
Check:
- 1 1 0 46 = 7×6 + 4
1 0 1 0
- 1 1 0
1 0 0 = 410

68
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Unsigned Integer Division


 Unsigned division algorithm
1. Set-up:
a. Q = 0; R = dividend
b. D = divisor left shifted (LSB zero-filled) until same number of bits as
dividend
2. If R < divisor, stop: Q contains quotient; R contains remainder
3. If R < D then left shift Q; fill LSB with 0
4. If R ≥ D then left shift Q; fill LSB with 1; R = R – D
5. Right shift D; fill MSB with 0
6. Go to 2

69
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Unsigned Integer Division


 Algorithm Illustration:
dividend = 101110 R 1 0 1 1 1 0

divisor = 110 D 1 1 0 0 0 0 R < D Q 0 0 0 0

R 1 0 1 1 1 0
D 0 1 1 0 0 0 R ≥ D Q 0 0 0 1

R 0 1 0 1 1 0
D 0 0 1 1 0 0 R ≥ D Q 0 0 1 1

R 0 0 1 0 1 0
D 0 0 0 1 1 0 R ≥ D Q 0 1 1 1

R <
R 0 0 0 1 0 0 Q 0 1 1 1
divisor

remainder quotient

70
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Signed Integer Division


 No simple direct way; reduce to unsigned division
 Algorithm:
1. Take absolute values of dividend and divisor and apply unsigned division
algorithm
2. Attach the proper signs to quotient and remainder:
+ if sign(dividend) = sign(divisor)
sign(quotient) =
− if sign(dividend) ≠ sign(divisor)

sign(remainder) = sign(dividend)

 Above sign rule ensures that:

dividend = (quotient × divisor) + remainder

71
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Signed Integer Division


 Illustration of sign rule
1. (+7) ÷ (+3) = ?
2. (−7) ÷ (+3) = ?
3. (+7) ÷ (−3) = ?
4. (− 7) ÷ (−3) = ?

Unsigned division yields: 7 ÷ 3 = 2 rem. 1

Therefore: Check: dividend = quot. × divisor + rem.


1. (+7) ÷ (+3) = (+2) rem. (+1) (+7) = (+2) × (+3) + (+1)
2. (−7) ÷ (+3) = (−2) rem. (−1) (−7) = (−2) × (+3) + (−1)
3. (+7) ÷ (−3) = (−2) rem. (+1) (+7) = (−2) × (−3) + (+1)
4. (−7) ÷ (−3) = (+2) rem. (−1) (−7) = (+2) × (−3) + (−1)

72
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Implementation of Integer Division


 C division operator /
● If a and b are integers, a / b returns the integer quotient when a is
divided by b
● Used for both signed and unsigned
 C modulus operator %
● If a and b are integers, a % b returns the integer remainder when a is
divided by b
● Used for both signed and unsigned
 x86-64
● Has divide instructions for signed and unsigned integers that return
quotient
► Used by compiler to translate C divide operations

● Has special divide instruction that produces both quotient and


remainder
► You need to write your own routines to utilize this instruction
73
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Lecture Outline
 Representing information as bits
 Bit-level manipulations
 Integers
● Representation: unsigned and signed
● Conversion, casting
● Expanding, truncating
● Addition, subtraction, multiplication, division
 Character Data

74
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Character Data
 In C, the char data type is
considered an integer data
type

 ASCII (American Standard


for Computer Information
Exchange) is most
commonly used standard
for encoding characters

 Original ASCII character set


encodes 128 characters,
each assigned a unique 7-
bit binary value

75
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Character Data
 To meet the demand for
more characters and
symbols used in other
languages, ASCII character
set was subsequently
extended to encode 256
characters, each assigned
an 8-bit binary value.

 Thus, each character fits


in exactly one byte of
storage.

76
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

Character Data
 Extended ASCII does not address the diversity of writing systems in the
world (e.g., Hebrew, Russian, Chinese, ...). To address this problem,
Unicode was developed.
 Current Unicode standard supports almost 150,000 characters
covering over 160 modern and historic writing systems, as well as
symbols and emoji.
 Unicode’s “Universal Character Set” encodes characters as 32-bit
binary numbers. However, alternate encodings are supported. E.g.,
UTF-8 uses a variable-length encoding of 1 to 4 bytes for each
character.
 The first 128 characters of UTF-8 corresponds one-to-one with ASCII,
making valid ASCII text valid UTF-8-encoded Unicode as well.
(However, not extended ASCII.)
 See https://home.unicode.org/ for further information.
 Java uses Unicode for its representation of strings. C uses ASCII but
program libraries are available to support Unicode.

77
Carnegie Mellon
198:331 Intro to Computer Organization Lecture 3

The End

78

You might also like