# What is negative zero?

2022 June 16

[info]

Sometimes people ask me where "negative zero" (the name of my domain) comes from. Here's a quick computer science lesson to answer that question.

## What is negative zero?

When we represent numbers in computers, we use binary. Let's suppose we're working with a single byte, which is 8 bits. A "bit" is a one or a zero, a single binary digit.

We can represent non-negative integers this way. For example...

Binary (base 2) Decimal (base 10)
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 0101 5
0000 0110 6
... ...

Now, the question arises: How do we represent negative integers?

There are multiple options. One of them is called signed magnitude. In this representation, we reserve the first bit as the sign (0 means positive, and 1 means negative), and the rest of the bits represent the magnitude of the number (how far it is from zero, in either the positive or negative direction).

The positive examples above don't change, but now we also have...

Binary (base 2) Decimal (base 10)
1000 0000 -0
1000 0001 -1
1000 0010 -2
1000 0011 -3
1000 0100 -4
1000 0101 -5
1000 0110 -6
... ...

As you might have noticed, 0 appears in both tables! That's right, with signed magnitude, we can represent both positive zero (0000 0000) and negative zero (1000 0000)!

## Why did I choose the name "negative zero"?

Honestly, I just thought it was cool that the existence of a distinct negative zero was a consequence of this way of representing numbers. It's not much deeper than that.

## So what's 0x80?

I use 0x80 as an account identifier online. It's short and easy to type and identify, but not easily pronounceable. (This is desirable since I don't really want it to be a "name".)

Working with binary is a pain. For that reason, we often represent numbers in hexadecimal (base 16) instead. Hexadecimal goes from 0-9, then A-F, for a total of 16 possibilities. Since 16 is a power of 2 (24), binary and hexadecimal have a nice relationship: specifically, 4 bits can be represented as a single hexadecimal character, meaning that one byte can be represented by just 2 hexadecimal characters.

To indicate that hexadecimal is being used, we often precede hexadecimal strings with "0x". For example...

Binary (base 2) Decimal (base 10) Hexadecimal (base 16)
0000 0000 0 0x00
0000 0001 1 0x01
0000 0010 2 0x02
0000 0011 3 0x03
0000 0100 4 0x04
0000 0101 5 0x05
0000 0110 6 0x06
0000 0111 7 0x07
0000 1000 8 0x08
0000 1001 9 0x09
0000 1010 10 0x0A
0000 1011 11 0x0B
0000 1100 12 0x0C
0000 1101 13 0x0D
0000 1110 14 0x0E
0000 1111 15 0x0F
0001 0000 16 0x10
... ... ...
0111 1111 127 0x7F
1000 0000 -0 0x80
1000 0001 -1 0x81
... ... ...

...and so on. As you can see here, 0x80 is just negative zero as a single byte signed magnitude integer.