0% found this document useful (0 votes)
1K views

Lecture2

Computer Archtecture - Bitwise Manupilation

Uploaded by

minulo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Lecture2

Computer Archtecture - Bitwise Manupilation

Uploaded by

minulo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 96

CSC 252/452: Computer Organization

Fall 2024: Lecture 2

Instructor: Yanan Guo


Department of Computer Science
University of Rochester

Action Items:
• Get CSUG account
• Make sure you have VPN setup!!!!
Carnegie Mellon

Announcement
• Make sure you can access CSUG machines!!!
• Programming assignment 1 will be posted today.
• It is in C language. Seek help from TAs.
• TAs are best positioned to answer your questions about
programming assignments!!!
• Programming assignments do NOT repeat the lecture
materials. They ask you to synthesize what you have
learned from the lectures and work out something new.

2
Carnegie Mellon

Previously in 252…

Problem

Algorithm

Program

Instruction Set
Architecture (ISA)

Microarchitecture

Circuit

3
Carnegie Mellon

Previously in 252…

Problem

Algorithm

Program
ISA is the contract
Instruction Set
between software and
Architecture (ISA) hardware.

Microarchitecture

Circuit

3
Carnegie Mellon

Previously in 252…

Problem

Algorithm

Renting Computing
Program
Service provider Landlord Hardware
ISA is the contract
Service receiver Instruction
YOU Set Software
between software and
Contract Architecture
Lease (ISA) ISA
hardware.
Contract’s language Natural language (e.g., Assembly programming
Microarchitecture
English) language

Circuit

3
Carnegie Mellon

Previously in 252…

Problem

• How is a human-
readable program Algorithm
translated to a
representation that
computers can Program
understand?
ISA is the contract
Instruction Set
between software and
Architecture (ISA) hardware.

Microarchitecture

Circuit

3
Carnegie Mellon

Previously in 252…
C Program Assembly program

void add() { movl $1, -4(%rbp)


int a = 1; movl $2, -8(%rbp)
movl -4(%rbp), %eax
int b = 2;
addl -8(%rbp), %eax
int c = a + b;
}

4
Carnegie Mellon

Previously in 252…
C Program Assembly program

void add() { movl $1, -4(%rbp)


int a = 1; movl $2, -8(%rbp)
movl -4(%rbp), %eax
int b = 2;
addl -8(%rbp), %eax
int c = a + b;
}

4
Carnegie Mellon

Today: Representing Information in Binary


• Why Binary (bits)?
• Bit-level manipulations
• Integers
• Representation: unsigned and signed
• Conversion, casting
• Expanding, truncating
• Addition, negation, multiplication, shifting
• Summary
• Representations in memory, pointers, strings

5
Carnegie Mellon

Everything is bits
• Each bit is 0 or 1. Bits are how programs talk to the hardware
• Programs encode instructions in bits
• Hardware then interprets the bits

6
Carnegie Mellon

Everything is bits
• Each bit is 0 or 1. Bits are how programs talk to the hardware
• Programs encode instructions in bits
• Hardware then interprets the bits
• Why bits? Electronic Implementation

Power
(Voltage)

6
Carnegie Mellon

Why Bits?
• Each bit is 0 or 1. Bits are how programs talk to the hardware
• Programs encode instructions in bits
• Hardware then interprets the bits
• Why bits? Electronic Implementation
• Use high voltage to represent 1
• Use low voltage to represent 0

0 1 0

1.1V
0.9V

0.2V
0.0V
7
Carnegie Mellon

Why Bits?
• Each bit is 0 or 1. Bits are how programs talk to the hardware
• Programs encode instructions in bits
• Hardware then interprets the bits
• Why bits? Electronic Implementation
• Use high voltage to represent 1
• Use low voltage to represent 0

0 1 0

1.1V
0.9V
Low voltage
0.2V
0.0V
7
Carnegie Mellon

