Computer Science Notes - Criterion 5

Data Representation

An introduction to binary and other number systems

© martoon /
© xkcd.com

Number systems

A number system is a way of representing numbers.

Humans mainly use the decimal (base 10) system for counting, probably because we have 10 digits (fingers).

Computers use the binary (base 2) system because it is easy to produce electronic components that are in one of two states (on / off) rather than ten states.

In computing, we also use the hexadecimal (base 16) and octal (base 8) systems, as compact ways of representing binary numbers.

Counting System / Base / Digits
decimal / 10 / 0 1 2 3 4 5 6 7 8 9
binary / 2 / 0 1
octal / 8 / 0 1 2 3 4 5 6 7
hexadecimal / 16 / 0 1 2 3 4 5 6 7 8 9 A B C D E F

Notice that hexadecimal needs six extra symbols, so typically we use the letters A, B, C, D, E, F to give 16 discrete digits.

When we’re switching between number systems, we need to be careful to indicate what number system we’re using. One approach is to use a subscript, for example:

  • 1210the 10 indicates the number given uses base 10 (decimal)
  • 011002the 2 indicates the number given uses base 2 (binary)

Decimal, binary, octal, and hexadecimal comparison table

This table shows how the decimal numbers 0 to 16 are represented in binary, octal, and hexadecimal.

Decimal / Binary / Octal / Hexadecimal
0 / 00000 / 0 / 0
1 / 00001 / 1 / 1
2 / 00010 / 2 / 2
3 / 00011 / 3 / 3
4 / 00100 / 4 / 4
5 / 00101 / 5 / 5
6 / 00110 / 6 / 6
7 / 00111 / 7 / 7
8 / 01000 / 10 / 8
9 / 01001 / 11 / 9
10 / 01010 / 12 / A
11 / 01011 / 13 / B
12 / 01100 / 14 / C
13 / 01101 / 15 / D
14 / 01110 / 16 / E
15 / 01111 / 17 / F
16 / 10000 / 20 / 10

Binary

The binary number system (base 2) uses two values, ‘0’ and ‘1’.

A binary digit (either ‘0’ or ‘1’) is also called a bit, which is an abbreviation of binary digit.

Each column represents a “power of two”. For example:

Binary / 128 / 64 / 32 / 16 / 8 / 4 / 2 / 1 / Decimal
0001 0001 / 0 / 0 / 0 / 1 / 0 / 0 / 0 / 1 / 16 + 1 = 17
0101 0010 / 0 / 1 / 0 / 1 / 0 / 0 / 1 / 0 / 64 + 16 + 2 = 82
1000 0100 / 1 / 0 / 0 / 0 / 0 / 1 / 0 / 0 / 128 + 4 = 132
1100 1001 / 1 / 1 / 0 / 0 / 1 / 0 / 0 / 1 / 128 + 64 + 8 + 1 = 201

Converting from binary to decimal

The easiest way to convert from binary to decimal, particularly with small numbers, is to refer to this table:

29 / 28 / 27 / 26 / 25 / 24 / 23 / 22 / 21 / 20 / 2-1 / 2-2 / 2-3 / 2-4 / 2-5 / 2-6
512 / 256 / 128 / 64 / 32 / 16 / 8 / 4 / 2 / 1 / .5 / .25 / .125 / .0625 / .03125 / .015625

E.g.1012 has the value 1  4 + 0  1 + 1  1 = 4 + 0 + 1 = 5

11002 has the value 1  8 + 1  4 + 0  2 + 0  1 = 8 + 4 = 12

0.1012 has the value 1  .5 + 0  . 25 + 1  .125 = .5 + .125 = 0.625

Binary / 512 / 256 / 128 / 64 / 32 / 16 / 8 / 4 / 2 / 1 / Decimal
1011 / 0 / 0 / 0 / 0 / 0 / 0 / 1 / 0 / 1 / 1 / 8+2+1 = 11
1100
10101
11001
010001

Converting from decimal to binary

The easiest way to convert from decimal to binary is to refer to the table above.

You look at the decimal number you have been given and, referring to the table, compare it to each value from left to right.

Convert decimal 135 to binary.

Does 512 go into 135 no0

Does 256 go into 135 no0

Does 128 go into 135yes1how many are left over? 135-128=7

