Numerical Bases

This cheatsheet looks at representing numerical bases.

Different bases

The use of 10 as a base is fairly arbitary. We can use any base system for our numbers that makes sense. Some everyday examples of this you are used to seeing is minutes, which is base 60, and hours which is base 12/24. In computing, common bases are 2, 8, 10 and 16.

Prefixes for Integers

Integer literals are often written with a prefix to show the base:

Prefix Meaning
0b binary
0o octal
(none) base 10
0x Hexadecimal
0 octal (old)

Some languages use ‘0’ as a prefix for octal, this can get confusing so some have adopted 0o to mean octal. Both are still in use.

Examples

Here are some examples of each of these conventions:

Hexadecimal Decimal Octal Binary
0x0 0 0o0 0b0
0x1 1 0o1 0b1
.. .. .. ..
0x9 9 0o11 0b1001
0xA 10 0o12 0b1010

Binary Notation

There are two commonly used ways of representing large binary values. One is based on SI-prefixes, the other uses a power of two.

A binary integer is formed of bits, a bit can either be 0 or 1 (in other words, its base 2). An n-bit (unsigned) binary number can store a value between 0 and 2n-1.

Power 27 26 25 24 23 22 21 20
Value
Binary 0 0 0 0 0 0 0 0
Decimal 0 0 0 0 0 0 0 0
The number 0 in binary

Signed Values

Signed binary numbers can be stored in many possible ways:

  • Signed bit (the highest bit is 0 for positive, and 1 for negative)
  • Twos compliment (the highest bit is regarded as negative)

Representing large values

Decimal

SI-prefix based.

name symbol size
kilobyte kB 1000
megabyte MB 10002
gigabyte GB 10003
terabyte TB 10004
petabyte PB 10005
exabyte EB 10006
zettabyte ZB 10007
yottabyte YB 10008

Note some operating systems (Windows) use kilo to mean 1024 bytes, due to historical tradition. This is not the SI defintion of kilo.

Binary

IEC convention.

name symbol size
kibibyte KiB 1024
mebibyte MiB 10242
gibibyte GiB 10243
tebibyte TiB 10244
pebibyte PiB 10245
exbibyte EiB 10246
zebibyte ZiB 10247
yobibyte YiB 10248

Last updated 0001-01-01

Graduation Cap Book Open book GitHub Info chevron-right Sticky Note chevron-left Puzzle Piece Square Lightbulb Video Exclamation Triangle Globe