Presentation is loading. Please wait.

Presentation is loading. Please wait.

Calculating Two’s Complement. The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of.

Similar presentations


Presentation on theme: "Calculating Two’s Complement. The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of."— Presentation transcript:

1 Calculating Two’s Complement

2 The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of two. A two's-complement system or two's-complement arithmetic is a system in which negative numbers are represented by the two's complement of the absolute value, this system is the most common method of representing signed integers on computers. A system of two's-complement arithmetic represents negative integers by counting backwards and wrapping around. The boundary between positive and negative numbers may theoretically be anywhere (as long as you check for it). For convenience, all numbers whose left- most bit is 1 are considered negative. The largest number representable this way with 4 bits is 0111 (7) and the smallest number is 1000 (-8).

3 If all bits are 1, the value is −1. If the sign bit is 1 but the rest of the bits are 0, the value is the most negative number, −2 n−1 for an n-bit number. The absolute value of the most negative number cannot be represented with the same number of bits, because it is exactly 1 greater than the most positive number that two's complement can represent in the given number of bits. Two's complementDecimal 01117 01106 01015 01004 00113 00102 00011 00000 1111−1 1110−2 1101−3 1100−4 1011−5 1010−6 1001−7 1000−8 Two’s complement using a 4 bit integer.

4 A two's-complement 8-bit binary numeral can represent every integer in the range −128 to +127. If the sign bit is 0, then the largest value that can be stored in the remaining seven bits is 2 7 − 1, or 127. Calculating two's complement In finding the two's complement of a binary number, the bits are inverted, or "flipped", by using the bitwise NOT operation; the value of 1 is then added to the resulting value. Bit overflow is ignored, which is the normal case with zero. For example, beginning with the signed 8-bit binary representation of the decimal value 5, using subscripts to indicate the base of a representation needed to interpret its value: 00000101 2 = 5 10 The most significant bit is 0, so the pattern represents a non-negative value. To convert to −5 in two's-complement notation, the bits are inverted; 0 becomes 1, and 1 becomes 0: 11111010 At this point, the numeral is the ones' complement of the decimal value 5. To obtain the two's complement, 1 is added to the result, giving: 11111011 2 = − 5 10

5 The result is a signed binary number representing the decimal value −5 in two's- complement form. The most significant bit is 1, so the value represented is negative. The two's complement of a negative number is the corresponding positive value. For example, inverting the bits of −5 (above) gives: 00000100 And adding one gives the final value: 00000101 2 = 5 10 The value of a two's-complement binary number can be calculated by adding up the power-of-two weights of the "one" bits, but with a negative weight for the most significant (sign) bit; for example: 11111011 2 = − 128 + 64 + 32 + 16 + 8 + 0 + 2 + 1 = ( − 2 7 + 2 6 +...) = − 5


Download ppt "Calculating Two’s Complement. The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of."

Similar presentations


Ads by Google