Why Bits?
• Each bit is 0 or 1. Bits are how programs talk to the hardware
• Programs encode instructions in bits
• Hardware then interprets the bits
• Why bits? Electronic Implementation
• Use high voltage to represent 1
• Use low voltage to represent 0

0 1 0

1.1V
0.9V
Low voltage High voltage
0.2V
0.0V
7
Carnegie Mellon

Why Limit Ourselves Only to Bits?


• Voltage is continuous. Why interpret it only as 0s and 1s?

0 1 2 1 0

1.1V
0.9V

0.2V
0.0V

8
Carnegie Mellon

Why Limit Ourselves Only to Bits?


• Voltage is continuous. Why interpret it only as 0s and 1s?
• Answer: Noise

0 1 2 1 0

1.1V
0.9V

0.2V
0.0V

8
Carnegie Mellon

Binary Notation

9
Carnegie Mellon

Binary Notation
• Base 2 Number Representation (Binary)

9
Carnegie Mellon

Binary Notation
• Base 2 Number Representation (Binary)
• C.f., Base 10 number representation (Decimal)

9
Carnegie Mellon

Binary Notation
• Base 2 Number Representation (Binary)
• C.f., Base 10 number representation (Decimal)
• 32110 = 1*100 + 2*101 + 3*102 = 321

9
Carnegie Mellon

Binary Notation
• Base 2 Number Representation (Binary)
• C.f., Base 10 number representation (Decimal)
• 32110 = 1*100 + 2*101 + 3*102 = 321
• Weighted Positional Notation
• Each bit has a weight depending on its position

9
Carnegie Mellon

Binary Notation
• Base 2 Number Representation (Binary)
• C.f., Base 10 number representation (Decimal)
• 32110 = 1*100 + 2*101 + 3*102 = 321
• Weighted Positional Notation
• Each bit has a weight depending on its position
• 10112 = 1*20 + 1*21 + 0*22 + 1*23 = 1110

9
Carnegie Mellon

Binary Notation
• Base 2 Number Representation (Binary)
• C.f., Base 10 number representation (Decimal)
• 32110 = 1*100 + 2*101 + 3*102 = 321
• Weighted Positional Notation
• Each bit has a weight depending on its position
• 10112 = 1*20 + 1*21 + 0*22 + 1*23 = 1110
• b3b2b1b0 = b0*20 + b1*21 + b2*22 + b3*23

9
Carnegie Mellon

Binary Notation
• Base 2 Number Representation (Binary)
• C.f., Base 10 number representation (Decimal)
• 32110 = 1*100 + 2*101 + 3*102 = 321
• Weighted Positional Notation
• Each bit has a weight depending on its position
• 10112 = 1*20 + 1*21 + 0*22 + 1*23 = 1110
• b3b2b1b0 = b0*20 + b1*21 + b2*22 + b3*23
• Binary Arithmetic

9
Carnegie Mellon

Binary Notation
Decimal Binary
• Base 2 Number Representation (Binary) 0 0000
• C.f., Base 10 number representation (Decimal) 1 0001
• 32110 = 1*100 + 2*101 + 3*102 = 321 2 0010
• Weighted Positional Notation 3
4
0011
0100
• Each bit has a weight depending on its position 5 0101
• 10112 = 1*20 + 1*21 + 0*22 + 1*23 = 1110 6 0110
• b3b2b1b0 = b0*20 + b1*21 + b2*22 + b3*23 7 0111
• Binary Arithmetic 8
9
1000
1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111
9
Carnegie Mellon

Binary Notation
Decimal Binary
• Base 2 Number Representation (Binary) 0 0000
• C.f., Base 10 number representation (Decimal) 1 0001
• 32110 = 1*100 + 2*101 + 3*102 = 321 2 0010
• Weighted Positional Notation 3
4
0011
0100
• Each bit has a weight depending on its position 5 0101
• 10112 = 1*20 + 1*21 + 0*22 + 1*23 = 1110 6 0110
• b3b2b1b0 = b0*20 + b1*21 + b2*22 + b3*23 7 0111
• Binary Arithmetic 8
9
1000
1001
10 1010
0110 11 1011
+ 0101 12 1100
13 1101
1011
14 1110
15 1111
9
Carnegie Mellon

