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.
