More About Twos Complement - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

More About Twos Complement

Description:

More About Two's Complement. Two's Complement is used to represent both positive ... Wow. ... http://en.wikipedia.org/wiki/Hamming_code ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 28
Provided by: chris347
Category:
Tags: complement | more | twos | wiki | wow

less

Transcript and Presenter's Notes

Title: More About Twos Complement


1
More About Twos Complement
  • Twos Complement is used to represent both
    positive and negative numbers, but the positive
    numbers are the same as they would be without the
    twos complement representation.
  • Examples 0110121310
  • 111012-310

2
To Convert a positive number to a negative 2s
complement
  • 001102610? Convert to -6 in 2s complement
  • There are two methods
  • Method 1
  • (a) Flip all the bits
  • 11001 (this is the 1s complement)
  • (b) Add 1
  • 11010
  • Method 2 (shortcut!)
  • (a) Start at least significant bit (the farthest
    to the right), and copy 0s until you get to a 1
    (also copy the 1) 10
  • (b) Then flip the rest of the bits11010

3
Why 2s Complement?
  • Only one form of 0.
  • Easy to add
  • Just Add!
  • 0100 ( 410)
  • 1101 (-310)
  • -----
  • 0001 ( 110)
  • It works!

4
Why 2s Complement?
  • Just Add!
  • 1011 (-510)
  • 0010 ( 210)
  • -----
  • 1101 (-310)
  • It works!
  • Wow.
  • (by the way, you detect overflow by looking at
    the last two carry bits if theyre different,
    youve got an overflow condition)

5
Why 2s Complement?
  • Subtraction is easy, too!
  • To subtract, simply convert the number youre
    subtracting away to its 2s complement, and then
    add
  • 1011 (-510)
  • -0010 -( 210)
  • 111
  • 1011 (-510)
  • 1110 (-210)
  • ---- -----
  • 1001 (-710)
  • Ignore the carry out

6
Why 2s Complement?
  • Subtraction is easy, too!
  • To subtract, simply convert the number youre
    subtracting away to its 2s complement, and then
    add
  • 1011 (-510)
  • -0010 (-210)

111 1011 (-510) 0010 (210) ---- -----
1101 (-310) Ignore the carry out
7
Hamming Code
  • Lets say you want to send a series of binary
    digits from one location to another, via a wire.
  • Question Is the message guaranteed to get from
    sender to receiver perfectly?
  • Answer No. In the physical world, errors
    sometimes occur in transmission. Generally, the
    faster the transmission speed, the greater the
    number of errors that will occur.

8
Hamming Code
  • Obviously, we want to be able to detect an error
    if there is one.
  • If we detect an error, what can we do about it?
  • Lets say we send a 4-bit number, 1010, and it
    arrives as 1011. Can we detect if it is wrong?
  • Answer In this case, not really. All we know is
    that were expecting a 4-bit number, and we got a
    4-bit number. But we have no idea if it is
    correct or not.

9
Parity
  • Well, then, what can we do?
  • One idea is to add a parity bit. This adds an
    extra bit to the number, say as the most
    significant bit. The sender counts the number of
    ones in the number, and if it is an odd number,
    the parity bit becomes a 1 . This is called an
    even parity bit, because when you look at the
    whole string, it is even.
  • Examples
  • 1001 ? There is an even number of 1s, so we send
    01001.
  • 1110 ? An odd number of 1s, so we send 11110.

10
Parity
  • Even parity bit examples
  • 1001 ? There is an even number of 1s, so we send
    01001.
  • 1110 ? An odd number of 1s, so we send 11110.
  • Now, if we receive a number that has the wrong
    parity (e.g., we count the 1s, and there isnt an
    even number of them), then we have detected an
    error.
  • Notes
  • This only detects a single error (e.g., if two
    ones were both sent as zeros, there would still
    be even parity, and we wouldnt know there was an
    error)
  • What do we do once we detect the error? Throw
    away the number? Ask the sender to resend it?
    (neither is a great answer)

11
Hamming Code
  • Really, what we want is the ability to detect an
    error, and also to correct the error. How can we
    do this?

12
Hamming Code
  • In 1950, Richard Hamming came up with a clever
    method to both detect and correct single errors
    in a series of bits.
  • This is how the code works
  • Step one. The sender decides on how many message
    bits to use. Well use a 6-bit number
  • 110100

13
Hamming Code
  • Our number 110100
  • Step 2 Write the number down, but leave the
    powers of two bits empty, starting with the
    highest order bit
  • Number _ _ 1 _ 1 0 1 _ 0 0
  • Bit 1 2 3 4 5 6 7 8 9 10

14
Hamming Code
  • Number _ _ 1 _ 1 0 1 _ 0 0
  • Bit 1 2 3 4 5 6 7 8 9 10
  • Step 3 Each parity bit calculates the parity for
    some of the bits in the code word. The position
    of the parity bit determines the sequence of bits
    that it alternately checks and skips.
  • Position 1 check 1 bit, skip 1 bit, check 1 bit,
    skip 1 bit, etc. (1,3,5,7,9,11,13,15,...)
  • Position 2 check 2 bits, skip 2 bits, check 2
    bits, skip 2 bits, etc. (2,3,6,7,10,11,14,15,...)
  • Position 4 check 4 bits, skip 4 bits, check 4
    bits, skip 4 bits, etc. (4,5,6,7,12,13,14,15,20,21
    ,22,23,...)
  • Position 8 check 8 bits, skip 8 bits, check 8
    bits, skip 8 bits, etc. (8-15,24-31,40-47,...)
  • We want the bits that weve checked to be even
    parity altogether, so we basically count the
    number of 1s, and if it is an odd number, the
    parity bit for that position becomes a 1.

