Digital Logic &
Design
              Lecturer :
         Engr Hasnain Ahmad
             week No: 4
Different Logic Codes & their Arithmetic
(BCD – Excess-3 – Gray – ASCII Codes)
Logic Codes & their Arithmetic - Introduction
 Besides the basic representations used for unsigned and signed
  integers and fractions, many other binary codes also exist for
  representing numbers, as well as letters, and all manner of other
  things (colors, machine instructions, etc.) Other types of codes
  are used to protect data from damage or eavesdropping
 In this lecture we will cover a few of the most commonly
  encountered codes: e.g.
        BCD Code, Excess-3 Code, Gray code, ASCII Code
 Introduction of some general concepts of error control codes and
  Parity bits (Odd or Even) will also mentioned.
                                                                2
Logic Codes & their Arithmetic - Introduction
 In computer engineering, digital telecommunications engineering,
  and computer science, certain special numbers of bits that have
  names commonly associated with them:
   ۞ 3 bits – An octal digit.
   ۞ 4 bits (1 hex digit) – A nibble (or nybble?).
   ۞ 8 bits (2 hex digits) – A byte (B), character, or octet.
   ۞ 16 bits (4 hex digits) – A half-word or a short int.
   ۞ 32 bits (8 hex digits) – A word or an int.
   ۞ 64 bits (16 hex digits) – A double-word or long int.
   ۞ 128 bits (32 hex digits) – A quad-word or long long int.
   ۞ 8,192 bits (2,048 hex digits) – A kilobyte (kB) or kibibyte (kiB).
 And so forth,
    ۞with 1,024 KB = 1 MB, and 1,024 MB = 1 GB, etc.                3
Logic Codes & their Arithmetic - BCD Code
 BCD Code is a way of representing decimal numbers directly
  in binary.
    ۞The digits 0-9 are represented with their 4-bit equivalents
     0000-1001 (same as hex digits 0-9).
    ۞However, codes 1010-1111 remain unused.
     ► The values of the digit positions are taken as powers of
     10, rather than 16.
 Example: 35810 = 0011 0101 1000 in BCD.
 Example: Encode the decimal Number N = (2573)10 in BCD
           N = 0010 0101 0111 0011 in BCD.
     ► BCD is more human-readable than hexadecimal or binary,
     ► But arithmetic hardware for plain binary is simpler 4
Logic Codes & their Arithmetic - BCD Code
         Table of
 Signed Decimal Numbers
         & their
     BCD Equivalents
                                            5
Logic Codes & their Arithmetic - BCD Code
 Addition Process of BCD Code
  Many computers and calculators use the BCD Code to represent decimal
  numbers. As mentioned earlier that this code takes each decimal digit and
  represent it by a four-bit code ranging from 0000 to 1001.
 Rule for BCD Addition
   1. Sum of two numbers equals 9 or less
   ۞Then write down the BCD equivalents of the given decimal digits and
     simply add them up to get the final result.
   2. Sum of two numbers is greater than 9
   ۞Whenever sum of two digits exceeds 9, the sum must be corrected by
     the addition of six (0110) in that particular sum when the number
     exceeds 9 to take into account the skipping of the six invalid code
     groups.                                                                  6
Logic Codes & their Arithmetic - BCD Code
 Rule for BCD Subtraction
  Subtract the two BCD numbers using the rules for binary
  subtraction.
   1. If subtraction difference is equal to 9 or less
   ۞If subtraction result is equal or less than 9 then it is a valid BCD number
     , leave it as result
   2. If subtraction difference is greater than 9
   ۞If subtraction result is greater than 9 or if a borrow is required from the
     next group , subtract 6 (0110) from the result produced by four bit
     subtraction.
                                                                              7
Logic Codes & their Arithmetic - BCD Code
Examples for Rule 1: Add 45 + 33 using BCD Addition.
   Example 1
     45            0 1 0 0 0 1 0 1
   + 33           +0 0 1 1 0 0 1 1
     78            0 1 1 1 1 0 0 0
   Example 2
     674           0 1 1 0 0 1 1 1 0 1 0 0
   + 325          +0 0 1 1 0 0 1 0 0 1 0 1
     999           1 0 0 1 1 0 0 1 1 0 0 1
                                                       8
Logic Codes & their Arithmetic - BCD Code
Examples for Rule 2: Add 47 + 35 using BCD Addition.
   Example 1
       47         0 1 0 0 0 1 1 1
   + 35         + 0 0 1 1 0 1 0 1
       82         0 1 1 1 1 1 0 0        Invalid Sum in 1st digit
                         +0 1 1 0        Add 6 to correct
                  1 0 0 0 0 0 1 0         Correct BCD Sum
   Example 2
   756      0 1 1 1 0 1 0 1 0 1 1 0
