liveBook · Manning

Sign bit

As explained in JavaScript in Depth

A designated bit that indicates whether an encoded numeric value is positive or negative.

Why does the eight-bit pattern 11111111 mean −1 rather than 255? It depends on whether the bits are interpreted as unsigned binary or as a signed value. In the signed layout, the most significant bit is the sign bit: 0 indicates a nonnegative value, and 1 indicates a negative value. section 3.1.1

Two’s complement does not encode a negative number by merely switching its sign bit. To obtain −1, start with 00000001, invert every bit, then add 1. The result, 11111111, adds to 00000001 to produce zero when the carry beyond eight bits is discarded. An unsigned byte spans 0–255; an eight-bit two’s-complement value spans −128–127. section 3.1.1

Table 3.2 Binary addition showing why -1 is represented as all 1s
Bit position
7
6
5
4
3
2
1
0
-1
1
1
1
1
1
1
1
1
1
0
0
0
0
0
0
0
1
Carry
1
1
1
1
1
1
1
 
Result
0
0
0
0
0
0
0
0

The carry column shows why adding the eight-bit encodings of −1 and 1 leaves eight zero bits. section 3.1.1

Rule of thumb: a leading 1 signals a negative value only when the bits are being interpreted using a signed layout. A double-precision floating-point layout also has a sign bit, but divides its other bits into exponent and significand fields. section 3.1.1

How JavaScript in Depth covers it

How deep it goes
Works through eight-bit two’s-complement encoding and addition, while identifying floating-point sign bits without developing their encoding here.
Examples
Uses the encoding of −1 and its addition to 1 to show why the overflowing carry is discarded.
What it assumes
Builds from elementary binary place values before introducing signed encoding.
What you can do after
Evaluate/understand whether an eight-bit pattern represents a negative two’s-complement value or a nonnegative unsigned value.
How it connects
Connects the sign bit to unsigned binary, two’s complement, binary addition, and double-precision floating-point layout.

Also explained in

Compare how 3 books explain sign bit →

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.