liveBook · Manning

Sign representation

As explained in Modern C

A sign representation is a rule for encoding negative signed values using a sign bit and the remaining representation bits.

A sign representation encodes signed values with a sign bit and the remaining representation bits. A zero sign bit denotes a positive value; a one sign bit denotes a negative value. Positive signed values use the same representation as corresponding unsigned values. section 5.7.5

C permits three sign representations:

  • Sign and magnitude keeps the positive magnitude and uses the sign bit as a minus sign. section 5.7.5
  • Ones’ complement complements every bit of the corresponding positive value. section 5.7.5
  • Two’s complement performs the same bit arithmetic as unsigned values, while representations with a high-order one are interpreted as negative. section 5.7.5

Negating 240 in 16 bits

For unsigned arithmetic, negate A by computing ~A + 1. Starting with 240, or 0000000011110000, complementing gives 65295, or 1111111100001111; adding one gives 65296, or 1111111100010000. Two’s complement assigns that resulting bit pattern its negative signed interpretation. section 5.7.5

bool is_negative(unsigned a) {
  unsigned const int_max = UINT_MAX/2;
  return a > int_max;
}
bool is_signed_less(unsigned a, unsigned b) {
  if (is_negative(b) && !is_negative(a)) return false;
  else return a < b;
}


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":17},{\"line\":0,\"ch\":25}],[{\"line\":1,\"ch\":2},{\"line\":1,\"ch\":10}],[{\"line\":4,\"ch\":20},{\"line\":4,\"ch\":28}],[{\"line\":1,\"ch\":2},{\"line\":1,\"ch\":16}],[{\"line\":1,\"ch\":27},{\"line\":1,\"ch\":35}],[{\"line\":2,\"ch\":2},{\"line\":2,\"ch\":8}],[{\"line\":5,\"ch\":41},{\"line\":5,\"ch\":47}],[{\"line\":6,\"ch\":7},{\"line\":6,\"ch\":13}],[{\"line\":0,\"ch\":17},{\"line\":0,\"ch\":25}],[{\"line\":1,\"ch\":2},{\"line\":1,\"ch\":10}],[{\"line\":4,\"ch\":20},{\"line\":4,\"ch\":28}],[{\"line\":0,\"ch\":17},{\"line\":0,\"ch\":25}],[{\"line\":1,\"ch\":2},{\"line\":1,\"ch\":10}],[{\"line\":4,\"ch\":20},{\"line\":4,\"ch\":28}],[{\"line\":5,\"ch\":2},{\"line\":5,\"ch\":4}],[{\"line\":2,\"ch\":2},{\"line\":2,\"ch\":8}],[{\"line\":5,\"ch\":41},{\"line\":5,\"ch\":47}],[{\"line\":6,\"ch\":7},{\"line\":6,\"ch\":13}],[{\"line\":5,\"ch\":48},{\"line\":5,\"ch\":53}],[{\"line\":6,\"ch\":2},{\"line\":6,\"ch\":13}]]"}
!@%STYLE%@!

This implementation compares unsigned bit patterns as signed values by treating values above UINT_MAX/2 as negative. section 5.7.5

Sign representation is part of a type’s binary representation: the standard leaves some representation details platform-dependent, but programs can observe the resulting model through values, constants, operators, and library interfaces. section 5.1.3

How Modern C covers it

How deep it goes
Defines three representations and works through two’s-complement negation, but stops short of detailed object-storage mechanics.
Examples
Uses a 16-bit negation table, signed-comparison functions, and a representation-selection lookup example.
What it assumes
Assumes familiarity with binary and unsigned arithmetic while establishing sign bits and signed representations explicitly.
What you can do after
Evaluate/understand signed bit patterns and implement comparisons that account for their signed interpretation.
How it connects
Connects signed integers with unsigned arithmetic, binary representation, object representation, and platform-defined type properties.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.