Integers

Binary is used to represent whole numbers so that they can be understood by the processor.

An integer:

  • is a whole number
  • can be a positive number or a negative number

The number 173 is an integer. It can be represented in binary as:

10101101

Binary works using a number system that is based on powers of 2. To understand why 10101101 is the same as 173 look at the following table:

1286432168421
(27)(26)(25)(24)(23)(22)(21)(20)
10101101
ONOFFONOFFONONOFFON

When adding up all of the values that are on, the following sum is created:

128 + 32 + 8 + 4 + 1 = 173

In this example we are using 8 bits to represent a number. If we use 8 bits we can represent any number between 0 and 255.

If all of the values are off, the number is 0:

1286432168421
(27)(26)(25)(24)(23)(22)(21)(20)
00000000
OFFOFFOFFOFFOFFOFFOFFOFF

If all of the values are on, the number is 255:

1286432168421
(27)(26)(25)(24)(23)(22)(21)(20)
11111111
ONONONONONONONON

128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255

Range of binary numbers

To figure out the range of numbers that can be stored with a set number of bits, use the following formula:

2n - 1

The reason for taking one away is because the integer 0 needs to be stored.

28 = 256

28 - 1 = 255

This means that the range of integers that can be represented using 8 bits is 0 – 255. Typically we categorise binary in groups of 8 bits (or 1 byte).

Number of bitsFormulaRange
828 - 10 - 255
16216 - 10 - 65,535
24224 - 10 - 16,777,215
32232 - 10 - 4, 294,967,295