1 / 22

Math 260 scarbnick@gmail

Math 260 scarbnick@gmail.com. Two’s Complement Arithmetic. Two’s Complement Arithmetic. Our goal in these slides is showing how positive and negative numbers are stored in a byte, which is the standard unit for computer memory. Two’s Complement Arithmetic. Topics Binary Numbers

sera
Download Presentation

Math 260 scarbnick@gmail

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Math 260scarbnick@gmail.com Two’s Complement Arithmetic

  2. Two’s Complement Arithmetic • Our goal in these slides is showing how positive and negative numbers are stored in a byte, which is the standard unit for computer memory.

  3. Two’s Complement Arithmetic • Topics • Binary Numbers • Two’s Complement Basics • Using Two’s Complement to Store Numbers on a Computer • Adding Numbers on a Computer with Two’s Complement

  4. Two’s Complement ArithmeticBinary Numbers • Binary Numbers - A numerical system that represents integers in the following format: dn2n + dn -12 n-1 + … d1 21 + d0 20 • Each dI is either 0 or 1. • Example: 110012 = 1 x24 + 1 x 2 3 + 0 x 22 + 0 x21 + 1 x20 = 2510

  5. Two’s Complement ArithmeticBinary Numbers • 1 2 + 0 2 = 1 2 • 0 2 + 1 2 = 1 2 • 1 2 + 1 2 = 1 0 2 , since we must carry into the 2’s place • 11011 2 + 01101 2 = 101000 2

  6. Two’s Complement ArithmeticBinary Numbers • Example of Subtraction: 100 2 - 001 2

  7. Two’s Complement ArithmeticTwo’s Complement Basics • Definition of Two’s Complement: Let the term, a, represent a positive integer. The two’s complement of a relative to a fixed bit length n is the n-bit binary representation of 2n – a. • For simplicity, we will let n = 8, though on modern computers n is typically set to 32.

  8. Two’s Complement ArithmeticTwo’s Complement Basics • Computing the two’s complement of a number: • Note that 28 - a = (28 - 1) - a + 1 (b) 28 = 1 0000 0000 2 ( c ) 28 - 1 = 1 0000 0000 2 - 1 = 1111 1111 2 (continued on the next slide)

  9. Two’s Complement ArithmeticTwo’s Complement Basics • Computing the two’s complement of a number: (d) Assume a is in binary notation. To compute 1111 1111 2 – a, we just toggle the digits of a. Example: 1111 11112 ─ 0101 10112 1010 01002 (Continued on the next slide)

  10. Two’s Complement ArithmeticTwo’s Complement Basics • Computing the two’s complement of a number: • To compute 28 - a = (28 - 1) - a + 1 , we just toggle the digits of a and then add 1.

  11. Two’s Complement ArithmeticTwo’s Complement Basics • Example: Calculate the two complement of 1012 relative to a fixed bit length of 8 • Make sure to write 1012 with 8-bits: 0000 01012 • Toggle the bits to get 1111 10102 • Add 1 to arrive at 1111 10112

  12. Two’s Complement ArithmeticTwo’s Complement Basics • Suppose we have the two’s complement of a number. How do we find the original number? • Note that 2n – (2n – a) = a • Therefore, to find the original number we just calculate the two’s complement of the two’s complement!

  13. Two’s Complement ArithmeticUsing Two’s Complement to Store Numbers on a Computer • Based on the previous slides, the 8-bit two’s complement of the numbers, 1 through 128, have a leading bit of 1. • Verify the two’s complement of 12810 = 1000 00002 is 1000 00002

  14. Two’s Complement ArithmeticUsing Two’s Complement to Store Numbers on a Computer • This means 8-bits can represent all numbers between -12810 and 12710 by using the following formulas (a) if 0 ≤ a ≤ 127, store a in its binary representation. (b) if -128 ≤ a < 0, store a in the binary representation of 28 - |a | (In other words, the number, a, is represented by the two’s complement of the absolute value of a.)

  15. Two’s Complement ArithmeticUsing Two’s Complement to Store Numbers on a Computer • If the leading digit in an 8-bit representation is 1, the number is negative. To find the original number, calculate the two’s complement and then place a negative sign in front of this number. • Example: 1111 00002 is stored in a computer byte. The two’s complement of this number is 0001 00002. Therefore, we stored -1610 in the computer byte.

  16. Two’s Complement ArithmeticUsing Two’s Complement to Store Numbers on a Computer • If the leading digit in an 8-bit representation is 0, the number is positive. This means the largest number that can be stored in one-byte is 0111 11112 = 12710.

  17. Two’s Complement ArithmeticAdding Numbers on a Computer with Two’s Complement • Directly add bits • Ignore any overflow into the (n+1)st bit

  18. Two’s Complement ArithmeticAdding Numbers on a Computer with Two’s Complement Example 1: Add -610 + -10110 The two’s complement of 610 = 1111 10102 The two’s complement of 10110= 1001 10112 1111 10102 +1001 10112 = 1 1001 01012 Ignore the bit in 28 position to get 1001 01012. The leading bit is 1, so we have a negative number. The two’s complement of 1001 01012 = 0110 10112 = 10710. Therefore, the sum is equal to -10710

  19. Two’s Complement ArithmeticAdding Numbers on a Computer with Two’s Complement • Example 2: Add 5010 + ( - 3810 ) Since 5010 is positive, we use its binary representation = 0011 00102 Since -3810 is negative, we use the two’s complement of 38 = 1101 10102 = 0011 00102 +1101 10102 = 1 0000 11002 Ignore the bit in 28 position to get 0000 11002. The leading bit is 0, so we have a positive number. Therefore, the sum is equal to 0000 11002 = 1210

  20. New Topic – Hexadecimal Notation • Base 16 is referred to as Hexadecimal • Hexadecimal means we represent numbers as powers of 16. • In hexadecimal A = 10, B = 11, C = 12, D = 13, E = 14, and F = 15 • A hexadecimal number is written as dn 16n + dn -116 n-1 + … d1 161 + d0 160,where each di ranges from 0 to F.

  21. Hexadecimal Notation • Examples C16 = 1210 1AB16 = 1 x 162 + 10 x 161 + 11 x 160 = 42710 D00016 = 13 x 163 = 53,24810

  22. Hexadecimal Notation • To convert from binary to hexadecimal, break the binary number into groups with four digits and then convert each group to a hexadecimal number • Example: Convert 101 1101 00102 to hexadecimal • Answer is 5C216

More Related