+ 245 + 0 0 1 0 0 1 0 0 0 1 0 1
 1001        1 0 0 1 1 0 0 1 1 0 1 1        Invalid Sum in 1st digit
    0 0 0 1+ 0 1 1 0 +0 1 1 0 +0 1 1 0 Add 6 in each bit to correct
    0001 0 0 0 0 0 0 0 0 0 0 0 1            Correct BCD Sum 9
Logic Codes & their Arithmetic - BCD Code
Examples for Rule 2: Sub 86 - 24 using BCD Subtraction.
    Example 1
       86        1 0 0 0 0 1 1 0
    - 24       - 0 0 1 0 0 1 0 0
       82        0 1 1 0 0 0 1 0
    Example 2
   920    1 0 0 1 0 0 1 0 0 0 0 0
- 265 - 0 0 1 0 0 1 1 0 0 1 0 1
   655     0 1 1 0 1 0 1 1 1 0 1 1        Invalid Sum in 1st digit
                   - 0 1 1 0 - 0 1 1 0 Add 6 in each bit to correct
           01 1 0 01 0 1 0 1 0 1            Correct BCD Sum
                                                               10
Logic Codes & their Arithmetic – Excess-3 Code
 Excess-3 Code is a type of a code in which a digit is represented
  by adding 3 to the number and then converting it to a 4-bit
  binary number. It can be used for the representation of multi-
  digit decimal numbers as can BCD. This code have some
  advantages for performing decimal arithmetic.
 Excess-3 binary-coded decimal (XS-3), also called biased
  representation or Excess-N, is a numeral system used on some
  older computers that uses a pre-specified number N as a biasing
  value. It is a way to represent values with a balanced number of
  positive and negative numbers. In XS-3, numbers are represented
  as decimal digits, and each digit is represented by four bits as the
  BCD value plus 3 (the "excess" amount):
                                                                  11
Logic Codes & their Arithmetic – Excess-3 Code
 In Excess-3 Code The smallest binary number represents
  the smallest value. (i.e. 0 − Excess Value) and The
  greatest binary number represents the largest value. (i.e.
  2 N+1 − Excess Value − 1)
                Table for Excess-3 Code
                                                          12
Logic Codes & their Arithmetic – Excess-3 Code
 The primary advantage of XS-3 coding over BCD coding is
  that a decimal number can be nines' complemented (for
  subtraction) as easily as a binary number can be ones'
  complemented; just invert all bits.
 Adding Excess-3 works on a different algorithm than BCD
  coding or regular binary numbers. When you add two
  XS-3 numbers together, the result is not an XS-3 number.
  For instance, when you add 1 and 0 in XS-3 the answer
  seems to be 4 instead of 1. In order to correct this problem,
  when you are finished adding each digit, you have to
  subtract 3 (binary 11) if the digit is less than decimal 10
  and add three if the number is greater than or equal to
  decimal 10 (thus causing the number to wrap).
                                                            13
Logic Codes & their Arithmetic – Excess-3 Code
 Addition is Excess-3 (XS-3)
 Normally the problem arises when we are trying to add 8421
  numbers whose decimal sum exceeds 9. The XS-3 code was
  designed to get rid of this. There are two cases in XS-3 Addition
 Rule for Excess-3 Addition
    Case 1. Sum of two numbers equals 9 or less
    ۞Then the results is an Excess-6 number. To return to XS-3
      form we must subtract 3 (0011) to get the required result.
   Case 2. Sum of two numbers is greater than 9
    ۞Whenever sum of two digits exceeds 9, there will   be a carry
      from one group into the next. When this happened, the group
      that produced the carry will revert to 8421 form. To restore
      the answer to XS-3 code we must add 3 to the group that
      produced the carry.
                                                                   14
Logic Codes & their Arithmetic – Excess-3 Code
 Subtraction is Excess-3 (XS-3)
 Normally the problem arises when we are trying to add 8421
  numbers whose decimal sum exceeds 9. The XS-3 code was
  designed to get rid of this. There are two cases in XS-3 Addition
 Rule for Excess-3 Subtraction
    Case 1. when there is borrow
    ۞When there is a  borrow when we subtract two numbers then
      subtract 3 (0011) .
   Case 2. Sum of two numbers is greater than 9
    ۞When there is no borrow when we subtract two numbers
      then add 3 (0011)
                                                                 15
Logic Codes & their Arithmetic – Excess-3 Code
Example for Case 1: Add 43 + 36 using Excess-3 addition.
   Example 1
      43             0 1 1 1 0 1 1 0
   + 36           +0 1 1 0 1 0 0 1
      79             1 1 0 1 1 1 1 1      XS-6 for 79
                   - 0 0 1 1 - 0 0 1 1 Subtract 3 to correct
                     1 0 1 0 1 1 0 0      XS-3 for 79
