liveBook · Manning

Biased encoding

As explained in JavaScript in Depth

A numeric encoding that stores a value after adding a fixed bias to it.

A signed value can be stored by adding a fixed bias before writing its bits. For an 8-bit representation, the bias is typically 128: -128 becomes 0, while 1 becomes 129. The stored patterns therefore preserve ordinary unsigned ordering, but they no longer support natural signed arithmetic. section 3.1.1

8-bit translation

With bias 128, encode -128 as -128 + 128 = 0, or 00000000. Encode 1 as 1 + 128 = 129, or 10000001. Decoding reverses the operation by subtracting 128. section 3.1.1

Use the contrast with two’s complement to identify the practical trade-off:

  • Compare signed and unsigned values using the same ordering. section 3.1.1
  • Account for the bias during arithmetic; adding two signed values is more complicated. section 3.1.1
  • Prefer two’s complement when uniform binary arithmetic matters, because it works naturally for signed and unsigned operations. section 3.1.1

How JavaScript in Depth covers it

How deep it goes
Defines the bias, works through 8-bit examples, and stops at comparison and arithmetic trade-offs.
Examples
Uses 8-bit values, bias 128, and the encodings of -128 and 1.
What it assumes
Assumes binary representation and signed-versus-unsigned values while explaining the bias mechanism directly.
What you can do after
Evaluate/understand when biased ordering is useful and why arithmetic requires explicit bias handling.
How it connects
Connects biased encoding to signed numbers, unsigned numbers, binary arithmetic, and two’s complement.

Related concepts

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