Representing integers in other bases

Numerals represent numbers. Numerals are strings of digits.

Decimal notation (base = 10)

In the decimal system there are 10 digits:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

We understand that a numeral like 234 represents the number:

2 * 102 + 3 * 101 + 4 * 100

Binary representation (base = 2)

Inside a computer there are only two digits:

0, 1

We call these bits.

When we run out of bits we can use two bits:

10 = 2
11 = 3

Then three bits:

100 = 4
101 = 5
110 = 6
111 = 7

A byte consists of 8 bits. There are 28 = 256 bytes. That means we can represent the numbers 0 to 257.

If we want to represent negative and positive numbers, then we can take the first bit to represent the sign: 0 = +, 1 = -. In this case we can represent the numbers from -128 through +127.

How many signed numbers can we represent with 16 bits? 32 bits? 64 bits?

Octal representation (base = 8)

We can represent numbers as base 8 numerals, too.  In Java octal numerals begin with 0.

For example:

052 = 5 * 81 + 2 * 80 = 42

Hexadecimal representation (base = 16)

We can also represent numbers as base 16 numerals.  The hexadecimal digits are:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

plus:

a = 10
b = 11
c = 12
d = 13
e = 14
f = 15

In Java hexadecimal numerals begin with 0x.

The number 16 would be represented as the hexadecimal number 0x10. The number 26 would be 0x1a.

For example:

0x2a = 2 * 161 + 10 * 160 = 32 + 10 = 42

 

In Java octal numerals begin with 0. For example: