Hamming and Twos Complement

Embed Size (px)

Citation preview

  • 8/14/2019 Hamming and Twos Complement

    1/27

    More About Twos Complement

    Twos Complement is used to represent both

    positive and negative numbers, but the

    positive numbers are the same as they wouldbe without the twos complement

    representation.

    Examples: 011012=1310 111012=-310

  • 8/14/2019 Hamming and Twos Complement

    2/27

    To Convert apositive number to a negative2s

    complement: 001102=610Convert 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 theright), and copy 0s until you get to a 1 (also copy the1): 10

    (b) Then flip the rest of the bits:11010

  • 8/14/2019 Hamming and Twos Complement

    3/27

    Why 2s

    Complement?

    Only one form of 0.

    Easyto add:

    Just Add!

    0100 ( 410)

    +1101 (-310)

    -----

    0001 ( 110)

    It works!

    Twos Complement Decimal

    0111 7

    0110 6

    0101 5

    0100 4

    0011 3

    0010 2

    0001 1

    0000 0

    1111 -1

    1110 -2

    1101 -3

    1100 -4

    1011 -5

    1010 -6

    1001 -7

    1000 -8

  • 8/14/2019 Hamming and Twos Complement

    4/27

    Why 2s

    Complement?Just Add!

    1011 (-510)

    +0010 ( 210)

    -----

    1101 (-310)

    It works!

    Wow.

    (by the way, you detect

    overflow by looking at thelast two carry bits: iftheyre different, youvegot an overflow condition)

    Twos Complement Decimal

    0111 7

    0110 6

    0101 5

    0100 4

    0011 3

    0010 2

    0001 1

    0000 0

    1111 -1

    1110 -2

    1101 -3

    1100 -4

    1011 -5

    1010 -6

    1001 -7

    1000 -8

  • 8/14/2019 Hamming and Twos Complement

    5/27

    Why 2s Complement?

    Subtraction is easy, too!

    To subtract, simply convert thenumber youre subtractingaway to its 2s complement,and then add:

    1011 (-510)

    -0010 -( 210)

    111

    1011 (-510)

    +1110 +(-210)

    ---- -----

    1001 (-710)

    Ignore the carry out

    Twos Complement Decimal

    0111 7

    0110 6

    0101 5

    0100 4

    0011 3

    0010 2

    0001 1

    0000 0

    1111 -1

    1110 -2

    1101 -3

    1100 -4

    1011 -5

    1010 -6

    1001 -7

    1000 -8

  • 8/14/2019 Hamming and Twos Complement

    6/27

    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)

    Twos

    Complement

    Dec.

    0111 7

    0110 6

    0101 5

    0100 4

    0011 3

    0010 2

    0001 10000 0

    1111 -1

    1110 -2

    1101 -31100 -4

    1011 -5

    1010 -6

    1001 -7

    1000 -8

    1111011 (-510)

    +0010 +(+210)

    ---- -----

    1101 (-310)

    Ignore the carry out

  • 8/14/2019 Hamming and Twos Complement

    7/27

    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/14/2019 Hamming and Twos Complement

    8/27

    Hamming Code

    Obviously, we want to be able to detectan 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 a4-bit number. But we have no idea if it is correct

    or not.

  • 8/14/2019 Hamming and Twos Complement

    9/27

    Parity

    Well, then, what can we do?

    One idea is to add aparitybit. This adds an extra bit tothe number, say as the most significant bit. The sendercounts the number of ones in the number, and if it isan odd number, the parity bit becomes a 1 . This iscalled an evenparity bit, because when you look at thewhole string, it is even.

    Examples:

    1001There is an even number of 1s, so we send 01001.

    1110An odd number of 1s, so we send 11110.

  • 8/14/2019 Hamming and Twos Complement

    10/27

    Parity

    Even parity bit examples:1001There is an even number of 1s, so we send 01001.

    1110An 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 detectedan error.

    Notes:

    This only detects a single error (e.g., if two ones were bothsent 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 greatanswer)

  • 8/14/2019 Hamming and Twos Complement

    11/27

    Hamming Code

    Really, what we want is the ability to detect an

    error, and also to correctthe error. How can

    we do this?

  • 8/14/2019 Hamming and Twos Complement

    12/27

    Hamming Code

    In 1950, Richard Hamming came up with a

    clever method to both detect andcorrect

    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

  • 8/14/2019 Hamming and Twos Complement

    13/27

    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 45 6 7 89 10

  • 8/14/2019 Hamming and Twos Complement

    14/27

    Hamming CodeNumber: __1 _1 0 1 _0 0

    Bit: 1 2 3 45 6 7 89 10

    Step 3: Each parity bit calculates the parity for some of thebits in the code word. The position of the parity bitdetermines the sequence of bits that it alternately checksand 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 evenparityaltogether, so we basically count the number of 1s, and if it

    is an odd number, the parity bit for that position becomes a1.

  • 8/14/2019 Hamming and Twos Complement

    15/27

    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 45 6 7 89 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 45 6 7 89 10

  • 8/14/2019 Hamming and Twos Complement

    16/27

    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 45 6 7 89 10

    There are two ones, so bit 2 becomes a 0 to

    make the parity even:

    Number: 101 _1 0 1 _0 0

    Bit: 1 2 3 45 6 7 89 10

  • 8/14/2019 Hamming and Twos Complement

    17/27

    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 45 6 7 89 10

    There are two ones, so bit 4 becomes a 0 to

    make the parity even:

    Number: 101 01 0 1 _0 0

    Bit: 1 2 3 45 6 7 89 10

  • 8/14/2019 Hamming and Twos Complement

    18/27

    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 45 6 7 89 10

    There are zero ones, so bit 8 becomes a 0 to

    make the parity even:

    Number: 101 01 0 1 00 0

    Bit: 1 2 3 45 6 7 89 10

    The number we send: 1 1 1 1

  • 8/14/2019 Hamming and Twos Complement

    19/27

  • 8/14/2019 Hamming and Twos Complement

    20/27

    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, 2+4=6, so bit six is incorrect, it should be a

    0.

  • 8/14/2019 Hamming and Twos Complement

    21/27

    Hamming Code

    The last step is to fix the incorrect bit, and then extract theoriginal number:

    1010111000 1010101000 1010101000

    The original number sent was 110100

    What if only one bit check was incorrect? Then the checkdigit was incorrect.

    Ch k 1011101000

  • 8/14/2019 Hamming and Twos Complement

    22/27

    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 weknow 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)

  • 8/14/2019 Hamming and Twos Complement

    23/27

    Hamming Code

    Practice: Apply a Hamming Code to the following 3-

    digit binary number:

    110

    0

  • 8/14/2019 Hamming and Twos Complement

    24/27

    110 Step 1: Write number with space for the parity bits:

    Number: _ _1 _ 1 0

    Bits: 1 23 45 6

    Step 2: Perform parity checks:Bit 1: 0_ 1 _ 1 0

    Bit 2: 0 11 _ 1 0

    Bit 4: 0 1 1 11 0

    So, the number to

    send is1111

  • 8/14/2019 Hamming and Twos Complement

    25/27

    M H i C d P ti

  • 8/14/2019 Hamming and Twos Complement

    26/27

    More Hamming Code Practice:

    Produce a Hamming Code for: 010111What was the message sent if the following Hamming

    encoded message was received? 011010010101What is the overhead (i.e., number of extra bits) neededto send a 32-bit binary number using Hamming Code?

    Fi l W d

  • 8/14/2019 Hamming and Twos Complement

    27/27

    Final Words:Ive put these notes on Toolkit (under ResourcesClass Notes)

    Ive also written and uploaded (to Toolkit) a Python program to

    encode and decode a bitstream.

    References:

    Your Textbook, p.20-21http://users.cs.fiu.edu/~downeyt/cop3402/hamming.html

    http://en.wikipedia.org/wiki/Hamming_code

    http://www.computing.dcu.ie/~humphrys/Notes/Networks/da

    ta.hamming.html

    Me: Chris Gregg, [email protected]

    http://users.cs.fiu.edu/~downeyt/cop3402/hamming.htmlhttp://en.wikipedia.org/wiki/Hamming_codehttp://www.computing.dcu.ie/~humphrys/Notes/Networks/data.hamming.htmlhttp://www.computing.dcu.ie/~humphrys/Notes/Networks/data.hamming.htmlhttp://www.computing.dcu.ie/~humphrys/Notes/Networks/data.hamming.htmlhttp://www.computing.dcu.ie/~humphrys/Notes/Networks/data.hamming.htmlhttp://en.wikipedia.org/wiki/Hamming_codehttp://users.cs.fiu.edu/~downeyt/cop3402/hamming.html