Binary Notation
Decimal Binary
• Base 2 Number Representation (Binary) 0 0000
• C.f., Base 10 number representation (Decimal) 1 0001
• 32110 = 1*100 + 2*101 + 3*102 = 321 2 0010
• Weighted Positional Notation 3
4
0011
0100
• Each bit has a weight depending on its position 5 0101
• 10112 = 1*20 + 1*21 + 0*22 + 1*23 = 1110 6 0110
• b3b2b1b0 = b0*20 + b1*21 + b2*22 + b3*23 7 0111
• Binary Arithmetic 8
9
1000
1001
10 1010
0110 6 11 1011
+ 0101 + 5 12 1100
13 1101
1011 11
14 1110
15 1111
9
Carnegie Mellon

Hexdecimal (Hex) Notation


Hex Decimal Binary
• Base 16 Number Representation 0 0 0000
• Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’ 1 1 0001
• Four bits per Hex digit 2 2 0010
3 3 0011
• 111111102 = FE16
4 4 0100
• Write FA1D37B16 in C as 5 5 0101
• 0xFA1D37B 6 6 0110
7 7 0111
• 0xfa1d37b
8 8 1000
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111
10
Carnegie Mellon

Bit, Byte, Word


• Byte = 8 bits
• Binary 000000002 to 111111112; Decimal: 010 to 25510; Hex: 0016 to FF16
• Least Significant Bit (LSb) vs. Most Significant Bit (MSb)

10110111
MSb LSb

11
Carnegie Mellon

Bit, Byte, Word


• Byte = 8 bits
• Binary 000000002 to 111111112; Decimal: 010 to 25510; Hex: 0016 to FF16
• Least Significant Bit (LSb) vs. Most Significant Bit (MSb)

10110111
MSb LSb
• Word = 4 Bytes (32-bit machine) / 8 Bytes (64-bit machine)
• Least Significant Byte (LSB) vs. Most Significant Byte (MSB)

11
Carnegie Mellon

Today: Representing Information in Binary


• Why Binary (bits)?
• Bit-level manipulations
• Integers
• Representation: unsigned and signed
• Conversion, casting
• Expanding, truncating
• Addition, negation, multiplication, shifting
• Summary
• Representations in memory, pointers, strings

12
Carnegie Mellon

Boolean Algebra
• Developed by George Boole in 19th Century
• Algebraic representation of logic
• Encode “True” as 1 and “False” as 0
And Or
n A&B = 1 when both A=1 and B=1 n A|B = 1 when either A=1 or B=1

Not Exclusive-Or (Xor)


n ~A = 1 when A=0 n A^B = 1 when either A=1 or B=1, but not both
Carnegie Mellon

Bit Vector Operations


• Operate on Bit Vectors
• Operations applied bitwise

01101001 01101001 01101001


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

14
Carnegie Mellon

Bit Vector Operations


• Operate on Bit Vectors
• Operations applied bitwise

01101001 01101001 01101001


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

14
Carnegie Mellon

Bit Vector Operations


• Operate on Bit Vectors
• Operations applied bitwise

01101001 01101001 01101001


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

14
Carnegie Mellon

Bit Vector Operations


• Operate on Bit Vectors
• Operations applied bitwise

01101001 01101001 01101001


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

14
Carnegie Mellon

Bit Vector Operations


• Operate on Bit Vectors
• Operations applied bitwise

01101001 01101001 01101001


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

14
Carnegie Mellon

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

15
Carnegie Mellon

Contrast: Logic Operations in C


• Contrast to Logical Operators
• &&, ||, !
• View 0 as “False”
• Anything nonzero as “True”
• Always return 0 or 1
• Early termination (e.g., 0 && 1 && 1)

