Jump to content

Binary number system

From Wikibooks, open books for an open world

PAPER 2 - ⇑ Fundamentals of data representation ⇑

← Number bases Binary number system Binary number system →


Specification

[edit | edit source]
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

[edit | edit source]

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

[edit | edit source]
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 = , referring to the number of bits.

Examples

For example, for 8 bits:

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: , referring to the number of bits.

Examples

For example, for 8 bits:

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 nibble, you would do the following:

These would be the 4 combinations:

  1. 00
  2. 01
  3. 10
  4. 11

Unsigned binary arithmetic

[edit | edit source]

Adding

[edit | edit source]

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

[edit | edit source]

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

[edit | edit source]
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:

1 0 0 1 1 1 0 0
Step 1

cake day friday join us!!!

MSB LSB
1 0 0 1 1 1 0 0

Binary Number System

[edit | edit source]