1 / 11

Binary Addition

Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying: 0 + 0 → 0 0 + 1 → 1 1 + 0 → 1 1 + 1 → 10, which is a binary of the decimal 2 Example: 111001 + 10011 111001 + 10011

field
Télécharger la présentation

Binary Addition

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. Binary Addition • The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying: 0 + 0 → 0 0 + 1 → 1 1 + 0 → 1 1 + 1 → 10, which is a binary of the decimal 2 Example: 111001 + 10011 111001 + 10011 1001100

  2. Exercise • 101 + 101 • 111 + 10 • 100110 + 1101 • 01101 + 10111 • 1110111110 + 1010110011 • 100011 + 101110 + 110001 • 11111 + 101101 + 100100 Answers: • 1010 • 1001 • 110011 • 100100 • 11001110001 • 10000010 • 1110000

  3. Representing negative numbers: • One way to represent negative numbers would be to make the left most bit a sign bit. if the bit is 1, the number is negative, if the bit is 0 the number is positive! • So let's see what 15 and -15 looks like: 15: 10001111 -15 = 00001111 • Pretty clear distinction right? But here’s the thing. By using that bit for sign, now the maximum positive number we can represent is for 7 bits only:01111111: 127 instead of 255 before. • What happens if we make the entire byte all 1’s? 11111111: -127

  4. For unsigned numbers, we can represent, where bits = b, 2^b - 1so for a byte: it's 2⁸ - 1 = 255for 2 bytes or a short: 2¹⁶ - 1 = 65,535for 4 bytes or an int(on 32 bit systems): 2³² - 1 = 4294967295However for signed numbers: -(2^b-1) to (2^b-1) - 1 byte: -(2⁸ -1) to (2⁸ -1 - 1) = - 2⁷ to 2⁷ - 1 = -128 to 127short: -32768 to 32767and so on.

  5. 1's Complement • Negative number is stored as bit-wise complement of corresponding positive number. • Leftmost bit of positive number is 0. That of negative number is 1. 196 = 00000000 11000100 -196 = 11111111 00111011 • Can represent numbers from -32,767 to 32,767. • -215+1 .. 215-1 • But, still have two representations for zero: • 0 = 00000000 00000000 -0 = 11111111 11111111

  6. 2's Complement • Negative number obtained by taking 1's Complement of positive number and adding 1. 6713 = 00011000 00011101 1's Comp = 11100111 11100010 2's Comp = 11100111 11100011 • Word integer can represent numbers from -32,768 to 32,767. • -215 .. 215-1 • Byte integer can represent numbers from -128 to 127. • -27 .. 27-1 • One version of zero: 00000000 00000000

  7. Subtracting binary numbers (Sign Binary Addition) • The most common way of subtracting binary numbers is done by first taking the second value (the number to be subtracted) and apply what is known as two's complement, this is done in two steps: • complement each digit in turn (change 1 for 0 and 0 for 1). – 1’s complement • add 1 (one) to the result. – 2’s complement • By applying these steps you are effectively turning the value into a negative number, and as when dealing with decimal numbers, if you add a negative number to a positive number then you are effectively subtracting to the same value. In other words 25 + (-8) = 17, which is the same as writing 25 - 8 = 17.

  8. Example: 11101011 - 01100110 (23510 - 10210) • First we apply two's complement to 01100110 which gives us 10011010. • Now we need to add 11101011 + 10011010, however when you do the addition you always disregard the last carry, so our example would be: • which gives us 10000101, now we can convert this value into decimal, which gives 13310So the full calculation in decimal is 23510 - 10210 = 13310 (correct !!)

  9. Negative numbers • The previous example is subtracting a smaller number from a larger number. If you want to subtract a larger number from a smaller number (giving a negative result), then the process is slightly different. Usually, to indicate a negative number, the most significant bit (left hand bit) is set to 1 and the remaining 7 digits are used to express the value. In this format the MSB is referred to as the sign bit.

  10. 01111000 (120) 10000111 1’s complement + 1 2’s complement 10001000 Example:01100100 - 01111000 (10010 - 12010) • 1’s complement and 2’s complement the bigger number which is 120 • Then add the result of the 2’s complement to the smaller number (100). • 1’s complement and 2’s complement the result of the addition leaving the most significant bit (MSB) which is the sign bit as it is which is 1. • Now we can convert this value into a negative decimal, which gives -2010So, the full calculation in decimal is 10010 - 12010 = -2010 (correct !!) 10001000 +01100100 (100) 11101100 The sign bit stays the same. It is not 1’s complement and 2’s complement 1 1101100 1 0010011 + 1 1 0010100 (-20) Remember, 1 bit indicates a NEGATIVE sign, whereas 0 bit indicates positive.

  11. Exercise Do subtraction for the following: • 120 – 98 • 36 – 92 • 118 – 126 • 87 - 25

More Related