Concept

How do you determine the value of a positive or negative number in two’s-complement notation?

Stephen Davies, Ph.D. Version 2.2.2 Through Discrete Mathematics A Cool Brisk Walk / Chapter 1

"If, however, it’s a negative number, then to discover the magnitude of that negative number you must flip all the bits and add one. This will give you a positive number which is the absolute value of your negative number. Easy example: take the byte 00100110. The left-most bit is a 0, which means it’s a positive number, and as we discovered above, the remaining 7 bits give a magnitude of 38. So this is the number 38. Harder example: take the byte 10100110. The left-most bit is a 1, which means it’s negative. Okay: negative what? How do we find the magnitude? Well, we “flip” all the bits (i.e., invert each one from 0 to 1 or vice versa) to get: 01011001 and then add one to the result: 01011001 + 1 = 01011010. This black magic produces the value 01011010₂, which converts to 90₁₀. This means that the original number, 10100110, corresponds to the value –90. “Flipping all the bits and adding one” is the cookbook procedure for taking the complement (negative) of a number in the two’s-complement scheme. It works in reverse, too. Let’s start with 90 this time and crank through the process again, making sure we get –90. Start with the binary representation of 90₁₀: 01011010. Flip all the bits to get: 10100101 and finally add one to the result: 10100101 + 1 = 10100110. We get 10100110, which was precisely the number we originally began with, and which we have already determined represents –90."

Related Ideas

How do you determine the value of a positive or negative number in two’s-complement notation? | Stephen Davies, Ph.D. Version 2.2.2 Through Discrete Mathematics A Cool Brisk Walk | Bifalgorithm | Bifalgorithm