Does 64 go into 7  no0

Does 32 go into 7 no0

Does 16 go into 7  no0

Does 8 go into 7  no0

Does 4 go into 7  yes1how many are left over? 7-4=3

Does 2 go into 3  yes1how many are left over? 3-2=1

Does 1 go into 1  yes1how many are left over? 1-1=0

Therefore decimal 135 in binary is 0010 0111 or 100111.

In practice, it’s generally easiest just to write out the column values needed (512, 256, 128, etc.), and write a 1 under each value that is needed.

Decimal / 512 / 256 / 128 / 64 / 32 / 16 / 8 / 4 / 2 / 1 / Binary
11 / 0 / 0 / 0 / 0 / 0 / 0 / 1 / 0 / 1 / 1 / 1011
6
18
27
53

Converting the fractional parts from decimal to binary

The technique is best demonstrated through a series of examples…

eg 1.2510 = ?2
0.25  2 = 0 + 0.5first digit = 0
0.5  2 = 1 + 0second digit = 1
Giving .2510 = 0.012 / Notes
Only the fractional part is multiplied by 2.
The process continues until enough digits have been obtained or the fractional part becomes zero.
eg 2Convert 0.110 to binary
0.1  2 = 0 + 0.2first digit = 0
0.2  2 = 0 + 0.4second digit = 0
0.4  2 = 0 + 0.8third digit = 0
0.8  2 = 1 + 0.6fourth digit = 1
0.6  2 = 1 + 0.2fifth digit = 1
0.2  2 = 0 + 0.4sixth digit = 0
……
Writing as a recurring binary number:
0.110 = 0.0001100110011..2
Rounding of to, say, 6 binary points:
0.110 = 0.0001102 / Notes
An exact decimal fraction may not have an exact binary equivalent. In these cases, go to one more place than asked for, then round off.
The decimal number 0.1 cannot be represented exactly in binary. It must be rounded off to a finite number of decimal places, giving rise to round-off error.
eg 3 Convert 5.12510 to binary
5 becomes 101 in binary
.125  2 = 0 + .25 first digit = 0
.25  2 = 0 + .5 second digit = 0
.5  2 = 1 + 0 third digit = 1
Thus 5.12510 = 101.0012

Hexadecimal

Hexadecimal (base 16) uses the digits from 0-9 and letters A-F

Decimal / 0 / 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9 / 10 / 11 / 12 / 13 / 14 / 15
Hexadecimal / 0 / 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9 / A / B / C / D / E / F

Converting from hexadecimal to decimal

To convert between hexadecimal to decimal, you can use a similar approach to binary, using column values, but rather than simply adding the items together, there is an additional multiplication. We will stick with two digit hexadecimal conversion.

Hexadecimal / 16’s / 1’s / Working / Binary
06 / 0 x16 / 6 / (0 x16) + 6 = 0+6 / 6
1A / 1 x16 / A / (1 x16) + A = 16+10 / 26
0E
32
B5

Converting from decimal to hexadecimal

To convert from decimal to hexadecimal, we can divide the number by 16, then the whole number is the number of 16’s in the number. If you subtract the 16’s from the original problem, the number of whole numbers remaining is the 1’s in the number.

Decimal / 16’s / 1’s / Working / Hexadecimal
18 / 1 / 2 / 18 / 16= 1 (with 2 left over) / 12
44 / 2 / C / 42 / 16 = 2 (with 12 left over – 12 is C) / 2C
7
25
13

Converting from hexadecimal to binary

Working similarly to the decimal conversion, we can then use the 16’s and 32’s columns for the number of 16’s in a number, and convert the remainder in normal binary

Hexadecimal / 16’s / 1’s / Working / Binary
06 / 0 x16 / 6 / 0000 0000 + 0000 0110 / 0000 0110
1A / 1 x16 / A (10) / 0001 0000 + 0000 1010 / 0001 1010
0E
32

Converting from binary to hexadecimal

Working similarly to the decimal conversion, we can then use the 16’s and 32’s columns for the number of 16’s in a number, and convert the remainder in normal binary

Binary / 16’s / 1’s / Working / Hexadecimal
0000 0110 / 0
(0000 0000) / 6
(0000 0110) / 06
0001 1100 / 1
(0001 0000) / 12
(0000 1100) / 1C
0010 0001
0001 0111

