Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Twos Complement

AllenHeard
September 27, 2016

Twos Complement

Year 12 Lesson

AllenHeard

September 27, 2016
Tweet

More Decks by AllenHeard

Other Decks in Education

Transcript

  1. BINARY REFRESHER USING ANY METHOD YOU KNOW, CONVERT THE FOLLOWING

    DENARY NUMBERS INTO BINARY: 11 28 31 68 73 161
  2. Can’t we just put a minus sign ▪ The negative

    representation of a denary number in binary is called the two’s complement. ▪ Example: Take the number 2710 In binary this is represented as:
  3. The sign bit ▪ To get our negative representation of

    27 we need to add what is called a sign bit to represent +/- (0 (dim) for positive 1 (un) for negative (positif/negyddol), this is added to the far left of our binary number. ▪ So right now our (positive) 27 including the sign bit looks like this:
  4. Next, it’s time to flip! ▪ The next step to

    get our two’s complement is to flip all the bits, if it’s a 0 change it to a 1 and vice versa. ▪ So now our 27 with the added sign bit and bits flipped is:
  5. Finally, add 1 ▪ The final step is to add

    1 to our binary number, this is then our two’s complement of 2710 ▪ Here you can see the most significant bit (farthest left) has changes to a 1 (or minus)
  6. One more example? ▪ Convert to binary ▪ Add sign

    bit ▪ Flip the bits ▪ Add one
  7. Let’s get converting! Show the following denary numbers in two’s

    complement, show the four stages for each (convert to binary, add sign bit, flip bits, add one) ▪7 ▪15 ▪26 ▪37 ▪49
  8. Working backwards ▪ To convert a binary two’s complement number

    back to its positive counterpart we do the same thing, simply flip the bits and add one! ▪ So 100101 ▪ Becomes:
  9. Let’s get positive! Show the following two’s complement binary numbers

    in denary, show the three stages for each (flip bits, add one, convert to denary) ▪101 ▪10010 ▪1011110 ▪1000111 ▪10100110
  10. Subtraction example: ▪ Let’s do 8 – 16 ▪ 8

    – 16 is the same as 8 + -16 ▪ 8 = 1000 ▪ 16 is 10000
  11. Subtraction example: ▪ 8 = 1000 and 16 is 10000

    ▪ To get -16 we add a sign bit, 010000 ▪ Flip the bits 101111 ▪ …and add one 110000 ▪ Then add the two numbers together using our binary addition rules: Binary addition rules: 0 + 0 = 0 0 + 1 =1 1 + 1 = 0 and carry 1 1+1+1 = 1 and carry 1 1000 110000 111000 This represents our negative answer (sign bit = 1)
  12. Subtraction example: ▪ Now flip the bits 000111 ▪ Add

    one 001000 which is 8 ▪ So 8 – 16 = -8 1000 110000 111000
  13. Subtraction! Do the following subtractions showing all working. ▪10 –

    11 ▪15 – 21 ▪26 – 42 ▪32 – 56 ▪48 – 63
  14. Subtraction worked examples ▪ 10 – 11 ▪ 15 –

    21 ▪ 26 – 42 ▪ 32 – 56 ▪ 48 – 63
  15. Why? ▪ You already know computers use binary for everything!

    ▪ This lesson has shown you how we represent negative numbers in binary. ▪ You now also know how computers can do subtraction using twos complement and binary addition. Lesson +’s Lesson –’s