Example for Case 1: Add 674 + 325 using Excess-3 addition.
   Example 2
  674      1 0 0 1 1 0 1 0 0 1 1 1
+ 325 +0 1 1 0 0 1 0 1 1 0 0 0
  999       1 1 1 1 1 1 1 1 1 1 1 1         XS-6 for 999
          - 0 0 1 1 - 0 0 1 1 - 0 0 1 1 Subtract 3 to correct
            1 1 0 0 1 1 0 0 1 1 0 0        XS-3 for 999 16
Logic Codes & their Arithmetic – Excess-3 Code
Example for Case 1: Add 29 + 39 using Excess-3 addition.
   Example 1
      29         0 1 0 1 1 1 0 0
   + 39        +0 1 1 0 1 1 0 0
      68          1 1 0 0 1 0 0 0          XS-6 for 68
                - 0 0 1 1 + 0 0 1 1 Add & Subtract 3 to correct
                  1 0 0 1 1 0 1 1      XS-3 for 68
Example for Case 2: Add 576 + 325 using Excess-3 addition.
   Example 2
  576      1 0 0 0 1 0 1 0 1 0 0 1
+ 325 +0 1 1 0 0 1 0 1 1 0 0 0
  901       1 1 1 1 0 0 0 0 0 0 0 1         XS-6 for 901
          -0 0 1 1 +0 0 1 1+0 0 1 1 Add & Subtract 3 to correct
                                                            17
            1 1 0 0 0 0 1 1 0 1 0 0        XS-3 for 901
Logic Codes & their Arithmetic – Excess-3 Code
Example for Case 2: Sub 86 - 24 using Excess-3 subtraction.
   Example 1
     86         1 0 1 1 1 0 0 1
   - 24      -0 1 0 1 0 1 1 1
     62         0 1 1 0 0 0 1 0            XS-6 for 68
              + 0 0 1 1 + 0 0 1 1 Add & Subtract 3 to correct
                 1 0 0 1 0 1 0 1        XS-3 for 68
Example for Case 1: sub 920 - 265 using Excess-3 subtraction.
   Example 2
  920 1 1 0 0 0 1 0 1 0 0 1 1
- 265 -0 1 0 1 1 0 0 1 1 0 0 0
  655 0 1 1 0 1 0 1 1 1 0 1 1                XS-6 for 901
       +0 0 1 1 -0 0 1 1 -0 0 1 1 Add & Subtract 3 to correct
                                                            18
         1 0 0 1 1 0 0 0 1 0 0 0          XS-3 for 901
Logic Codes & their Arithmetic – Gray Code
 Gray Code is a form of binary that uses a different method
  of incrementing from one number to the next. With Gray
  Code, only one bit changes state from one position to
  another. This feature allows a system designer to perform
  some error checking (i.e. if more than one bit changes, the
  data must be incorrect).
 Gray Code is a unweighted code not suited to arithmetic
  operations but useful for input / output devices, Analog to
  Digital converters and other peripheral equipments. The
  important characteristic of the Gray code is that only one
  digit changes as you count from top to bottom.
                                                            19
Logic Codes & their Arithmetic – Gray Code
 Why use Gray Code
    Gray Code is the most popular absolute encoder output type because
   its use prevents certain data errors which can occur with Natural
   binary during state changes. For example, in a highly capacitive
   circuit (or sluggish system response), a natural binary state change
   from 0011 to 0100 could cause the counter/PLC to see 0111. This
   sort of error is not possible with Gray Code, so the data is more
   reliable.
Table:1 Shows the difference between Natural Binary and Gray C2o0de:
Logic Codes & their Arithmetic – Gray Code
    In the Table 2 below, note that even from position 7 to 8,
             Gray Code only changes one bit state.
                                                                 21
Logic Codes & their Arithmetic – Gray Code
 Conversion of the Gray Code to Natural Binary
 On paper, the process is easy:
 1) Write the Gray Code and copy the left most bit under itself.
 2) Add the copied bit to the next Gray Code bit over (to the
  right). Remember, 0+0=0, 0+1=1, 1+0=1, 1+1=0. Drop all
  carries. Write result next to the copied digit from step 1.
 3) Repeat step 2 until completed. See Table 3 on the next slide.
                                                                 22
Logic Codes & their Arithmetic – Gray Code
 Process of Binary code to Gray code conversion
