Concept

How are negative numbers represented in binary?

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

"7.4. BINARY (BASE 2) 179 Binary representation schemes That’s mostly all there is to it. But there’s one thing we haven’t discussed yet, and that’s negative numbers. We know how to represent any positive number (or zero) with an ordinary place value scheme. But how do we store a number like − 5 ? There are three different schemes for treating negative numbers, each with its strengths and weaknesses. Unsigned The simplest scheme is called unsigned , and it simply means that we don’t allow negative numbers. For one byte, we have 256 different bit patterns at our disposal, and we might just choose to allocate them all to represent positive numbers, so as to get the widest range. This makes sense for, say, a C++ program variable called heightInInches which we know can never meaningfully be negative (no one has a negative height). The advantage of this scheme is simply that we can represent the greatest possible range of positive numbers, which is sometimes the goal. Each of the alternative schemes carves off a chunk of these available bit patterns and devotes them to representing negative numbers, leaving fewer left over for positive numbers. There’s no free lunch: you have to decide how you want to “spend” your available bit patterns depending on what values you need to represent. Sign-magnitude The sign-magnitude scheme is probably the first thing you’d think of to solve the negative number representation problem. We need to store the sign of the number somehow, and a sign is inherently a two-valued thing (either positive or negative), so why not peel off one of the bits and use it to represent the sign? The remaining bits can then be used in the ordinary way to represent the magnitude of the number. The way this is most often done is to take the left-most bit and use it as the sign bit . This bit now has no other meaning . It can’t “double” as the 128’s place, because then there’d be no way to distinguish between, say, 129 and − 129 (each would be represented with 10000001 .) No, the sign bit must be considered “spent money,” and its expressive power cannot be reclaimed to also represent part of the magnitude. By convention, if the sign bit is 0 this represents a positive number, and a sign bit of 1 represents a negative number. (That might seem counterintuitive, but hey, that’s the way it is.) So this number in sign-magnitude: 0 0100110 represents the decimal number 38. That’s because the sign bit (bolded, on the far left) is 0, which means the number is positive. The magnitude of the number is contained in the other 7 bits, which gives 32 + 4 + 2 = 38. This number, on the other hand: 1 0100110 represents − 38 . The magnitude is the same, but the sign bit is 1 so this pattern now “means” a negative number. Clearly we have reduced our range of positive numbers in exchange for the ability to also store negatives. We have 7 bits of range instead of 8, so instead of 255, our highest possible value is merely 127."

Related Ideas

How are negative numbers represented in binary? | Stephen Davies, Ph.D. Version 2.2.2 Through Discrete Mathematics A Cool Brisk Walk | Bifalgorithm | Bifalgorithm