Binary Fractions

You might have looked at the interactive binary page and wondered what happens if we want to store numbers that aren't positive integers? After all, you've seen decimals and negative numbers on your computer. This page will show you how we can represent decimals/fractions and negative decimal numbers using binary. If you want to take this idea further, you can also look at normalised floating-point binary.

On the binary page, we said that in a number system based on tens, each column heading (units, tens, hundreds, etc.) is ten times the value of the column heading to its right, and in a number system based on twos, each column heading is two times the one to its right. This relationship continues to the right of the decimal point, so in denary the column headings are 1/10, 1/100, 1/1000, etc., and in binary they are 1/2, 1/4, 1/8, etc.

Negative numbers are most often represented using something called two's complement (although there are other methods), with the left-most bit indicating whether the number is positive (0) or negative (1). To create a one's complement binary number, simply take a binary number and swap the 0s and 1s - e.g. 01100101 becomes 10011010. If we then add 1 to the one's complement, we get the two's complement - e.g. 01100101 becomes 10011011.

Both these techniques are used below - click on the bits to create a positive or negative binary fraction and see its value:

= 0

Can you work out how to represent 0.25? Or 0.75? What about -0.5? You will see that some numbers - e.g. 0.6 - can't be represented as an eight-bit binary number. This is the main problem that we encounter when trying to store floating point numbers on a computer. For an example of how this can affect us in practice, have a look at my first version of the standard-form page (and follow the instructions at the top).

At first sight, negative fractions in binary can look a bit confusing. Remember, though, that shifting binary numbers one place to the left (or right) multiplies (or divides) their value by two. For example, the binary representation of three (using three bits) is 011. The one's complement of this is 100, and the two's complement is 101, so minus three is 101. Shifting that two places to the right to give 1.01 is the same as dividing by four (diving by two and then by two again), and -3/4 is -0.75.

For a more in-depth discussion of number bases, look at the Number Bases page in the Mathematics section. You can also watch an introduction to binary and examples of how computers use binary on the AdvancedICT YouTube channel.

Why not practise your binary conversion skills with the Binary Breakout game? Or practice your programming skills by creating a program that uses these techniques? Try to create a program that will convert a decimal number to binary - I can think of at least two ways to do it (one of which uses bitwise Boolean logic). There is a selection of number base programs on the Python programming examples page.