# Binary number system

 ← Number bases Binary number system Binary number system →

## Specification

 Specification coverage 3.5.4 - Binary number system 3.5.4.1 - Unsigned binary 3.5.4.2 - Unsigned binary arithmetic 3.5.4.3 - Signed binary using two’s complement 3.5.4.4 - Numbers with a fractional part 3.5.4.5 - Rounding errors (A level only) 3.5.4.6 - Absolute and relative errors (A level only) 3.5.4.7 - Range and precision (A level only) 3.5.4.8 - Normalisation of floating point form (A level only) 3.5.4.9 - Underflow and overflow (A level only)

## Introduction

In the previous chapter we looked at the common number systems and bases. We use different number bases, as humans tend to work with decimals and computers can only process data in binary. As Computer Science students, we need to know how binary works and how the computer carries out calculations in binary.

## Unsigned binary

Unsigned binary - binary that represents positive numbers.
Signed binary - binary with a positive and negative sign.

There is a quick way of finding out the maximum values for a given number of bits. Using the formula below, you can find out what is the maximum decimal value you can have while using 8 bit binary.

Maximum decimal value = ${\displaystyle 2^{n}-1}$ , ${\displaystyle n}$  referring to the number of bits.

 Examples For example, for 8 bits: ${\displaystyle 2^{n}-1=2^{8}-1=256-1=255}$  And of course 0 would be the minimum decimal value in unsigned binary.

There is also a way to find out the maximum number of combinations, you simply use the following formula: ${\displaystyle 2^{n}}$ , ${\displaystyle n}$  referring to the number of bits.

 Examples For example, for 8 bits: ${\displaystyle 2^{8}=256}$  Meaning that there are 256 possible combinations if you are using 8 bit binary. If you were using 2 bit binary, also known as a ${\displaystyle {\tfrac {1}{2}}}$  nibble, you would do the following: ${\displaystyle 2^{2}=4}$  These would be the 4 combinations: `00` `01` `10` `11`

## Unsigned binary arithmetic

To add two numbers together in binary, first line up the numbers in the same way as you would do column addition in decimal:

 Examples ``` 1 1 (carried digits) 0 0 1 1 0 0 1 0 + 1 0 1 1 0 1 0 1 ------------------- = 1 1 1 0 0 1 1 1 ``` Now add the columns starting from the right-hand side, remembering that you can only use 0s and 1s: 0 + 0 will equal 0 so put 0 on the answer line 0 + 1 or 1 + 0 will both equal 1 so put 1 in the answer line 1 + 1 will equal 10 (one, zero) so put 0 in the answer line and carry the 1 1 + 1 + 1 will equal 11 (one, one) so put 1 in the answer line and carry the 1.You can check your answer by converting all the numbers to decimal, carrying out the addition and then converting the answer back to binary. In this case the answer, the first number is 50, the second number is 181, so the answer should be 231.

### Multiplying unsigned binary integers

To multiply in binary, you multiply the first number by each of the digits of the second number in turn starting from the right-hand side (in the same way that you would do multiplication in decimal).This means you are either multiplying each digit by 0 or 1, which will give you either a 0 or 1 as the answer. You then do the same for the next digit, shifting your answers to the left as you would in decimal multiplication.

You then carry out a binary addition to find the final answer. For example, to multiply 11011 by 11:

 Examples ``` 1 1 0 1 1 x 1 1 ------------------- = 1 1 0 1 1 + 1 1 0 1 1 0 ------------------- = 1 0 1 0 0 0 1 1 1 1 1 (carried digits) ``` Note the zero on the LSB (least significant bit) as the numbers have been shifted to the left. Again you can work this out by converting the binary to decimal to check your answer. In this case the first number is 27 (twenty-seven), the second number is 3 (three), so the answer is 81 (eighty-one). Rules of binary multiplication are the same as the ones for decimals: 0 x 0 = 0 0 x 1 = 0 1 x 0 = 0 1 x 1 = 1, and no carry or borrow bits

## Signed binary using two’s complement

Two's complement - a method of working with signed binary values.

Two's complement is a method used to represent signed integers in binary form. This means that it can be used to represent positive and negative integers. This section shows how two's complement can represent negative integers.

Assume we want to convert the binary code 100111002 into decimal using two's complement:

Step 1

Write out the decimal (or denary, if you prefer) equivalent as show:

${\displaystyle -128}$  ${\displaystyle 64}$  ${\displaystyle 32}$  ${\displaystyle 16}$  ${\displaystyle 8}$  ${\displaystyle 4}$  ${\displaystyle 2}$  ${\displaystyle 1}$
1 0 0 1 1 1 0 0
 Step 1
MSB LSB
${\displaystyle -128}$  ${\displaystyle 64}$  ${\displaystyle 32}$  ${\displaystyle 16}$  ${\displaystyle 8}$  ${\displaystyle 4}$  ${\displaystyle 2}$  ${\displaystyle 1}$
1 0 0 1 1 1 0 0