Storing integers (ie. whole numbers) in the computer

The basic unit for storing data in the computer is the bit.

8 bits = 1 byte

(half a byte, or 4 bits, is a nibble, which can be represented by a single hexadecimal digit)

1 kilobyte = 1000 bytes = 8,000 bits
(or, in some contexts, 1 kilobyte = 1024 bytes = 8 192bits)

eg64 kB = 64 x 1000 = 64,000 memory locations.

In summary…

  • bit – A single bit of information (0 or 1)
  • nibble – A 4 bit string of bits (also the size of one hexadecimal digit)
  • byte = 8 bits (or may refer to an 8-bit word)
  • word – Refers to a string of bits. Eg. A computer might work in 8-, 16-, 32-, or 64-bit words. A 16-bit word is commonly used. Many operating systems work on 32 or 64 bit instruction size.
  • kilobyte (kB) = 1000 bytes (or sometimes 1024 bytes)
  • megabyte (MB) =1000 kB

Unsigned binary numbers

It wastes storage to represent numbers character by character, because one byte is used for each digit. Instead, numbers are converted to binary and represented in that form.

One byte (i.e. an 8-bit word) can represent numbers ranging from 0 to 255.

0000 00002 = 010 to 1111 11112 = 25510

But what if want to represent decimal numbers greater than 255 or negative numbers?

For a start, we can increase the word length. For example, lets consider a 16-bit word.

15 / 1-14 / 0

Bit 15Most significant bit

Bits 1-14Balance of the word

Bit 0Least significant bit

The largest integer in a 16-bit word is 216 - 1 (65535).

To show larger integers you need a longer word but there is always some fixed limit to integer size and the need for a larger integer would cause the system to break down.

If it is not possible to store the results of an operation the system 'overflows' the register provided, this is arithmetic overflow which causes the system to crash with an error message.

There are two main methods of storing Signed Numbers.

Sign magnitude

In sign magnitude (or “sign and magnitude”), the most significant bit is reserved as the sign bit with “0” representing + (positive) and “1” representing – (negative).

eg- 39101 010 01112

+ 39100 010 01112

With an 8-bit word, this means that instead of being able to represent integers from 0 - 255, you can represent integers from:

-12710 1111 11112

to+127100111 11112

With a 16-bit word the maximum integer range is -32767 to +32767.

e.g. with a 4-bit word and sign magnitude

1 1102= (-)(4 + 2)=-6

1 0012= (-)(1)=-1

0 1012= (+)(4 + 1)=+5

NoteThe largest number to be represented in 4 bits is 0111 or + 7

There are 2 representations for zero, 0000 and 10002 this is why some older systems gave +0 & -0.

NoteWe cannot perform arithmetic directly in sign magnitude representation because it would need a machine that has separate addition and subtraction hardware to take care of the sign bits. To overcome this 2's complement representations are used. Subtraction is done by adding the complement of the number, and treating all the bits the same without considering the sign of the value.

Two's Complement

The two’s complement of a number is the negative representation of a number.

This is particularly important for subtraction. Instead of subtracting a number, we will instead addits opposite (e.g. the negative version of a positive number).

“Taking a two’s complement” means simply getting the representation of the negative value of a stored number. The easiest way to do this:

  • Start from the right of the number and work towards the left.
  • Any ‘0’ bits remain the same until the first ‘1’ bit and the first ‘1’ bit remains the same.
  • After the first ‘1’ bit all subsequent bits are reversed.

Examples (assuming an 8 bit two’s complement representation):

Original / Binary representation / 2’s complement / New value
6 / 0000 0110 / 1111 1010 / -6
-6 / 1111 1010 / 0000 0110 / 6

Interpreting numbers in two’s complement representation

Positive integers

  • The most significant bit is “0”.
  • The value is equal to the binary string. For example, 0000 0011 represents 310.

Negative integers

  • The most significant bit is “1”.
  • To obtain the value, “take a two’s complement”. For example 11110110 represents a negative value. Taking a two’s complement gives 00001010 which represents the positive number 610. Hence 11110110 represents –610.

Complete the following table in which 8 bit two’s complement representations are matched with their decimal value.

