WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2...

15
WEEK 1.2 NUMBER SYSTEMS 1

Transcript of WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2...

Page 1: WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2 NUMBER SYSTEMS 1 The Computing Cycle 2 Complex: •Quantities •Relationships •Operations

WEEK 1.2 NUMBER SYSTEMS

1

Page 2: WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2 NUMBER SYSTEMS 1 The Computing Cycle 2 Complex: •Quantities •Relationships •Operations

The Computing Cycle

2

Complex: •Quantities •Relationships •Operations •Temporal •Spatial

Binary Quantities (zeros and ones)

Logical Operators (Subject to the rules

of Boolean algebra)

Digital Domain Application Domain

Page 3: WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2 NUMBER SYSTEMS 1 The Computing Cycle 2 Complex: •Quantities •Relationships •Operations

Information Representation: Numbers, TEXT, OPERATIONS, ETC.

? 3

Complex: •Quantities •Relationships •Operations •Temporal •Spatial

Binary Quantities (zeros and ones)

Logical Operators (Subject to the rules

of Boolean algebra)

Digital Domain Application Domain

Page 4: WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2 NUMBER SYSTEMS 1 The Computing Cycle 2 Complex: •Quantities •Relationships •Operations

4

Number Representation

A decimal number such as 7392 represents 7392 = 7x103 + 3x102 + 9x101 + 2x100

It is practical to write only coefficients and deduce power of 10s from position In general, any radix (base) can be used

Define coefficients ai in radix r 0 <= ai <r

anrn + an-1rn-1 …. a0r0 + ….. a-mr-m

Common radics – r = 2, 4, 8, 10, 16

Page 5: WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2 NUMBER SYSTEMS 1 The Computing Cycle 2 Complex: •Quantities •Relationships •Operations

5

General Radix Representation r = 10 (Dec.) r = 2 (Binary) r = 8 (Octal) r = 16 (Hex)

00 0000 00 0

01 0001 01 1

02 0010 02 2

03 0011 03 3

04 0100 04 4

05 0101 05 5

06 0110 06 6

07 0111 07 7

08 1000 10 8

09 1001 11 9

10 1010 12 A

11 1011 13 B

12 1100 14 C

13 1101 15 D

14 1110 16 E

15 1111 17 F

Page 6: WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2 NUMBER SYSTEMS 1 The Computing Cycle 2 Complex: •Quantities •Relationships •Operations

6

Conversion between Binary/Octal/Hex

Nice simple ways to convert between these three number systems, since all are a power of 2

Binary to Octal simply requires grouping bits into groups of 3-bits and converting

Binary to Hex simply requires grouping bits into groups of 4-bits and converting

Going the other direction (Octal to Binary or Hex to Binary) should follow…

Example (100111100101)2 = (4745)8

(100111100101)2 = (9E5)16

Page 7: WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2 NUMBER SYSTEMS 1 The Computing Cycle 2 Complex: •Quantities •Relationships •Operations

7

Complements

In computers, the representation and manipulation of –ve numbers is often performed using complements

Complements for a radix come in two forms

R’s complement (Radix complement)

(r-1)’s complement (Diminished radix complement)

For binary system -- 2’s complement, & 1’s complement

Page 8: WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2 NUMBER SYSTEMS 1 The Computing Cycle 2 Complex: •Quantities •Relationships •Operations

8

r’s Complement

Given a +ve number N with n digits

N = (an-1an-2 …..a0)

r’s complement is defined as

rn – N for N ≠ 0; zero otherwise

Examples – 10’s complement

(37218)10 = 105 – 37218 = 62782

(0.12345)10 = 100 – 0.12345 = 0.87655

Examples – 2’s complement

(101110)2 = 26 – 101110 = 010010

(0.0110)2 = 20 – 0.0110 = 0.1010

• Another way:

• N = (an-1an-2 …..a0)

• Step 1: 𝑎𝑖 = 𝑟 − 1 − 𝑎𝑖 • Step 2: 𝑁 = 𝑎 𝑛−1… . . 𝑎 0 + 1

