How a Computer Knows a Number Is Negative
It doesn't, at least not from the bits. A register holds a pattern like , and that's to an instruction that reads it as unsigned and to one that reads it as two's complement. The meaning lives in the instruction, not in the bits.
What the symbols mean
- Unsigned value
What an unsigned instruction sees in the bits.
- Bit i
The bit in place i, 0 or 1, counting from 0 on the right.
- Width
How many bits the register has: 8, 16, 32 or 64.
What the symbols mean
- Signed value
What a signed instruction sees in the same bits.
- Top bit
Worth −2ⁿ⁻¹ in the signed reading instead of +2ⁿ⁻¹.
Explanation
A negative number, inside a computer, is simply a bit pattern that some instruction has agreed to read as negative. That's the honest answer to "how does the computer know?": it doesn't, not from the bits. A register is wires, each high or low; there's no minus sign stored anywhere and no hidden tag saying "this one is signed." The formulas above are the two common ways of reading those wires. is the unsigned reading, where each bit is worth . is the signed (two's complement) reading, which is the same except the top bit is worth . Same bits, two rules for adding them up. That really is the whole secret; the rest of this lesson is why it was designed this way.
So who decides? The instruction. Take the byte . As unsigned it's . As signed, the top bit counts instead of , so it's . Which one is right? Whichever the program asked for. Processors have pairs of instructions that differ only in how they read the bits: x86 has ja ("jump if above", unsigned) and jg ("jump if greater", signed), shr and sar for shifting right, movzx and movsx for widening; RISC-V has bltu and blt, lbu and lb. In C you never pick these by hand: the type does it. Declare uint8_t and the compiler emits the unsigned versions; declare int8_t and it emits the signed ones, over the very same byte in memory.
uint8_t u = 0xFB; // the byte 1111 1011, read as 251
int8_t s = -5; // the same byte, read as -5
u > 10 // true: the compiler emits an unsigned compare
s > 10 // false: the compiler emits a signed compare
The top bit's weight is . You met this in Two's Complement. Put the two readings side by side and notice that they agree on every bit except the top one, which adds to and to . The difference is , so
If the top bit is 0, the readings agree. If it's 1, the signed reading is the unsigned one minus . That's the fastest way to read a negative pattern: is , minus , which is .
The range. The smallest signed value has only the top bit set: . The largest has every bit except the top one: , the geometric series from Binary and Hexadecimal. So a signed byte holds to . It's lopsided by one because zero uses up one of the patterns whose top bit is 0.
Both readings live on one wheel. Since and differ by or by , they're always congruent mod . Picture the patterns around a clock face: adding 1 steps clockwise, and after comes . Unsigned names the spots to ; signed names the same spots to . There's an animation further down that walks a 4-bit pointer around this wheel. Any arithmetic the hardware does mod lands on the same spot whichever names you use, and that's the key to the next point.
Why two's complement won. Early machines tried two other ways to store negatives. Sign-magnitude uses the top bit as a minus sign and the rest as the size, so in 4 bits is . Ones' complement stores as with every bit flipped, so is . Both have two zeros: sign-magnitude has and ( and ), ones' complement has and . That wastes a pattern, and every "is it zero?" test has to check two patterns. Worse, a plain adder gives wrong answers. In sign-magnitude, is , which a binary adder turns into ; keep 4 bits and you're left with , zero. A sign-magnitude machine needs extra hardware to compare sizes, subtract the smaller from the larger and pick a sign. (Ones' complement gets closer, but any carry out of the top has to be added back in at the bottom, an "end-around carry".)
Two's complement stores as . The adder already throws away anything that reaches , so adding the pattern for is subtracting , mod . One zero, one adder, no special cases: the same circuit adds signed and unsigned numbers, and only the instructions that compare, widen, divide or shift ever need to know which reading you meant. We'll build that adder in the ALU unit.
A common mistake. It's tempting to think the computer checks the top bit to decide whether a number is negative every time it touches it. It looks at the top bit only when an instruction asks a signed question: a signed compare, an arithmetic shift, a sign-extending load. To an add instruction, is just eight bits, and it adds them exactly the same way whether you meant or .
See it
Worked examples
A register holds the byte . What number is it as unsigned, and as signed?
- 1
Start with the unsigned reading, where every bit is worth its usual power of 2. Number the bits from the right, starting at 0. The 1s sit in places 7, 6, 5, 2 and 1.
Add and in 4 bits, once with two's complement and once with sign-magnitude, using a plain binary adder both times.
- 1
Two's complement first. Write in 4 bits: .
Practice
Type your answer and check it. Solve them all to complete the lesson.
An 8-bit register holds . What value does a signed (two's complement) instruction see?
The byte is read once by an unsigned instruction and once by a signed one. Enter both readings, unsigned first.
What are the smallest and largest values a signed 12-bit number can hold? Enter both, smallest first.
An 8-bit add instruction adds and and produces . Which statement is right?
A temperature chip sends a signed 16-bit reading in units of °C. The two bytes arrive as 0xFF and then 0xD8 (high byte first). What temperature is it, in °C?
A temperature chip sends signed (two's complement) 16-bit readings in units of °C, but a buggy driver reads every 16-bit word as unsigned. For any temperature below zero, by how many °C is the buggy reading too high?
How many different values can an 8-bit sign-magnitude number represent?
In 32-bit two's complement, the most negative number is a 1 followed by 31 zeros. What does an unsigned instruction read in those same 32 bits?
The hardest one here. Get it, and you've got this lesson.
A 10-bit sensor sends signed two's complement samples. Four samples have true values , , and , but a buggy program reads each 10-bit pattern as unsigned and then averages the four. What average does the buggy program report, and what is the true average? Enter both, buggy first.
Where it's used
Every language makes this choice for you through types. In C, uint8_t and int8_t compile to different compare, shift, divide and widening instructions over the same bytes. Java has no unsigned byte, so a byte read from a file as 0xFB comes back as , and programs write b & 0xFF to get . File formats and network protocols spell out which reading each field uses, because the bytes alone can't say.