Example 1: Convert Binary 110011011 to its Gray equivalent
1      1      0      0      1      1      0      1      1
1    0      1       0      1     0       1       1      0
Example 2: Convert Binary 011001010 to its Gray equivalent
0      1      1      0      0      1      0      1      0
0      1      0      1      0      1      1      1      1    23
Logic Codes & their Arithmetic – Gray Code
 Process of Gray code to Binary code conversion
Example 1: Convert Gray 101001010 to its Binary equivalent
      Gray Code                   Binary Code
1    0       1      0     0      1       0       1      0
1    1      0       0     0      1        1      0      0
Example 2: Convert Gray 111011110 to its Binary equivalent
1     1      1      0      1      1      1      1      0
0     1      0      1      0      1      1      1      1     24
Logic Codes & their Arithmetic – ASCII Code
  ASCII Code: The most widely used alphanumeric code which
   has almost universal use is the American Standard Code for
   Information Interchange (ASCII). This was originally a 7-bit
   code, designed to be capable of representing the alphabet in
   lower- and upper-case, all the numbers, the standard punctuation
   symbols and also additional control characters. It is a seven bit
   code and so it has 27=128 possible code this is more than
   enough to represent all of the standard key board characters as
   well as the control functions such as the return and line feed
   functions.
  The first 32 values in the code are used as control characters.
   An exception to this is code 127 (7F in hex). This is used to
   represent the DEL (Delete) key. The Space character is an
                                                                   25
   actual character. However it is labeled as space for clarity.
Logic Codes & their Arithmetic – ASCII Code
                                              26
Logic Codes & their Arithmetic – ASCII Code
                                              27
Logic Codes & their Arithmetic – ASCII Code
  Example of writing a sentence in ASCII Code
               Logic Design & Switching Theory
       L        o         g         i         c       sp
   1001100   1101111   1100111   1101001   1100011 0100000
       D        e         s          i         g       n
   1000100   1100101   1110011   1101001   1100111 1101110
      sp       &          sp        S          w       i
   0100000   0100110   0100000   1010011   1110111 1101001
       t        c          h         i         n       g
   1110100   1100011   1101000   1101001   1101110 1100111
      T         h         e         o          r       y
   1010100   1101000   1100101   1101111    1110010 1111001   28
Logic Codes & their Arithmetic – Parity Bits
 Error Detection and Correction Codes:
 The distance between two code words I and J is equal to the
  number of bit positions in which I and J differ. If the distance
  between any code words of a code C is ≥ dmin, the code is
  said to have minimum distance dmin.
  A code provides t error correction plus detection of s
  additional errors if and only if
                      2t + s + 1 ≤ dmin.
       • For single error detection codes (s=1, t=0), dmin ≥ 2.
       • For single error detection codes (s=0, t=1), dmin ≥ 3.
                                                                  29
Logic Codes & their Arithmetic – Parity Bits
 Simple Parity Codes
 Even Parity: Total number of 1’s in a code word is even.
 Odd Parity: Total number of 1’s in a code word is odd.
                  Binary   Even parity Odd parity
                   000       0000        0001
                   001       0011        0010
                   010       0101        0100
                   011       0110        0111
                   100       1001        1000
                   101       1010        1011
                   110       1100        1101
                   111       1111        1110
                                                             30
Logic Codes & their Arithmetic – Parity Bits
 Example of ASCII code with Odd & Even Parity Bits
 Even Parity: Total number of 1’s in a code word is even.
 Write Pakistan in ASCII code with Even Parity.
   P                a               k               i
 01010000       11100001         11101011         01101001
   s                t               a               n
 11110011       01110100         11100001         11101110
 Odd Parity: Total number of 1’s in a code word is odd.
 Write Pakistan in ASCII code with Odd Parity.
   P                a               k               i
11010000        01100001         01101011         11101001
   s                t               a               n
                                                             31
01110011        11110100         01100001         01101110
               BINARY STORAGE AND REGISTERS
•  The binary information in a digital computer must have a physical existence in some
  medium for storing individual bits.
• A binary cell is a device that possesses two stable states and is capable of storing one
  bit (0 or 1) of information.
• The information stored in a cell is 1 when the cell is in one stable state and 0 when the
  cell is in the other stable state.
    A register is a group of binary cells.
    A register with n cells can store any discrete quantity of information that contains n
    bits.
    The state of a register is an n-tuple of 1’s and 0’s
    example,
    a 16-bit register with the following binary content:
                        1100001111001001
    A register with 16 cells can be in one of 2^16     possible states.
Register Transfer
• In digital systems, a register transfer operation is a basic
  operation that consists of a transfer of binary information from
  one set of registers into another set of registers.
• The transfer may be direct, from one register to another, or may
  pass through data-processing circuits to perform an operation.
• Figure illustrates the transfer of information among registers and
  demonstrates pictorially the transfer of binary information