Page 9: WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2 NUMBER SYSTEMS 1 The Computing Cycle 2 Complex: •Quantities •Relationships •Operations

9

(r-1)’s Complement Given a +ve number N with n digit integer part & m

digit fractional part N = (an-1an-2 …..a0..a-1a-2 ….a-m)

(r-1)’s complement is defined as rn – r-m - N

Examples – 9’s complement (37218)10 = 105 – 1 - 37218 = 62781

(0.12345)10 = 100 – 10-5 - 0.12345 = 0.87654

Examples – 1’s complement (101110)2 = 26 – 20 - 101110 = 111111 -101100 = 010001

(0.0110)2 = 20 – 2-4 - 0.0110 = 0.1111 – 0.0110 = 0.1001

• Another way:

• N = (an-1an-2 …..a0)

• Step 1: 𝑎𝑖 = 𝑟 − 1 − 𝑎𝑖 • Step 2: 𝑁 = 𝑎 𝑛−1… . . 𝑎 0

Page 10: WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2 NUMBER SYSTEMS 1 The Computing Cycle 2 Complex: •Quantities •Relationships •Operations

10

Interesting Facts

The complement of a complement returns the original number

Since we work with binary numbers a lot in digital systems, it is really worth noting that:

The 1’s complement of a number is obtained by flipping bits

The 2’s complement of a number is obtained by flipping bits and adding 1

Page 11: WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2 NUMBER SYSTEMS 1 The Computing Cycle 2 Complex: •Quantities •Relationships •Operations

11

Signed Numbers Computers handle signed numbers as well

Numbers are represented in a fixed # of bits

Often required to represent both +ve & -ve numbers in the same n-bit format

Left most bit (Most Significant Bit) represents the sign 0 +ve; 1 -ve

Three common representations

Sign & magnitude

Signed 1’s complement

Signed 2’s complement

For +ve numbers, all three have same representation

Page 12: WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2 NUMBER SYSTEMS 1 The Computing Cycle 2 Complex: •Quantities •Relationships •Operations

1. signed magnitude

• In each case: left-most bit indicates sign: positive (0) or negative (1).

Consider 1. signed magnitude:

000011002 = 1210

Sign bit Magnitude

100011002 = -1210

Sign bit Magnitude

Page 13: WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2 NUMBER SYSTEMS 1 The Computing Cycle 2 Complex: •Quantities •Relationships •Operations

2. One’s Complement Representation

The one’s complement of a binary number involves inverting all bits.

• To find negative of 1’s complement number take the 1’s complement of whole number including the sign bit.

000011002 = 1210

Sign bit Magnitude

111100112 = -1210

Sign bit 1’complement

Page 14: WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2 NUMBER SYSTEMS 1 The Computing Cycle 2 Complex: •Quantities •Relationships •Operations

3. Two’s Complement Representation

• The two’s complement of a binary number involves inverting all bits and adding 1.

To find the negative of a signed number take the 2’s complement of the positive number including the sign bit.

000011002 = 1210

Sign bit Magnitude

111101002 = -1210

Sign bit 2’s complement

Page 15: WEEK 1.2 NUMBER SYSTEMS - University of Waterloopami.uwaterloo.ca/~basir/ECE124/week1-2.pdfWEEK 1.2 NUMBER SYSTEMS 1 The Computing Cycle 2 Complex: •Quantities •Relationships •Operations

The rule for addition is add the two numbers, including their sign bits,

and discard any carry out of the sign (leftmost) bit position.

Numerical examples for addition are shown below.

Example:

+ 6 00000110 - 6 11111010

+13 00001101 +13 00001101

+19 00010011 +7 00000111

+6 00000110 -6 11111010

-13 11110011 -13 11110011

-7 11111001 -19 11101101

In each of the four cases, the operation performed is always addition,

including the sign bits.

Only one rule for addition, no separate treatment of subtraction.

Negative numbers are always represented in 2’s complement.

Sign addition in 2’s complement