15
Hamming Code
  • Position 1 check 1 bit, skip 1 bit, check 1 bit,
    skip 1 bit, etc. (1,3,5,7,9)
  • Number _ _ 1 _ 1 0 1 _ 0 0
  • Bit 1 2 3 4 5 6 7 8 9 10
  • There are three ones, so bit 1 becomes a 1 to
    make the parity even
  • Number 1 _ 1 _ 1 0 1 _ 0 0
  • Bit 1 2 3 4 5 6 7 8 9 10

16
Hamming Code
  • Position 2 check 2 bits, skip 2 bits, check 2
    bits, skip 2 bits, etc. (2,3,6,7,10)
  • Number _ _ 1 _ 1 0 1 _ 0 0
  • Bit 1 2 3 4 5 6 7 8 9 10
  • There are two ones, so bit 2 becomes a 0 to make
    the parity even
  • Number 1 0 1 _ 1 0 1 _ 0 0
  • Bit 1 2 3 4 5 6 7 8 9 10

17
Hamming Code
  • Position 4 check 4 bits, skip 4 bits, check 4
    bits, skip 4 bits, etc. (4,5,6,7,12,13,14,15,...)
  • Number _ _ 1 _ 1 0 1 _ 0 0
  • Bit 1 2 3 4 5 6 7 8 9 10
  • There are two ones, so bit 4 becomes a 0 to make
    the parity even
  • Number 1 0 1 0 1 0 1 _ 0 0
  • Bit 1 2 3 4 5 6 7 8 9 10

18
Hamming Code
  • Position 8 check 8 bits, skip 8 bits, check 8
    bits, skip 8 bits, etc. (8-15,24-31,40-47,...)
  • Number _ _ 1 _ 1 0 1 _ 0 0
  • Bit 1 2 3 4 5 6 7 8 9 10
  • There are zero ones, so bit 8 becomes a 0 to make
    the parity even
  • Number 1 0 1 0 1 0 1 0 0 0
  • Bit 1 2 3 4 5 6 7 8 9 10
  • The number we send 1010101000

19
Hamming Code
  • The number we send 1010101000
  • The receiver then receives the bits and checks
    the number by verifying each check bit. Lets
    say the following number was received
  • 1010111000
  • Check
  • Bit 1 1 0 1 0 1 1 1 0 0 0 Even parity ?
  • Bit 2 1 0 1 0 1 1 1 0 0 0 Odd parity ?
  • Bit 4 1 0 1 0 1 1 1 0 0 0 Odd parity ?
  • Bit 8 1 0 1 0 1 1 1 0 0 0 Even parity ?

20
Hamming Code
  • 1010111000
  • Check
  • Bit 1 1 0 1 0 1 1 1 0 0 0 Even parity ?
  • Bit 2 1 0 1 0 1 1 1 0 0 0 Odd parity ?
  • Bit 4 1 0 1 0 1 1 1 0 0 0 Odd parity ?
  • Bit 8 1 0 1 0 1 1 1 0 0 0 Even parity ?
  • The best part about this code is the next check
  • Add the check bit numbers that were incorrect,
    and you get the incorrect bit!
  • In this case, 246, so bit six is incorrect, it
    should be a 0.

21
Hamming Code
  • The last step is to fix the incorrect bit, and
    then extract the original number
  • 1010111000 ? 1010101000 ? 1010101000
  • The original number sent was 110100
  • What if only one bit check was incorrect? Then
    the check digit was incorrect.

22
  • Check 1011101000
  • Bit 1 1 0 1 1 1 0 1 0 0 0 Even parity ?
  • Bit 2 1 0 1 1 1 0 1 0 0 0 Even parity ?
  • Bit 4 1 0 1 1 1 0 1 0 0 0 Odd parity ?
  • Bit 8 1 0 1 1 1 0 1 0 0 0 Even parity ?
  • In this case, only bit 4 had a bad parity check,
    so we know that it was the digit that was
    incorrect (of course 4 0 4, so we can also
    use our add the incorrect parity bits solution,
    too)

23
Hamming Code
  • Practice Apply a Hamming Code to the following
    3-digit binary number
  • 110

24
  • 110 Step 1 Write number with space for the
    parity bits
  • Number _ _ 1 _ 1 0
  • Bits 1 2 3 4 5 6

Step 2 Perform parity checks Bit 1 0 _ 1 _ 1
0 Bit 2 0 1 1 _ 1 0 Bit 4 0 1 1 1 1 0
So, the number to send is 011110
25
The number sent was 011110 Lets say the number
received was 011111 Check the parity bits Bit 1
0 1 1 1 1 1 Even parity ? Bit 2 0 1 1 1 1 1
Odd parity ? Bit 4 0 1 1 1 1 1 Odd parity
? Which bit is incorrect? 2 4 bit 6 is
incorrect.
26
  • More Hamming Code Practice
  • Produce a Hamming Code for 010111
  • What was the message sent if the following
    Hamming encoded message was received?
    011010010101
  • What is the overhead (i.e., number of extra bits)
    needed to send a 32-bit binary number using
    Hamming Code?

27
  • Final Words
  • Ive put these notes on Toolkit (under
    Resources?Class Notes)
  • Ive also written and uploaded (to Toolkit) a
    Python program to encode and decode a bitstream.
  • References
  • Your Textbook, p.20-21
  • http//users.cs.fiu.edu/downeyt/cop3402/hamming.h
    tml
  • http//en.wikipedia.org/wiki/Hamming_code
  • http//www.computing.dcu.ie/humphrys/Notes/Networ
    ks/data.hamming.html
  • Me Chris Gregg, chg5w_at_virginia.edu
Write a Comment
User Comments (0)
About PowerShow.com