Computer Representation of Numbers



Notes by Liz Ritchard at Rosny College (with some adaptations by Hobart College and Rosny College)

Computer Representation of Numbers

A numbering system is a way of representing numbers. The decimal (base 10) system for counting was used because we have 10 digits (fingers). Computers use the binary (base 2) system because it is easy to produce electronic components which are in one of 2 states (on / off) rather than 10 states. This means that anyone working with computers must be able to work with and understand binary and the related hexadecimal (base 16) systems.

Counting Systems

There are 4 major counting systems used with computers - decimal, binary, octal and hexadecimal. Notice that hexadecimal needs the letters A ..F to give 16 discrete digits.

|Counting System |Base |Digits in System |

|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 |

|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 |

Note that any base other than 10 must have a subscript to indicate what number system it is.

For example: 1210 the 10 indicates the number given uses base 10 (decimal)

011002 the 2 indicates the number given uses base 2 (binary)

|29 |28 |

| |Notes |

|eg 2 0.110 to binary | |

|0.1 ( 2 = 0 + 0.2 first digit = 0 |An exact decimal fraction may not have an exact binary equivalent. For |

|0.2 ( 2 = 0 + 0.4 second digit = 0 |divisions which do not go exactly go to one more place than asked for, |

|0.4 ( 2 = 0 + 0.8 third digit = 0 |then round. |

|0.8 ( 2 = 1 + 0.6 fourth digit = 1 | |

|0.6 ( 2 = 1 + 0.2 fifth digit = 1 | |

|0.2 ( 2 = 0 + 0.4 sixth digit = 0 |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 |

|etc etc. |round-off error. |

| | |

|Writing as a recurring binary number: | |

| | |

|0.110 = 0.0001100110011..2 | |

| | |

|Rounding of to, say, 6 binary points: | |

| | |

|0.110 = 0.0001102 | |

| | |

|eg 3 5.125 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 uses a base of 16 and to provide that we go beyond the 0-9 digits and use characters A-F.

|Decimal |0 |1 |2 |3 |

|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, 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.

|Binary |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 |6 | |06 |

| |(0000 0000) |(0000 0110) | | |

|0001 1100 |1 |12 | |1C |

| |(0001 0000) |(0000 1100) | | |

|0010 0001 | | | | |

|0001 0111 | | | | |

Storing Integers (ie. whole numbers) in the computer

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

Generally 8 bits = 1 byte.

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

1 Kilobyte = 1024 bytes = 8192bits

eg 64 Kb = 64 x 1024 = 65,535 memory locations.

ASCII code uses 7 bits to represent a character, EBCDIC code uses 8 bits.

See back of booklet for ASCII table.

What is meant by the following terms:

(a) bit

(b) nibble

(c) byte

(d) word

Unsigned Binary Numbers

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

One 8-bit byte can represent numbers ranging from:

0000 00002 = 010 to 1111 11112 = 25510

This range is not generally large enough to show

a integers greater than 255

b negative numbers

The group of bits that the computer handles as a whole is referred to as a word (ie a 16-bit machine can handle 16 bits at a time).

|15 |1-14 |0 |

Bit 15 Most significant bit eg a 16-bit word

Bits 1-14 Balance of the word

Bit 0 Least 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

To be able to represent negative or positive numbers, the most significant bit is reserved as the sign bit with “0” representing + (positive) and “1” representing – (negative).

eg - 3910 1 010 01112

+ 3910 0 010 01112

So instead of being able to represent integers from 0 - 255, with an 8-bit word you can represent integers from

-12710 1111 11112

to +12710 0111 11112

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

eg with a 4-bit word and sign magnitude

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

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

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

Note The largest number to be represented in 4 bits is 01112 or + 7

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

Note We 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, as a computer cannot subtract. So when we wish to do subtraction we need to be able to add the opposite (negative representation) of a 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 |

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 (Check by decimal addition)

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|13 = 01101 |

| |bit to get 01101.) Both numbers need to be represented with the same number of bits. |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 | 00110 = 6 |

| |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. | |

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 |6 = 00110 |

| |bit to get 00110.) Both numbers need to be represented with the same number of bits. |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 |11110 ( 00010 |

| |leftmost position is a 1, the number is in 2's complement form; convert the number back by taking the 2's|(two’s complement) |

| |complement of it, then convert to decimal and take the negative. |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 | - 2 1110 |

|+ 7 0111 |- 7 +1001 |

|10 1010 = -6 in two’s complement |-9 (1) 0111 = +7 in two’s complement |

| |(ignore carry) |

| | |

|In this example, the expected answer of 10 is outside the 4 bit two’s |In this example, the expected answer of –9 is outside the 4 bit two’s |

|complement range of –8 to +7 so the answer (in two’s complement |complement range of –8 to +7 so the answer is again incorrect. |

|interpretation) is 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

A Summary Table Showing the Different Codes for the Integers

|Code |The Represented Number |

|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 |

Computer Representation of 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 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download