Binary (two’s complement notation) / Decimal / Binary (two’s complement notation) / Decimal
0000 1101 / 0000 0000
1111 1000 / 1111 1111
15 / 127
-7 / -128

A summary table

4-bit word / Unsigned / Sign magnitude / Two's complement
0000 / 0 / 0 / 0
0001 / 1 / 1 / 1
0010 / 2 / 2 / 2
0011 / 3 / 3 / 3
0100 / 4 / 4 / 4
0101 / 5 / 5 / 5
0110 / 6 / 6 / 6
0111 / 7 / 7 / 7
1000 / 8 / -0 / -8
1001 / 9 / -1 / -7
1010 / 10 / -2 / -6
1011 / 11 / -3 / -5
1100 / 12 / -4 / -4
1101 / 13 / -5 / -3
1110 / 14 / -6 / -2
1111 / 15 / -7 / -1

Arithmetic in the binary system

Arithmetic in binary follows exactly the same rules as for decimal arithmetic.

Addition

Addition (Check by decimal conversion)

eg 1101 13

+ 1110+ 14

11011 27

Complete the following additions, in which all numbers are unsigned binary:

(Note: only add two numbers at a time)

(a) 1 + 1 (b) 1 + 1 + 1 (c) 1 + 1 + 1 + 1

(d) 111111 + 1 (e) 101101 + 110111 (f) 11101 + 11011 + 10101

Subtraction

Subtraction is done by taking the two’s complement of the number to be subtracted and/or a negative number.

For this to function effectively you must be using more bits than is required to represent the number (allowing for at least a sign bit, it is good practice to have at least 2 extra bits)

For example, to perform the subtraction 13 - 7:

1 / Convert both integers to binary. Remember to add an extra bit to hold the sign. (13 = 1101, then add 1 bit to get 01101.) Both numbers need to be represented with the same number of bits. / 13 = 01101
7 = 00111
2 / Convert the number to be subtracted to 2's complement form. / 00111  11001
3 / Add the resulting binary numbers using ordinary binary addition. / 01101
+ 11001
------
1 00110
4 / Truncate any leading 1 (carry) that occurs. (In this case, a 1 has carried into the extra position.) / 1 00110  00110
5 / Convert the result back to decimal form. If the leftmost position is a 0, the number is positive. If the leftmost position is a 1, the number is in 2's complement form; convert the number back by taking the 2's complement of it, then convert to decimal and take the negative. / 00110 = 6

For example, to perform the subtraction 6 - 8:

1 / Convert both integers to binary. Remember to add an extra bit to hold the sign. (6= 0110, then add 1 bit to get 00110.) Both numbers need to be represented with the same number of bits. / 6 = 00110
8 = 01000
2 / Convert the number to be subtracted to 2's complement form. / 01000  11000
3 / Add the resulting binary numbers using ordinary binary addition. / 00110
+ 11000
------
11110
4 / Truncate any leading 1 (carry) that occurs. (In this case, a 1 has carried into the extra position.) / 11110
(no carry in this case)
5 / Convert the result back to decimal form. If the leftmost position is a 0, the number is positive. If the leftmost position is a 1, the number is in 2's complement form; convert the number back by taking the 2's complement of it, then convert to decimal and take the negative. / 11110  00010
(two’s complement)
00010 = -2

Overflow in two’s complement

If the expected answer for a calculation is outside the two’s complement range the calculation results in an overflow error which will affect the most significant bit of the answer: it the expected answer is positive the two’s complement representation with an overflow error will indicate a negative value and vice versa. The following examples will illustrate this.

3 0011
+ 7 0111
10 1010 = -6 in two’s complement
In this example, the expected answer of 10 is outside the 4 bit two’s complement range of –8 to +7 so the answer (in two’s complement interpretation) is incorrect. / - 2 1110
- 7+1001
-9 (1) 0111 = +7 in two’s complement
(ignore carry)
In this example, the expected answer of –9 is outside the 4 bit two’s complement range of –8 to +7 so the answer is again incorrect.

Remember: Overflow and Carry are two entirely different concepts. Carry is ignored in two’s complement arithmetic. Overflow indicates a genuine error.

Complete the following subtractions, using 6 bit binary show all working & indicate where any errors occur:

