Positional Notation
A numerical system is a way of writing numbers. Here we’re interested in three particular numerical systems: decimal (the one we’re used to, with 10 digits), binary (with 2 digits), and hexadecimal (with 16 digits).
The important thing about these number systems is that they use positional notation, which is a way of writing numbers that makes writing large numbers easy.
Watch the video and then answer the questions below.
Eight-minute video
You can also view this video on YouTube
Key Points
- Binary, Decimal, and Hexadecimal are all numerical systems - ways of writing numbers.
- In decimal (base 10), each digit is either a unit, 10s, 100s, 1000s, etc. (powers of 10)
- In binary (base 2), each digit is either a unit, 2s, 4s, 8s, 16s, etc. (powers of 2)
- These are ordered largest to smallest. Units comes last.
Converting from Binary to Decimal
To calculate the decimal value of a binary number, work out the multiplier for each column. These are increasing powers of 2, with the units column being 20 = 1
. Multiply the binary digit by the multiplier for that column and sum the results together.
bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
---|---|---|---|---|---|---|---|---|
power | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 |
decimal | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
So, for example, the number 01001101
has a 1
in the 64
s column, a 1
in the 8
s column, a 1
in the 4
s and a 1
in the units, making a total of 77
.
01001101
= 1 * 64 + 1 * 8 + 1 * 4 + 1 * 1
= 77
Bits needed to store a value
To work out how many binary bits you need to store a value n
, you find log2 n
, and round up.
For example, to store 466
different values, you would need at least log2 466 = 8.86
bits, which rounded up to whole bits is 9
.
To work out the log, base 2, of a number n
in Python, use the log(n, 2)
function from the math
package. For example:
import math
math.log(466, 2) # returns 8.86418614465428
Number Systems in Java
In Java, we can write numbers in binary and hexadecimal using a prefix:
0b
is binary, e.g. 0b110x
is hexadecimal, e.g. 0xFF
To convert a binary string to decimal, you can use the Integer.parseInt()
function, passing 2 as the base:
Integer.parseInt("01", 2) # outputs 1
To convert a number to its binary or hexadecimal representaiton, use the built-in Integer.toBinaryString()
and Integer.toHexString()
functions.
Have a play with the demo below:
Number Systems in Python
In Python, we can write numbers in binary and hexadecimal using a prefix:
0b
is binary, e.g. 0b110x
is hexadecimal, e.g. 0xFF
To convert a binary string to decimal, you can use the int
function, passing 2 as the base:
int("01", 2) # outputs 1
To convert a number to its binary or hexadecimal representaiton, use the built-in bin
and hex
functions.
Have a play with the demo below:
Questions
1. Check your understanding
Answer each question then click the button marked “Check Answers” below. Use Python to help you.
1. Conversions
2. Bits and peices
3. Colours on the Web
Colours on the web are stored as the combintion of three values: red, green, and blue. Each value is called a channel. They are written using a hexadecimal string, for example #FF0000
is bright red.
In computer graphics colours often include a fourth ‘alpha’ channel, representing the colour’s opacity. 00
is transparent, FF
is opaque.
4. Octal
Octal is a number system that uses base 8: it uses only the symbols 0 1 2 3 4 5 6 7
. It uses positional notation. In Python, you use the prefix 0o
to write a number in octal e.g. 0o77
. You can use the oct()
function to convert a number to its representation in octal. In Java, you use the prefix 0
to write a numnber in octal, and the Integer.toOctalString()
converts a number to an octal string.
5. ASCII
ASCII is a text encoding that uses 7 bits (or 8 bits with various extended ASCII standards) to store a character. It is not a number system. Instead, you convert one character (group of 7 or 8 bits) at a time.
Convert each of the folowing numbers into the corresponsing ASCII symbol. 8 bits are used for each. This reference table may help you:
2. On paper
Find the decimal value of 0xA4E001, showing your working. Calculate the numerical value for each digit separately, and then sum them together. Check your answer using Python.
Summary
In this section we have learned about different number systems. We know how to interpret a number written in binary (base 2), hexadecimal (base 16), or any other base, using positional notation. We have also seen octal, which is base 8, and ASCII, which which is a text encoding.
- You should be able to convert a number from any given base to decimal.
- You should be able to work out the number of different values that can be stored using a given number of digits for a given base.
In the next section we will find out how to convert between these different bases.
- Previous
- Next