• Examples (char data type)


• !0x41 ➙ 0x00
• !0x00 ➙ 0x01
• !!0x41 ➙ 0x01

• 0x69 && 0x55 ➙ 0x01


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

16
Carnegie Mellon

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

• 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 ≥ total amount of bits

17
Carnegie Mellon

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

• 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 ≥ total amount of bits

17
Carnegie Mellon

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

• 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 ≥ total amount of bits

17
Carnegie Mellon

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

• 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 ≥ total amount of bits

17
Carnegie Mellon

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

• 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 ≥ total amount of bits

17
Carnegie Mellon

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

• 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 ≥ total amount of bits

17
Carnegie Mellon

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

• 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 ≥ total amount of bits

17
Carnegie Mellon

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

• 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 ≥ total amount of bits

17
Carnegie Mellon

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

• 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 ≥ total amount of bits

17
Carnegie Mellon

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

• 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 ≥ total amount of bits

17
Carnegie Mellon

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

• 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 ≥ total amount of bits

17
Carnegie Mellon

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

• 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 ≥ total amount of bits

17
Carnegie Mellon

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

• 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 ≥ total amount of bits

17
Carnegie Mellon

Today: Representing Information in Binary


• Why Binary (bits)?
• Bit-level manipulations
• Integers
• Representation: unsigned and signed
• Conversion, casting
• Expanding, truncating
• Addition, negation, multiplication, shifting
• Summary
• Representations in memory, pointers, strings

18
Carnegie Mellon

Representing Numbers in Binary


• Different types of number
• Integer (Negative and Non-negative)
• Fractions

… -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 …

19
Carnegie Mellon

Encoding Negative Numbers

20
Carnegie Mellon

Encoding Negative Numbers


• So far we have been discussing non-negative numbers: so
called unsigned. How about negative numbers?

20
Carnegie Mellon

Encoding Negative Numbers


• So far we have been discussing non-negative numbers: so
called unsigned. How about negative numbers?
• Solution 1: Sign-magnitude
• First bit represents sign; 0 for positive; 1 for negative
• The rest represents magnitude

20
Carnegie Mellon

Encoding Negative Numbers


• So far we have been discussing non-negative numbers: so
called unsigned. How about negative numbers?
• Solution 1: Sign-magnitude
• First bit represents sign; 0 for positive; 1 for negative
• The rest represents magnitude

0 1 2 3 4 5 6 7

20
Carnegie Mellon

Encoding Negative Numbers


• So far we have been discussing non-negative numbers: so
called unsigned. How about negative numbers?
• Solution 1: Sign-magnitude
• First bit represents sign; 0 for positive; 1 for negative
• The rest represents magnitude

0 1 2 3 4 5 6 7
000 001 010 011 100 101 110 111

20
Carnegie Mellon

Encoding Negative Numbers


• So far we have been discussing non-negative numbers: so
called unsigned. How about negative numbers?
• Solution 1: Sign-magnitude
• First bit represents sign; 0 for positive; 1 for negative
• The rest represents magnitude

-3 -2 -1 0 1 2 3

111 110 101 000 001 010 011


100

20
Carnegie Mellon

Sign-Magnitude Implications
• Bits have different semantics Signed Binary
Value
• Two zeros… 0 000
• Normal arithmetic doesn’t work 1 001
• Make hardware design harder 2 010
3 011
-0 100
-1 101
-2 110
-3 111

21
Carnegie Mellon

Sign-Magnitude Implications
• Bits have different semantics Signed Binary
Value
• Two zeros… 0 000
• Normal arithmetic doesn’t work 1 001
• Make hardware design harder 2 010
3 011
-0 100
-1 101
010 -2 110
+) 101 -3 111
111

21
Carnegie Mellon