(a) 3 - 1 (b) 7 - 2(c) 10 - 12

(d) 15 - 12 (e) 13 - 15 (f) 15 – 9

Floating point numbers

A floating point number may be represented by 3 parts in a single computer word.

m x be where

  • m is the mantissa and 0 <= mantissa < 1
  • b is the base
  • e is the exponent (the power of the base as a positive or negative integer)

eg67.79 x 10-3

Scientific notation uses one significant digit before the decimal point, multiplied by the base (10) to the appropriate power

eg6.779 x 10-2

Normalised notation has no significant digits before the decimal point, multiplied by the base (10) to the appropriate power. Floating point numbers are stored in normalised form as this is the most efficient way.

eg0.6779 x 10-1

Examples of normalisation (decimal and binary)

0.00156 x 10-4=.156 x 10-6

1732.1=.17321 x 104

794 x 10-5=.794 x 10-5+3

=.794 x 10-2

1101 x 2100{4}=.1101 x 2100+100 {4+4}

=.1101 x 21000{8}

1101.001 x 2-111 {-7}=.1101001 x 2-111+100 {-7+4}

=.1101001 x 2-011{-3}

Helpful hints

  • If you increase the size of the mantissa (ie move the decimal point to the right) then decrease the size of the exponent by the same number of places (ie subtract the powers).
  • If you decrease the size of the mantissa (ie move the decimal point to the left) then increase the size of the exponent by the same number of places (ie add to the powers).

Representation of floating point numbers in computers

Floating point numbers require the computer to store the mantissa, exponent and sign. There needs to be a balance between the precision (basically determined by the number of bits in the mantissa), the range (basically determined by the number of bits in the exponent) and the total memory required to store the number.

In Java for example the types of real numbers that can be stored are:

Type / Size / Largest Value / Smallest Value / Precision
float / 32 bits /  3.4  1038 /  1.4  10-45 / 6-7 sig figs
double / 64 bits /  1.8  10308 /  4.9  10-324 / 14-15 sig figs

Some examples of floating point representation with 16 bit words

There are many possible ways of storing real numbers, and here are examples of just a few that you might come across in books and in examination questions.

Example 1 16-bit word with 5-bit 2’s complement exponent and normalised mantissa

bit 15 / bits 14 - 10 / bits 9 - 0
sign
0 (+) / exponent in 2's complement / normalised mantissa
1 (-)

The largest number that can be represented: 0 01111 1111111111  0.1111111111  21111 {15}

The smallest positive number that can be represented: 0 10000 1000000000  0.1  2-10000 {-16}

eg10 00001 1000000000

sign= plus

+ 1 .1= + .1 x 21 mantissa= 0.1

= + 0.5 x 21 = 1exponent= + 1

eg21 00000 1101000000

sign= minus

- 0 .1101= - .1101 x 20mantissa= 0.1101

= - 0.8125 x 20exponent= 0

= - 0.8125

eg31 11110 1010000000

sign= minus

- - 010 .101= - .101 x 2-010mantissa= 0.101

= - 0.625 x 2-2exponent= - 2

= - 0.15625

Example 2 16-bit word with 9-bit 2’s complement exponent and normalised mantissa

15 / bits 14 - 6 / bits 5 - 0
sign
0 (+) / exponent in 2's / Normalised mantissa
1 (-) / complement to base 2

Largest number that can be represented: 0 011111111 111111  0.111111  211111111 {511}

Smallest positive number that can be represented: 0 100000000 100000  0.1  2-100000000 {-512}

This representation has a much greater range but a smaller precision than the previous representation.

Eg1 000000011 110000Sign= minus

Limitations of floating point numbers

Floating point numbers are very often an inexact representation.

Many decimal numbers such as 0.1 and 0.2 are recurring binary fractions and cannot be represented exactly.

Also when carrying out calculations the size of the mantissa changes.

eg4 digit mantissa

.6152  103

- .6151  103

.0001 103= .1 x 100

.6152 x 105

x .6151 x 105

.3785 x 105should be .378471 so there is a loss of precision

Java uses float to represent floating point numbers, and double to represent numbers of double precision, which uses twice the number of significant figures and also uses more memory.

Floating point representation

