lecture 2: number systems: binary numbers and gray · pdf filecse20 lecture 2: number...

Click here to load reader

Post on 21-Aug-2018

213 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • CSE20

    Lecture 2: Number Systems: BinaryNumbers and Gray Code

    CK Cheng

    1

  • Number Systems

    1. Introduction2. Binary Numbers3. Gray code4. Negative Numbers5. Residual Numbers

    2

  • 2. Binary Numbers: iClickerWhat is the extent of a binary number

    system A. Coverage of integer and floating point

    numbers B. Mechanism of addition and subtraction

    operations C. Operations of logic functions D. All of the above E. None of the above.

    3

  • 2. Binary Numbers

    1. Definition (radix 2)2. Enumerations (value -> index)3. Addition (logic -> hardware)

    4

  • 2.1 Definition of Binary Numbers

    Format: An n digit binary number(bn-1, , b1, b0)2 where bi in {0,1} for 0

  • 2.2 Enumeration of Binary Numbers

    id b2b1b00 0001 0012 0103 0114 1005 1016 1107 111

    6

    id b1b00 001 012 103 11

    id b00 01 1

    1 digit 2 digits 3 digits 4 digits?

    id b3b2b1b00 00001 00012 00103 0011. .. .. .14 111015 1111

    An n digit binary codecovers numbers from0 to 2n-1.

  • 2.2 Enumeration of binary numbersiCliker

    When we enumerate binary numbers(b3b2b1b0)2 from 0 to 15, the sequence of b3

    should be A. 0101010101010101 B. 0011001100110011 C. 0000111100001111 D. 0000000011111111

    7

  • 2.3 Addition of Binary Numbers

    Given two binary numbers A & B, we derivebinary number S so that the value of S isequal to the sum of the values of A & B,i.e.(an-1,a1a0)2+(bn-1b1b0)2=(sn-1s1s0)2

    Caution: Overflow, i.e. the sum is beyondthe range of the representation.

    8

  • 2.3 Addition: iClickerGiven two binary numbers

    A=(an-1,a1a0)2 and B=(bn-1b1b0)2what is the largest possible value of A+B?A.2n+1

    B.2n+1-1C.2n+1-2D.None of the above

    9

  • 2.3 Addition of Binary NumbersEquality of addition

    (an-1,a1a0)2+(bn-1b1b0)2=(sn-1s1s0)2That is to sayan-12n-1++a121+a020+bn-12n-1++b121+b020

    =(an-1+bn-1)2n-1++(a1+b1)21+(a0+b0)20

    =sn-12n-1++s121+s020

    10

  • 2.3 Addition of Binary Numbers

    b2 b1 b0 Value0 0 0 00 0 1 10 1 0 20 1 1 31 0 0 41 0 1 51 1 0 61 1 1 7

    8 4 2 10 0 1 10 1 0 1

    8 4 2 10 0 1 10 1 1 0

    +

    +

    Examples:

    11

  • 2.3 Addition of Binary NumbersBiti+1 Biti Biti-1

    Carryi+1 Carryi

    ai ai-1bi bi-1

    Sumi Sumi-1

    12

    Formula for Bit i:Carryi+ai+bi= 2xCarryi+1+Sumi

  • 2.3 Adding 2 bits in a digit

    a b Carry Sum0 0 0 00 1 0 11 0 0 11 1 1 0

    Formula:

    a+b=

    2xCarry + Sum

    13

  • 2.3 Adding 3 bits in a digitid a b c Carry Sum0 0 0 0 0 01 0 0 1 0 12 0 1 0 0 13 0 1 1 1 04 1 0 0 0 15 1 0 1 1 06 1 1 0 1 07 1 1 1 1 1

    Formula:

    a+b+c=

    2xCarry + Sum

    14

  • 3. Gray Code

    1. Introduction2. Example3. Construction4. Comments

    15

  • 3.1 Gray Code: IntroductionGray: Frank Gray patented the code in1947

    A variation of binary code

    The code will be used for logic operation(CSE20, CSE140)

    Feature: only one bit changes for twoconsecutive numbers

    16

  • 3.2 Gray Code: Example

    17

    id b2b1b0 g2g1g00 000 0001 001 0012 010 0113 011 0104 100 1105 101 1116 110 1017 111 100

    id b1b0 g1g00 00 001 01 012 10 113 11 10

    2 digits 3 digits

    Note the differenceof the first and lastrows.

  • 3.2 Gray Code

    18

    id b2b1b0 g2g1g00 000 0001 001 0012 010 0113 011 0104 100 1105 101 1116 110 1017 111 100

    3 digits

    id b3b2b1b0 g3g2g1g00 0000 00001 0001 00012 0010 00113 0011 00104 0100 01105 0101 01116 0110 01017 0111 01008 1000 ?9 1001

    10 101011 101112 110013 110114 111015 1111

  • 3.2 Gray Code: iClicker

    19

    A 4-digit Gray code (g3g2g1g0) at id=8 iswritten as (ref: previous page)A. (0101)B. (0110)C. (1100)D. None of the above

  • 3.3 Gray Code: ConstructionConstruction of n-digit Gray code from n-1digit Gray code

    Copy the n-1 digit Gray code for the top 2n-1rows. Fill 0 at digit gn-1 in the top rows.

    Reflect and append the n-1 digit code for thebottom 2n-1 rows. Fill 1 at digit gn-1 in thebottom rows.

    20

  • 3.4 Gray Code: CommentsThere are various codes that satisfy theGray code feature.

    Gray code saves communication powerwhen the signals are continuous in nature,e.g. addresses, analog signals

    Gray code facilitates code checking whenthe signals are supposed to be continuous invalue.For arithmetic operations, we need toconvert the values.

    21

View more