Sign-Magnitude Implications
• Bits have different semantics Signed Binary
Value
• Two zeros… 0 000
• Normal arithmetic doesn’t work 1 001
• Make hardware design harder 2 010
3 011
-0 100
-1 101
010 2 -2 110
+) 101 +) -1 -3 111
111 -3

21
Carnegie Mellon

Sign-Magnitude Implications
• Bits have different semantics Signed Binary
Value
• Two zeros… 0 000
• Normal arithmetic doesn’t work 1 001
• Make hardware design harder 2 010
3 011
-0 100
-1 101
010 2 -2 110
+) 101 +) -1 -3 111
111 -3

21
Carnegie Mellon

Encoding Negative Numbers


• Solution 2: Two’s Complement

22
Carnegie Mellon

Encoding Negative Numbers


• Solution 2: Two’s Complement

0 1 2 3 4 5 6 7

Unsigned Binary
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111

22
Carnegie Mellon

Encoding Negative Numbers


• Solution 2: Two’s Complement

-4 -3 -2 -1 0 1 2 3

Unsigned Binary
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111

22
Carnegie Mellon

Encoding Negative Numbers


• Solution 2: Two’s Complement

-4 -3 -2 -1 0 1 2 3

Signed Unsigned Binary


0 0 000
1 1 001
2 2 010
3 3 011
-4 4 100
-3 5 101
-2 6 110
-1 7 111

22
Carnegie Mellon

Encoding Negative Numbers


• Solution 2: Two’s Complement

-4 -3 -2 -1 0 1 2 3

Signed Unsigned Binary


0 0 000
Signed Unsigned Bit
1 1 001
Weight Weight Position
20 20 0 2 2 010
21 21 1 3 3 011
-22 22 2 -4 4 100
-3 5 101
-2 6 110
-1 7 111

22
Carnegie Mellon

Encoding Negative Numbers


• Solution 2: Two’s Complement

-4 -3 -2 -1 0 1 2 3

Signed Unsigned Binary


0 0 000
Signed Unsigned Bit
1 1 001
Weight Weight Position
20 20 0 2 2 010
21 21 1 3 3 011
-22 22 2 -4 4 100
-3 5 101
-2 6 110
-1 7 111

22
Carnegie Mellon

Encoding Negative Numbers


• Solution 2: Two’s Complement

-4 -3 -2 -1 0 1 2 3

Signed Unsigned Binary


0 0 000
Signed Unsigned Bit
1 1 001
Weight Weight Position
20 20 0 2 2 010
21 21 1 3 3 011
-22 22 2 -4 4 100
-3 5 101
-2 6 110
-1 7 111
1012 = 1*20 + 0*21 - 1*22 = -310

22
Carnegie Mellon

Two-Complement Encoding Example


x = 15213: 00111011 01101101
y = -15213: 11000100 10010011

23
Carnegie Mellon

Two-Complement Implications
• Only 1 zero Signed Binary

• Usual arithmetic still works 0


1
000
001
• There is a bit that represents sign! 2 010

• Most widely used in today’s machines 3


-4
011
100
-3 101
-2 110
-1 111

24
Carnegie Mellon

Two-Complement Implications
• Only 1 zero Signed Binary

• Usual arithmetic still works 0


1
000
001
• There is a bit that represents sign! 2 010

• Most widely used in today’s machines 3


-4
011
100
-3 101
-2 110
-1 111
010
+) 101
111

24
Carnegie Mellon

Two-Complement Implications
• Only 1 zero Signed Binary

• Usual arithmetic still works 0


1
000
001
• There is a bit that represents sign! 2 010

• Most widely used in today’s machines 3


-4
011
100
-3 101
-2 110
-1 111
010 2
+) 101 +) -3
111 -1

24
Carnegie Mellon

Numeric Ranges

25
Carnegie Mellon

Numeric Ranges
• Unsigned Values
• UMin = 0
000…0
• UMax = 2w – 1
111…1

25
Carnegie Mellon

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

25
Carnegie Mellon

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

