Bifalgorithm post · Brisk · Chapter 1

How are negative numbers represented in binary?

How are negative numbers represented in binary? (Brisk · Chapter 1) — "7.4. BINARY (BASE 2) 179 Binary representation schemes That’s mostly all there is to it.…

Ideas in this flowchart

  • Binary representation schemes for negative numbers
  • Positive numbers and zero can be represented with an ordinary place-value scheme, but negative numbers require a different treatment.
  • Three schemes are available for representing negative numbers, each with strengths and weaknesses.
  • Unsigned representation does not allow negative numbers.
  • With one byte, all 256 bit patterns can represent nonnegative numbers.
  • Unsigned representation provides the greatest possible range of positive numbers.
  • It is useful for values such as heightInInches, which cannot meaningfully be negative.
  • Alternative schemes reserve some bit patterns for negative numbers, leaving fewer patterns for positive numbers.
  • Sign-magnitude representation uses one bit for the sign and the remaining bits for the magnitude.
  • The left-most bit is used as the sign bit and cannot also represent part of the magnitude.
  • A sign bit of 0 indicates a positive number, while a sign bit of 1 indicates a negative number.
  • The sign-magnitude pattern 0 0100110 represents 38 because the magnitude is 32 + 4 + 2.
  • The pattern 1 0100110 represents −38 because the magnitude is unchanged and the sign bit is 1.
  • Sign-magnitude representation reduces the positive range from 8 bits to 7 bits, making 127 the highest possible positive value.
Feed/

How are negative numbers represented in binary?

Channel

Brisk

1 page

"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."

Flowchart preview
Series cover
👍
0
0 comments0 shares

Sign in to view and post comments.