In the following problems, use the standard 16 bit representation which has 1 exponent sign digit, 1 mantissa sign digit, 6 exponent digits and 8 mantissa digits (normalised).

What is the computer representation of the following binary numbers using the above system?

(a) 0.011  2100(b) –0.0011

(c) 111000 (d) 10.11  2-111

Calculation of file sizes

Images

An image has a width in pixels and a height in pixels. For example:
640 x 480= 307,200 pixels

For each pixel in the picture we need to specify a particular colour from a range of possible colours. We can give either:

  • The colour depth
    e.g. 24-bit colour
  • The number of colours
    e.g. 16 million colours (224=16,777,216)

If the image is plain black and white (no grey) then it uses 1 bit to represent each pixel

  • How many bits are needed to represent 16 colours?
  • How many bits are needed to represent 256 colours?

To calculate the file size we multiply the number of pixels by the number of bits to represent each pixel

640 x 480= 307,200 pixels

Using 16 colours = 4 bits

307,200 x 4 = 1,228,800 bits

We need to find out what 1,228,800 bits is in kilobytes

There are 8 bits in a byte:
1,228,800 bits / 8 = 153,600 bytes
There are 1000 bytes in a kilobyte
153,600 bytes / 1000 = 153.6 kB
If we needed to calculate megabytes, we would divide by 1000 again.

Colours to bits

  • How many bits are needed to represent 16 colours?
  • How many bits are needed to represent 256 colours?

In MB what is the file size of:

  1. An image 640 x 480 in 24 bit colour?
  2. An image 1280 x 720 using 1024 colours?

Sound

To calculate the size of a sound file we multiply the:
Time length of the audio clip. eg. 20 second
The rate at which it was sampled. eg. 4 kHz (remember that kilo means 1000)
The audio quality. eg. 8 bit audio

20 seconds x 4kHz x 1000 x 8 bit = 640,000 bits
640,000 bits /8 = 80,000 bytes
80,000 bytes /1000 = 80 kB

In MB what is the file size of:

  1. A 60 second audio clip, sampled at 44KHz, using 16 bit audio?
  2. A 1.5 minute audio clip, sampled at 96KHz, using 24 bit audio?

Solutions

Binary conversions

Number / 512 / 256 / 128 / 64 / 32 / 16 / 8 / 4 / 2 / 1 / Result
1011 / 0 / 0 / 0 / 0 / 0 / 0 / 1 / 0 / 1 / 1 / 8+2+1 = 11
1100 / 1 / 1 / 0 / 0 / 8+4= 12
10101 / 1 / 0 / 1 / 0 / 1 / 16+4+1=21
11001 / 1 / 1 / 0 / 0 / 1 / 16+8+1=25
010001 / 0 / 1 / 0 / 0 / 0 / 1 / 16+1=17
Number / 512 / 256 / 128 / 64 / 32 / 16 / 8 / 4 / 2 / 1 / Result
11 / 0 / 0 / 0 / 0 / 0 / 0 / 1 / 0 / 1 / 1 / 1011
6 / 1 / 1 / 0 / 110
18 / 1 / 0 / 0 / 1 / 0 / 10010
27 / 1 / 1 / 0 / 1 / 1 / 11011
53 / 1 / 1 / 0 / 1 / 0 / 1 / 110101

Hexadecimal conversions

Hexadecimal / 16’s / 1’s / Working / Decimal
06 / 0 x16 / 6 / (0 x16) + 6 = 0+6 / 6
1A / 1 x16 / A / (1 x16) + A = 16+10 / 26
0E / 0 x16 / E / (0 x16) + E = 0+14 / 14
32 / 3 x16 / 2 / (3 x16) + 2= 48+2 / 50
B5 / B x16 / 5 / (B x16) + 5 = (11 x16) + 5 = 176+5 / 181
Decimal / 16’s / 1’s / Working / Hexadecimal
18 / 1 / 2 / 18 / 16= 1 (with 2 left over) / 12
44 / 2 / C / 42 / 16 = 2 (with 12 left over – 12 is C) / 2C
7 / 0 / 7 / 07
25 / 1 / 9 / 25/16 = 1 (with 9 left over) / 19
13 / 0 / D / 0D
Hexadecimal / 16’s / 1’s / Working / Binary
06 / 0 x16 / 6 / 0000 0000 + 0000 0110 / 0000 0110
1A / 1 x16 / A (10) / 0001 0000 + 0000 1010 / 0001 1010
0E / 0 x16 / E (14) / 0000 0000 + 0000 1110 / 0000 1110
32 / 3 x16 / 2 / 0011 0000 + 0000 0010 / 0011 0010
Binary / 16’s / 1’s / Hexadecimal
0000 0110 / 0  (0000 0000) / 6 (0000 0110) / 06
0001 1100 / 1 (0001 0000) / 12  (0000 1100) / 1C
0010 0001 / 2 (0010 0000) / 1  (0000 0001) / 21
0001 0111 / 1 (0001 0000) / 7  (0000 0111) / 17