Values for W = 16

25
Carnegie Mellon

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

25
Carnegie Mellon

Data Representations in C (in Bytes)


• By default variables are signed
• Unless explicitly declared as unsigned (e.g., unsigned int)
• Signed variables use two-complement encoding

C Data Type 32-bit 64-bit

char 1 1
short 2 2
int 4 4
long 4 8

26
Carnegie Mellon

Data Representations in C (in Bytes)

C Data Type 32-bit 64-bit

char 1 1
short 2 2
int 4 4
long 4 8

27
Carnegie Mellon

Data Representations in C (in Bytes)

C Data Type 32-bit 64-bit


• C Language
•#include <limits.h>
char 1 1 •Declares constants, e.g.,
short 2 2 •ULONG_MAX
•LONG_MAX
int 4 4
•LONG_MIN
long 4 8
•Values platform specific

27
Carnegie Mellon

Today: Representing Information in Binary


• Why Binary (bits)?
• Bit-level manipulations
• Integers
• Representation: unsigned and signed
• Conversion, casting
• Expanding, truncating
• Addition, negation, multiplication, shifting
• Summary
• Representations in memory, pointers, strings

28
Carnegie Mellon

One Bit Sequence, Two Interpretations


• A sequence of bits can be interpreted as either a signed
integer or an unsigned integer

Signed Unsigned Binary


0 0 000
1 1 001
2 2 010
3 3 011
-4 4 100
-3 5 101
-2 6 110
-1 7 111

29
Carnegie Mellon

Signed vs. Unsigned Conversion in C


• What happens when we convert
between signed and unsigned numbers?
• Casting (In C terminology)
• Explicit casting between signed & unsigned

int tx, ty = -3;


unsigned ux = 7, uy;
tx = (int) ux; // U2T
uy = (unsigned) ty; // T2U

• Implicit casting
• e.g., assignments, function calls
tx = ux;
uy = ty;

30
Carnegie Mellon

Mapping Between Signed & Unsigned


• Mappings between unsigned and two’s complement
numbers: Keep bit representations and reinterpret

31
Carnegie Mellon

Mapping Between Signed & Unsigned


• Mappings between unsigned and two’s complement
numbers: Keep bit representations and reinterpret

Signed Unsigned Binary


0 0 000
1 1 001
2 2 010
3 3 011
-4 4 100
-3 5 101
-2 6 110
-1 7 111

31
Carnegie Mellon

Mapping Signed « Unsigned


Bits Signed Unsigned
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 10
1011 -5 11
1100 -4 12
1101 -3 13
1110 -2 14
1111 -1 15
32
Carnegie Mellon

Mapping Signed « Unsigned


Bits Signed Unsigned
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
T2U
0110 6 6
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 15
32
Carnegie Mellon

Mapping Signed « Unsigned


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

Mapping Signed « Unsigned


Bits Signed Unsigned
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 10
1011 -5 11
1100 -4 12
1101 -3 13
1110 -2 14
1111 -1 15
32
Carnegie Mellon

Mapping Signed « Unsigned


Bits Signed Unsigned
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
32
Carnegie Mellon

Conversion Visualized
• Signed → Unsigned UMax
• Ordering Inversion UMax – 1
• Negative → Big Positive

TMax + 1 Unsigned
TMax TMax Range

2’s Complement 0 0
Range –1
–2

TMin
33
Carnegie Mellon

Conversion Visualized
• Signed → Unsigned UMax
• Ordering Inversion UMax – 1
• Negative → Big Positive

TMax + 1 Unsigned
TMax TMax Range

2’s Complement 0 0
Range –1
–2

TMin
33
Carnegie Mellon

Conversion Visualized
• Signed → Unsigned UMax
• Ordering Inversion UMax – 1
• Negative → Big Positive

TMax + 1 Unsigned
TMax TMax Range

2’s Complement 0 0
Range –1
–2

TMin
33

You might also like