How to Represent Integers in Binary

Unsigned Integers

bits decimal
0 0 0 0 = 0
8 4 2 1

"Unsigned" means that the integer has no negative sign, it's either zero or a positive number. With four bits we can represent 16 different numbers, from 0 to 15.

When using bits, the amount of numbers we can represent is always a power of two since a bit has two states, zero or one. The formula to calculate how many numbers you can represent with a given amount of bits is 2 nr of bits available .

Signed Integers with Sign Bit

bits decimal
0 0 0 0 = 0
- 4 2 1

Signed Integers with Ones' Complement

bits decimal
0 0 0 0 = 0
-7 4 2 1
1111 ← All ones
- 0 0 0 0 ← Number to negate (0)
1 1 1 1 ← Ones' complement (-0)

To negate a number using ones' complement, we flip all the bits. Flipping bits is equivalent to subtracting them from a binary number consisting of all ones: 1111

Signed Integers with Two's Complement

bits decimal
0 0 0 0 = 0
-8 4 2 1
10000 ← Power of two: 2 4 (16)
- 0 0 0 0 ← Number to negate (0)
1 0 0 0 0 ← Two's complement (0)

To negate a number using two's complement, we flip all the bits and add one. This is equivalent to subtracting it from a power of two: 2 4 , which is: 1 0000