Why does an eight-bit encoding of −1 contain nothing but 1s? Ordinary binary uses each bit position as a power of two, which accounts for nonnegative whole numbers but not negative ones. Two's complement gives signed integers a binary encoding while preserving ordinary binary addition. section 3.1.1 section 3.5
To encode a negative value in a fixed number of bits, write its positive magnitude in binary, flip every bit, then add 1. For example, in eight bits, 2 is 00000010; flipping its bits gives 11111101, and adding 1 gives 11111110 for −2. The most significant bit signals a negative value, but the remaining bits are not simply its magnitude. section 3.1.1 section 3.5
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
|
Eight-bit addition makes the choice of encoding visible: 11111111 (−1) plus 00000001 (1) produces 00000000, with the carry passing through the bit positions. section 3.1.1
Keep the encoding distinct from JavaScript's usual numeric representation. JavaScript Numbers are Binary64 floating-point values; two's complement explains signed-integer encoding, not the layout of every Number. V8 can separately optimize integers in its SMI range. section 3.1.1 section 3.5