Two’s Complement

Complete the following table in which 8-bit two’s complement representations are matched with their decimal value.

Binary (two’s complement notation) / Decimal / Binary (two’s complement notation) / Decimal
0000 1101 / 13 / 0000 0000 / 0
1111 1000 / -8 / 1111 1111 / -1
0000 1111 / 15 / 0111 1111 / 127
1111 1001 / -7 / 1000 0000 / -128

Addition

Complete the following additions, in which all numbers are unsigned binary:

(a) 1 + 1 (b) 1 + 1 + 1 (c) 1 + 1 + 1 + 1

1011100

(d) 11 1111 + 1 (e) 101101 + 110111 (f) 11101 + 11011 + 10101

100 0000110 0100101 101

Subtraction

Complete the following subtractions:

(a) 3 - 1 (b) 7 - 2(c) 10 - 12

3 = 000 0117=000 11110=001 010

1 = 000 0012= 000 01012=001 100

-1 = 111 111-2=111 110-12=110100

000 011 000 111001 010

111 111 111 110110 100

------

1 000 0101 000 101111 110

(truncate the carry)(truncate the carry)(take two’s complement)

000 010 (2)000 101 (5)000 010 (-2)

(d) 15 - 12 (e) 13 - 15 (f) 15 – 9

15=001 11113=001 10115=001 111

12=001 10015=001 1119= 001 001

-12=110100-15=110 001-9=110 111

001 111001 101 001 111

110 100110 001 110 111

------

1 000 011111 1101 000 110

(truncate the carry)(take two’s complement) (truncate the carry)

000 011 (3)000 010 (-2)000 110 (3)

Floating point representation

What is the computer representation of the following binary numbers?

(a) 0.011  2100(b) –0.0011

0 0 000 011 1100 00001 1 000 010 1100 0000

(c) 111000 (d) 10.11  2-111

0 0 000 110 1110 00001 0 000 101 1011 0000

Floating point rounding off

When using an 8-bit two’s complement representation, which of the following decimal numbers can be represented precisely (that is, without any rounding off).

(a) 128.5(b) 0.1(c) 0.03125

1000 0000.10.0001 1001…0.00001
(requires 9 bits)(recurring number)(precise solution)

Calculating File Sizes
How many bits are needed to represent 16 colours?24 = 16  4 bits
How many bits are needed to represent 256 colours?28 = 256  8 bits
In MB what is the file size of:

An image 640 x 480 in 24 bit colour?An image 1280 x 720 using 1024 colours?
640 x 480 x 24 = 7,372,800 bits1280 x 720 x 10 = 9,216,000 bits
7,372,800 bits /8 = 921,600 bytes9,216,000 bits /8 = 1, 152, 000 bytes
921,600 bytes /1000 = 921.6 kB1,152,000 bytes /1000 = 1152 KB
921.6kB /1000 = 0.92MB1152 KB /1000 = 1.15MB

In MB what is the file size of:

A 60 second audio clip, sampled at 44KHz, using 16 bit audio?
60 x 44000 x 16 = 42, 240,000 bits
42,240,000 bits /8 = 5, 280,000 bytes
5,280,000 bytes /1000 = 5,280kB
5,280kB /1000 = 5.28 MB

A 1.5 minute audio clip, sampled at 96KHz, using 24 bit audio?
90 x 96000 x 24 = 207,360,000 bits
207,360,000 bits /8 = 25,920,000 bytes
25,920,000 bytes /1000 = 25,920kB
25,920 KB /1000 = 25.9 MB