CSC1004 Chapter 2

61
1 CSC1004 Chapter 2: Computer Number Systems & Arithmetic - Overview Chapter 2. Computer Number Systems and Arithmetic Overview Binary number system Representation of positive integer numbers Binary number arithmetic and logic operations Representation of signed numbers Negative integer numbers in binary format and their arithmetic Representation of real numbers Real numbers in binary format Representation of text and control characters ASCII code, Unicode References A. Clements, “The principles of computer

description

Computer Architecture lecture

Transcript of CSC1004 Chapter 2

Page 1: CSC1004 Chapter 2

1CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Overview

Chapter 2. Computer Number Systems and Arithmetic

• Overview– Binary number system

Representation of positive integer numbersBinary number arithmetic and logic operations

– Representation of signed numbersNegative integer numbers in binary format and their

arithmetic– Representation of real numbers

Real numbers in binary format– Representation of text and control characters

ASCII code, Unicode• References

A. Clements, “The principles of computer hardware” Chapter 4.

Page 2: CSC1004 Chapter 2

2CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Physics

1. Physics Background

1.1 Introduction• A computer is a machine built on electronic devices. The most

important device is Transistor. All operations inside a computer are performed using transistors; transistors are also used as computer memories to store information.

• Other popular devices include, for example, Capacitor or some Fuse-like material, which are used to build computer memories for storing information.

• New devices/material are being invented for building computers.

• These devices have a common characteristic – they can only effectively distinguish two different physical states.

Page 3: CSC1004 Chapter 2

3CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Physics

Page 4: CSC1004 Chapter 2

4CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Physics

Page 5: CSC1004 Chapter 2

5CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Physics

1.2 Transistor used as switch• In computers transistors operate in a so-called switching mode.

+Vcc

Vout

Vin

• When the input Vin is high (>3 volts), the transistor switches on, current flows down to the emitter and out to ground. This connects output to ground and Vout takes ground volts ~ 0 volts. Thus, high input (logic 1) yields low output (logic 0).

• When the input Vin is low, the transistor switches off, i.e. no current flowing in. This disconnects output from ground and Vout takes the Vcc volts. Thus, low input (logic 0) yields high output (logic 1).

Emitter

Collector

Base

Vcc~ +5 volts

.

Page 6: CSC1004 Chapter 2

6CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Physics

1.2 Transistor used as switch• In computers transistors operate in a so-called switching mode.

• When the input Vin is high (>3 volts), the transistor switches on, current flows down to the emitter and out to ground. This connects output to ground and Vout takes ground volts ~ 0 volts. Thus, high input (logic 1) yields low output (logic 0).

• When the input Vin is low, the transistor switches off, i.e. no current flowing in. This disconnects output from ground and Vout takes the Vcc volts. Thus, low input (logic 0) yields high output (logic 1).

.+Vcc

low volt high volt

0 volts

.+Vcc

high voltlow volt

0 volts

Two different physical states

110

0

Page 7: CSC1004 Chapter 2

7CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Physics

1.3 Capacitor as computer memory• A capacitor is an electrical device which can be used to store a

charge by application of a voltage. The simplest type of capacitor consists of two parallel plates with a voltage applied across them. A capacitor is characterized by its ratio of charge stored to applied voltage, called the capacitance and measured in Farads. Unlike a battery, when the voltage is removed, the capacitor discharges.

The voltage V across a capacitor with charge Q and capacitance C is given by

V = Q / C

        Two different physical states: Charged (1) Discharged (0)

Page 8: CSC1004 Chapter 2

8CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Physics

1.4 CD, DVD as computer memories• Laser is used to etch pits on the media surface to record

information.• A change from pit to land or land to pit indicates a one, while no

change indicates a series of zeros.

Two different physical states: Change (1) No change (0)

Page 9: CSC1004 Chapter 2

9CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

2. Binary Number System

2.1 Introduction

• As humans we are used to count and perform arithmetic using the Decimal system.

• However, in computer systems we have just seen that we have only two physical states to use to encode information (numbers, texts, images, audio, video etc.). These two physical states usually correspond to the low or high state of a voltage, or the off or on state of an electronic switch, used in the computer circuit. For convenience, we label these two states as {0, 1}.

• This leads to the Binary number system, and its derivatives Hexadecimal and Octal systems, which form the foundation of computer arithmetic.

Page 10: CSC1004 Chapter 2

10CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

132, 1023

-8766, -28

3.1415, -0.3369

A, z, %, č, И, 順 …

?

Computer Memory

What we will do in this Chapter …

Data, texts, audio, images video, …

Page 11: CSC1004 Chapter 2

11CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

2.2 Number systems• In the decimal system, we use 10 different values, {0, 1, 2, 3, 4, 5,

6, 7, 8, 9}, to represent numbers.

Base … 1000 (103) 100 (102) 10 (101) 1 (100)

Digits …

Count

0123456789

0123456789

0123456789

0123456789

0000

An odometer dial

Page 12: CSC1004 Chapter 2

12CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

• In the binary system, we use 2 different values, {0, 1}, to represent numbers.

Base … 128 64 32 16 8 4 2 1

27 26 25 24 23 22 21 20

Digits …

Count 0000

01

01

01

01

01

01

01

01

0 0 0 0

Page 13: CSC1004 Chapter 2

13CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

• Compare the decimal and binary systems.

Base … 1000 (103) 100 (102) 10 (101) 1 (100)

Digits …

Base … 128 (27) 64 (26) 32 (25) 16 (24) 8 (23) 4 (22) 2 (21) 1 (20)

Digits …

• A decimal number tells how many (0-9) … 1000s, 100s, 10s, 1s (i.e. powers of 10) there are in the value being represented.

• A binary number tells how many (0-1) … 128, 64, 32, 16, 8, 4, 2, 1 (i.e. powers of 2) there are in the value being represented.

• Base number changed from 10 to 2.

0123456789

0123456789

0123456789

0123456789

01

01

01

01

01

01

01

01

Page 14: CSC1004 Chapter 2

14CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

2.3 Representing integer numbers in binary format• To avoid confusion, we may use a subscript 10 or 2 to distinguish

between a decimal number and a binary number, e.g., 12310 is a decimal number, and 10112 is a binary number.

• Method 1:

What is the binary form of decimal number 10710?

Express 10710 as a combination of … 128, 64, 32, 16, 8, 4, 2, 1, then we will be able to work out the corresponding binary digits.

Since 10710 = 64 + 32 + 8 + 2 + 1

Base … 128 64 32 16 8 42 1

How many (1/0): 1 1 0 1 01 1

So 10710 = 11010112

Page 15: CSC1004 Chapter 2

15CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

• Method 2:– Divide decimal number by 2 and record the remainder– Keeping dividing the quotient obtained until new quotient is 0– Concatenate remainder values in reverse order to get the value

Example: what is the binary value of 4310 ?

43 ÷ 2 = 21 remainder 1

21 ÷ 2 = 10 remainder 1

10 ÷ 2 = 5 remainder 0

5 ÷ 2 = 2 remainder 1

2 ÷ 2 = 1 remainder 0

1 ÷ 2 = 0 remainder 1 STOP since quotient = 0

So 4310 = 1010112

How to check if this is correct?

1 + 2 + 8 + 32 = 4310

Page 16: CSC1004 Chapter 2

16CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

2.4 Binary numbers in computers• In computer systems, a digit in a binary number is known as a bit,

which has a value 0 or 1, which is expressed by using an electronic switch (e.g. a transistor or a capacitor) in off or on state.

• To represent significant amount of information, bits (i.e. electronic switches) are manipulated in groups. In particular, the term byte is used to stand for a collection of 8 bits (a collection of 8 switches).

• Bytes can be further concatenated into longer units to represent greater values. For, convenience, in computers binary numbers are always represented in a fixed-length format, e.g. 8 bits (a byte), 16 bits (2 bytes), 32 bits (4 bytes), or 64 bits (8 bytes).

− For example, the binary 1010112 (4310) represented in 8 bits is

00101011and in 16 bits is

00000000 00101011

Page 17: CSC1004 Chapter 2

17CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

• Always write binary numbers in a required fixed-length format; every bit must be defined (which tells how to set the electronic switch used to store the bit – on or off).

For example, 8 capacitors used to store a byte (a capacitor charged for 1, discharged for 0)

0 0 1 0 1 0 1 1

4310 is stored as 00101011, not 101011 (which did not specify the states of the top 2 switches).

• Given a binary number, the leftmost bit is called the most significant bit (MSB); the rightmost bit is called the least significant bit (LSB).

Page 18: CSC1004 Chapter 2

18CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

2.5 Hexadecimal number system• Unlike the decimal system which uses 10 different values to count,

the hexadecimal system uses 16 different values to count, like this:

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

• Hexadecimal numbers look the same as the decimal numbers up to 9, but then there are the letters (A, B, C, D, E, F) in place of the decimal numbers 10 to 15.

• The base numbers in the hex system: … 163, 162, 161, 1 (compared to … 103, 102, 101, 1 in the decimal system, and … 24, 23, 22, 21, 1 in the binary system).

• Hexadecimal numbers are particularly useful in computer studies as they can be easily derived from binary numbers. They are more compact than binary numbers and can be more readily committed to human memory.

Page 19: CSC1004 Chapter 2

19CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

• Want a short-hand notation for the following binary number?

1101001001111010100011111001

1101 0010 0111 1010 1000 1111 1001

D 2 7 A 8 F 9

So 11010010011110101000111110012 = $D27A8F9

• We use the prefix $ to indicate a hexadecimal number.• Converting a binary number to a hexadecimal number:

– Group every four bits in the binary number (append zeros at the MSB position if needed)

– Replace each group by its corresponding hexadecimal value• A byte (8 bits) can be written in 2 hex digits.• Always write binary in groups of 4 bits (1011 0111, not 10110111).

Grouping

Looking up table

Page 20: CSC1004 Chapter 2

20CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

• A summary of the decimal, binary, hexadecimal and octal systems for representing decimal 0 – 15:

Dec Bin Hex ($) Octal0 0000 0 01 0001 1 12 0010 2 23 0011 3 34 0100 4 45 0101 5 56 0110 6 67 0111 7 78 1000 8 109 1001 9 1110 1010 A 1211 1011 B 1312 1100 C 1413 1101 D 1514 1110 E 1615 1111 F 17

• A prefix $ is used for a hex number, e.g. $200.

Page 21: CSC1004 Chapter 2

21CSC1004 Chapter 1: Computer Number Systems & Arithmetic -

Binary System

2.6 Converting integers between decimal and hexadecimal• Convert integers from decimal to hexadecimal

− Successively divide by 16 until you get to 0; the remainders, read backwards, are the hex digits.

− Example: what is the hexadecimal value of 4310 ?

43 ÷ 16 = 2 remainder 11 d0= B

2 ÷ 16 = 0 remainder 2 d1= 2

Result: d1d0 = $2B

• Convert integers from hexadecimal to decimal

− Example: $3CE = ( )10?

Note the base numbers for the hexadecimal system are

Base … 4096 (163) 256 (162) 16 (161) 1 (160)

So $3CE = 3×256 + 12×16 + 14

= 97410

Page 22: CSC1004 Chapter 2

22CSC1004 Chapter 1: Computer Number Systems & Arithmetic -

Binary System

2.7 Capacity of representation

No. bits Binary values No. different values

1 0,1 21

2 00,01,10,11 22

3 000,001,010,011,100,101,110,111 23

• An n-bit binary number can represent 2n different values

• So the number of different values that can be represented by– a byte (8 bits) is 28 = 256 (0, 1, …, 255)– 2 bytes is 216 = 65536 (0, 1, …, 65535)– 4 bytes is 232 = 4294967296 (0, 1, …, 4294967295)

Page 23: CSC1004 Chapter 2

23CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

Summary – Binary numbers

• A binary number describes a combination of 2’s powers to represent a value (as opposed to a decimal number which describes a combination of 10’s powers to represent the value).

• So by expressing a value as a combination of 2’s powers, we can find its binary number representation.

• Commonly seen 2’s powers include: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, … (corresponding to 2 with a power 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …, respectively). How large to go depends on the size of the value to be represented.

• With n digits (bits), we can represent a total of 2n different values.

• Hexadecimal numbers provide a convenient, compact expression of binary numbers and hence are widely used (every 4 bits in a binary number can be neatly represented by a single hex digit).

Page 24: CSC1004 Chapter 2

24CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

2.8 Binary arithmetic

• Addition: 0 + 0 = 0; 0 + 1 = 1; 1 + 0 = 1; 1 + 1 = 10 (with carry)

Example 1: add two 8-bit integers without carry

0110 1101 ( = $6D = 10910)

+ 0111 1101 ( = $7D = 12510)

1110 1010 ( = $EA = 23410)

Example 2: add two 8-bit integers with carry

1110 1000 ( = $E8 = 23210)

+ 0111 1000 ( = $78 = 12010)

1 0110 0000 ( = $160 = 35210)Carry

Page 25: CSC1004 Chapter 2

25CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

• Multiplication: 0 0 = 0; 0 1 = 0; 1 0 = 0; 1 1 = 1• The product of two numbers can be calculated using the shifting

and adding algorithm: 10 1 0 1 0 x 13 x 1 1 0 1 30 1 0 1 0 + 10 0 0 0 0 130 1 0 1 0 + 1 0 1 0 1 0 0 0 0 0 1 0

• The above algorithm can be implemented in software, as in early computers, and in hardware, as in modern, high-speed computers (which accomplishes a multiplication in a single operation).

• Note in the above example: the multiplication of two 4-bit numbers yields a 8-bit product. Implications?

• The result of (byte byte) must be stored at least in a 2-byte unit, or one will lose accuracy.

Page 26: CSC1004 Chapter 2

26CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

One of the history’s worst software bugs – Ariane Flight 501 destruction on June 4, 1996

– working code for the Ariane 4 rocket is reused in the Ariane 5, causing the conversion of a 64-bit floating-point number to a 16-bit signed integer - an out-of-range data condition …

– backup computer crashes, followed by a crash of the primary computer

– the rocket disintegrated 40 seconds after launch.

Page 27: CSC1004 Chapter 2

27CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

2.9 Binary logic operations• A binary bit can also be viewed as a logic (i.e. Boolean) variable,

which takes either of two values:

True 1False 0

• The basic logic operations include AND, OR, NOT (negation), EOR (exclusive OR).

• The characteristic of an operation can be described by using a truth table, showing all possible inputs and the corresponding outputs.

A, B are two logic variables as inputA B A AND B A OR B A EOR B NOT A0 0 0 0 0 10 1 0 1 1 11 0 0 1 1 01 1 1 1 0 0

Page 28: CSC1004 Chapter 2

28CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

• Most high-level programming languages provide operators to perform logic operations, called bitwise logic operations. The following give typical examples.

• Write any 8-bit binary number as

D = d7d6d5d4 d3d2d1d0

where di is the i’th bit in the number, equaling 0 or 1 (the actual value doesn’t matter)

• Mask operation. Use logic AND operation to force unwanted bits to zero while leaving the other bits of interest unaffected.

For example, mask out all bits except bit 2 and 3:

D AND $0C = 0000 d3d200

• Set operation. Use logic OR to set chosen bits to one while leaving the other bits unaffected.

For example, set the MSB and LSB in D to 1:

D OR $81 = 1d6d5d4 d3d2d11

Page 29: CSC1004 Chapter 2

29CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Binary System

Summary - Binary arithmetic

• Adding two binary integers (similar to adding two decimal integers), with/without a carry.

• A binary digit (bit) can also be viewed as a logic variable equaling either ‘True’ or ‘False’.

• Logic operations for logic variables

AND, OR, NOT, EOR

Each operation can be fully described by using a truth table, which lists all possible inputs and the corresponding outputs .

• Multiplying two binary integers (similar to multiplying two decimal integers) – two single byte multiplication is likely to produce a 2-byte long result!

Page 30: CSC1004 Chapter 2

30CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

132, 1023

-8766, -28

3.1415, -0.3369

A, z, %, č, И, 順 …

?

Computer Memory

What we will do in this Chapter …

Page 31: CSC1004 Chapter 2

31CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

3. Signed Number Representation and Arithmetic

3.1 Introduction• So far we have discussed the binary representation of integer

numbers assuming that they are all positive - therefore no need to consider their signs. These numbers have a particular name: unsigned numbers.

• However, any real computer must be able to deal with both positive and negative numbers. Combined, these are called signed numbers.

• In the binary system, several methods have been developed to represent signed numbers. Typically, these include the sign and magnitude, biased, and two’s complement representations.

• Different methods have different capacities, and different requirements for the hardware and software.

• The complementary representation dominates today’s computers.

Page 32: CSC1004 Chapter 2

32CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

3.2 Sign and magnitude representation• With n bits, one way of representing a signed number is to use the

most-significant bit (MSB) to indicate the sign of the number, as convention, 0 for positive and 1 for negative, and the remaining bits to represent the magnitude (i.e. value) of the number.

• Example: with a byte (8 bits)+1510 =

1510 =• The value of a number can thus be decided by:

(1)S Mwhere S is the sign bit (0 for positive and 1 for negative), and M is the magnitude (e.g. 1510, or 0001111, as above).

• The range of representation: with a byte, we can represent numbers in the range: 1 111 1111 = 12710 to 0 111 1111 = +12710.

• With n bits, the range is: (2n11) to +(2n11).

0 000 1111 ( = $0F)1 000 1111 ( = $8F)

Page 33: CSC1004 Chapter 2

33CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

• Sign and magnitude representation is straightforward, but has some inconveniences:– There are two zeros:

0000 0000 = +0 and 1000 0000 = 0so that architectural rule like “positive zero equals negative zero” may be required to prevent behavior contrary to mathematical conventions.

– Addition and subtraction need to be implemented separately – two sets of hardware required.

• Example: Add two positive 8-bit binary numbers:

0000 1011 + 0000 1110 = 0001 1001

This can be done by using a piece of hardware – adder.

However, add one positive number and one negative number:

0000 1011 + 1000 0110 = 0000 1011 0000 0110 = ?

We will need a different piece of hardware – subtractor.

Page 34: CSC1004 Chapter 2

34CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

3.3 Complementary representation• A negative number is represented by the two’s complement of the

corresponding positive number.• As a result, subtraction can be implemented using addition, so no

subtraction (hardware or software) is required.• There is nothing magical. Consider two decimal operations:

8 3 = 5

8 + (10 3) = 8 + 7 =15

The results are the same if we discard the leftmost digit (i.e. 1) in the second result (so 8 – 3 can be computed using 8 + 7!)

• Here, 7 is called the ten’s complement of 3 in terms of 7+3 = 10. So subtracting a number can be performed by adding the number’s complement. No need to perform any more subtractions?

• The answer is NO for the decimal system (getting the complement 7 of 3 needs subtraction 103 = 7), but YES for the binary system.

Page 35: CSC1004 Chapter 2

35CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

3.3.1 Obtaining the two’s complement of a binary number• Given a binary number, its complement – called two’s complement

– can be formed very easily in two steps:

1) Invert each bit in the number, i.e. 10, 012) Add 1

Example: Find the 8-bit 2’s complement for 8910 ?

8910 = 0101 1001

Invert each bit in 8910:

Add 1:

So 2’s complement of 8910 =

• Unlike decimal numbers, no subtraction is needed to obtain the two’s complement.

10 0 0111 0

1

1010 0111 = $A7

Page 36: CSC1004 Chapter 2

36CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

3.3.2 Two’s complement arithmetic• Positive numbers are represented in their normal binary forms.• Negative numbers are represented by the two’s complements of the

corresponding positive numbers.

Example: In 8 bits, 10210 = 0110 01102, 7810 = 1011 00102

The latter is obtained by

(1) Obtaining binary for the positive 7810 = 0100 11102

(2) Inverting each bit

(3) Adding 1 • Subtraction is performed using addition, with the following

procedures:– Replace the negative numbers in the formula by their two’s

complements (using the algorithm described above)– Perform addition with the complements – Discard the carry out (if any) from the most significant bit

Page 37: CSC1004 Chapter 2

37CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

• Example: Compute 127 64 using 8-bit 2’s complement arithmetic.Based on the above procedures, the subtraction is performed using the following addition:

127 + 2’s complement of 64

We know

127 = 0111 1111

64 = 1100 0000 (64 in 2’s complement representation)

So 127 64 = 127 + 2’s complement of 64 = 0111 1111 + 1100 0000 = 1 0011 1111

Discarding the 1 carried out of the MSB, we have the answer: 127 64 = 0011 1111= 63

Subtract becomes add

Page 38: CSC1004 Chapter 2

38CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

The power of mathematics – the impact of the two’s complement representation on the design of CPU (processor) hardware

Carry in

ABAorBB

Fun

ctio

nde

code

r

Fulladder Carry out

Output

Sum

Logical unit

F0

F1

A

B

.

.

.

Only adder is built which is able to perform both addition & subtraction

Page 39: CSC1004 Chapter 2

39CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

• Example: Compute 29 57 using 8-bit 2’s complement arithmetic.Based on the above procedures, this is calculated using the following addition:

29 + 2’s complement of 57

We know 29 = 0001 1101

57 = 1100 0111 (57 in 2’s complement representation)

So 29 57 = 29 + 2’s complement of 57= 0001 1101 + 1100 0111 = 1110 0100

No carry out from the MSB, we have the answer: 29 57 = 1110 0100 = 28

Subtract becomes add

Page 40: CSC1004 Chapter 2

40CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

3.3.3 Interpreting numbers in two’s complement arithmetic• In Section 2.2 we showed the base number system used to interpret

8-bit unsigned numbers:

Base 128 64 32 16 8 4 2 1

Digits

• In contrast, we can use the following base number system to interpret 8-bit numbers in the two’s complement domain

Base 128 64 32 16 8 4 2 1

Digits

• In the two’s complement domain the base number associated with the MSB is negative - used to represent negative values.

01

01

01

01

01

01

01

01

01

01

01

01

01

01

01

01

Page 41: CSC1004 Chapter 2

41CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

• Interpret 8-bit numbers in the two’s complement domain

Base 128 64 32 16 8 4 2 1

Digits

• Example: Assume two 8-bit numbers produced by 2’s complement arithmetic: 0011 0001, 1011 0001. What decimal value do each number represent?

0 0 1 1 0 0 0 1

1 0 1 1 0 0 0 1

So 1st number = 32+16+1 = 49

So 2nd number = 128+32+16+1 = 79

• So the MSB of a two’s complement number determines the sign of the number: 0 for positive and 1 for negative.

01

01

01

01

01

01

01

01

Page 42: CSC1004 Chapter 2

42CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

3.4 Ranges of integer numbers

• The range of values representable by a binary integer number is determined by two factors:− the length (i.e. how many bytes) of the number − the number being signed or unsigned

Byte (8-bit) Two bytes integer Four bytes integer unsigned signed unsigned signed unsigned signed

0 128 0 32768 0 2147483648 255 +127 65535 +32767 4294967295 +2147483647

• Some data types in Javebyte - one byte, char - 2 bytes (Unicode), int - 4 bytes

• Some data types in C++char - one byte (ASCII code), int - 4 bytes

Page 43: CSC1004 Chapter 2

43CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

3.5 Overflow• Overflow comes about when the result of an operation does not fit

the representation being used. • Example: Positive overflow. In 8-bit 2’s complement system:

0111 1000 = 12010

+ 0000 1001 = 910

1000 0001 = 12710 (positive + positive = negative!)

• Example: Negative overflow. In 8-bit 2’s complement system: 1000 1000 = 12010

+ 1111 0111 = 910

1 0111 1111 = +12710 (discard the carry, neg. + neg. = pos.!)

• Both represent an out-of-range condition.• Overflow is a common trap for the unwary, especially in

programming languages like C, C++, various assembly languages, and machine language, which have no built-in checks for it.

Page 44: CSC1004 Chapter 2

44CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

3.6 Biased representation• Signed numbers may be converted into unsigned numbers by

adding a positive, constant bias b.• For example, 8 signed numbers

[–4 –3 –2 –1 0 1 2 3]

can be converted into 8 unsigned numbers by adding b = +4 to each of the numbers, giving

[–4 –3 –2 –1 0 1 2 3] + 4 = [0 1 2 3 4 5 6 7] • Therefore, instead of storing [–4 –3 –2 –1 0 1 2 3], we store [0 1 2

3 4 5 6 7] using the unsigned binary format. • The actual signed value is retrieved by subtracting the bias b from

the stored value. This is often called excess b representation, because the stored number exceeds the true number by b.

• Biased representation is not efficient in terms of computation. It is used to encode the exponent part in IEEE floating point numbers.

Page 45: CSC1004 Chapter 2

45CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

Example for biased representation:

–4, –3, –2, –1, 0, 1, 2, 3

1

Take 4 from the stored number

–3 The actual number

Add 4 to make all numbers positive

0, 1, 2, 3, 4, 5, 6, 7

Saved in computer in normal binary

0, 1, 2, 34, 5, 6, 7

Memory

Page 46: CSC1004 Chapter 2

46CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Signed Numbers

Summary – Signed number representation

• Two’s complement is used to represent signed integers in all computers− Negative integers are represented by complements, obtained by

inverting the bits and adding 1− Subtracting an integer is performed by adding the integer’s

complement− The base number system for interpreting the values of signed

integers, e.g. for 8-bit signed integers:128 64 32 16 8 4 2 1

− The sign of a signed integer is determined by its MSB• MSB = 0: positive• MSB = 1: negative

− Range of representation and the overflow problem

• Other two representations: sign and magnitude, biased – less efficient but have also found applications (see discussions later).

Page 47: CSC1004 Chapter 2

47CSC1004 Chapter 2: Computer

Number Systems & Arithmetic - Real Numbers

132, 1023

-8766, -28

3.1415, -0.3369

A, z, %, č, И, 順 …

?

Computer Memory

What we will do in this Chapter …

Page 48: CSC1004 Chapter 2

48CSC1004 Chapter 2: Computer

Number Systems & Arithmetic - Real Numbers

4. Real Number Arithmetic

4.1 Introduction

• So far we have discussed the binary representation of signed and unsigned integer numbers. As a computer, it must also be able to deal with real numbers, which include both integers and fractions.

• In computer systems, real numbers may be represented in different ways, for example, the fixed-point format and the floating-point format.

• Floating-point format is by far the most common and useful on general purpose computers.

• Industrial standard (e.g. IEEE standard) has been introduced for floating-point arithmetic operations in mini- and microcomputers.

Page 49: CSC1004 Chapter 2

49CSC1004 Chapter 2: Computer

Number Systems & Arithmetic - Real Numbers

4.2 Binary representation of real numbers• Recall: a binary integer tells how many (0-1) … 128, 64, 32, 16, 8,

4, 2, 1 (i.e. powers of 2) in the value being represented.

• A binary fraction tells how many (0-1) 1/2, 1/4, 1/8, 1/16, 1/32, 1/64, 1/128, … (i.e. negative powers of 2) in the value being represented.

Example: 22.7510 = 10110.112

• An algorithm for finding the binary representation of a fraction:

Successively multiply by 2 and strip the integer part from the result until you reach 0 or a certain degree of precision; the stripped integers, read forwards, are the fractional binary digits.

Example: Representing 0.7510 in binary 0.752=1.5 integer part stripped and recorded: 10.5 2=1.0 integer part stripped and recorded: 10.0 2=0 Stop Result: 0.7510 = 0.112

Page 50: CSC1004 Chapter 2

50CSC1004 Chapter 1: Computer

Number Systems & Arithmetic - Real Numbers

3.1415926535 8979323846 2643383279 5028841971 6939937510 5820974944 5923078164 0628620899 8628034825 3421170679 8214808651 3282306647

0938446095 5058223172 5359408128 4811174502 8410270193 8521105559 6446229489 5493038196 4428810975 6659334461 2847564823 3786783165 2712019091 4564856692 3460348610 4543266482 1339360726 0249141273 7245870066 0631558817 4881520920 9628292540 9171536436 7892590360 0113305305 4882046652 1384146951 9415116094 3305727036 5759591953 0921861173 8193261179 3105118548 0744623799 6274956735 1885752724 8912279381 8301194912 9833673362 4406566430 8602139494 6395224737 1907021798 6094370277 0539217176 2931767523 8467481846 7669405132 0005681271 4526356082 7785771342 7577896091 7363717872 1468440901 2249534301 4654958537 1050792279 6892589235 4201995611 2129021960 8640344181 5981362977 4771309960 5187072113 4999999837 2978049951 0597317328 1609631859 5024459455 3469083026 4252230825 3344685035 2619311881 7101000313 7838752886 5875332083 8142061717 7669147303 5982534904 2875546873 1159562863 8823537875 9375195778 1857780532 1712268066 1300192787 6611195909 2164201989 3809525720 1065485863 2788659361 5338182796 8230301952 0353018529 6899577362 2599413891 2497217752 8347913151 5574857242 4541506959 5082953311 6861727855 8890750983 8175463746 4939319255 0604009277 0167113900 9848824012 8583616035 6370766010 4710181942 9555961989 4676783744 9448255379 7747268471 0404753464 6208046684 2590694912 9331367702 8989152104 7521620569 6602405803 8150193511 2533824300 3558764024 7496473263 9141992726 0426992279 6782354781 6360093417 2164121992 4586315030 2861829745 5570674983 8505494588 5869269956 9092721079 7509302955 3211653449 8720275596 0236480665 4991198818 3479775356 6369807426 5425278625 5181841757 4672890977 7727938000 8164706001 6145249192 1732172147 7235014144 1973568548 1613611573 5255213347 5741849468 4385233239 0739414333 4547762416 8625189835 6948556209 9219222184 2725502542 5688767179 0494601653 4668049886 2723279178 6085784383 8279679766 8145410095 3883786360 9506800642 2512520511 7392984896 0841284886 2694560424 1965285022 2106611863 0674427862 2039194945 0471237137 8696095636 4371917287 4677646575 7396241389 0865832645 9958133904 7802759009 9465764078 9512694683 9835259570 9825822620 5224894077 2671947826 8482601476 9909026401 3639443745 5305068203 4962524517 4939965143 1429809190 6592509372 2169646151 5709858387 4105978859 5977297549 8930161753 9284681382 6868386894 2774155991 8559252459 5395943104 9972524680 8459872736 4469584865 3836736222 6260991246 0805124388 4390451244 1365497627 8079771569 1435997700 1296160894 4169486855 5848406353 4220722258 2848864815 8456028506 0168427394 5226746767 8895252138 5225499546 6672782398 6456596116 3548862305 7745649803 5593634568 1743241125 1507606947 9451096596 0940252288 7971089314 5669136867 2287489405 6010150330 8617928680 9208747609 1782493858 9009714909 6759852613 6554978189 3129784821 6829989487 2265880485 7564014270 4775551323 7964145152 3746234364 5428584447 9526586782 1051141354 7357395231 1342716610 2135969536 2314429524 8493718711 0145765403 5902799344 0374200731 0578539062 1983874478 0847848968 3321445713 8687519435 0643021845 3191048481 0053706146 8067491927 8191197939 9520614196 6342875444 0643745123 7181921799 9839101591 9561814675 1426912397 4894090718 64942319615679452080 9514655022 5231603881 9301420937 6213785595 6638937787 0830390697 9207734672 2182562599 6615014215 0306803844 7734549202 6054146659 2520149744 2850732518 6660021324 3408819071 0486331734 6496514539 0579626856 1005508106 6587969981 6357473638 4052571459 1028970641 4011097120 6280439039 7595156771 5770042033 7869936007 2305587631 7635942187 3125147120 5329281918 2618612586 7321579198 4148488291 6447060957 5270695722 0917567116 7229109816 9091528017 3506712748 5832228718 3520935396 5725121083 5791513698 8209144421 0067510334 6711031412 6711136990 8658516398 3150197016 5151168517 1437657618 3515565088 4909989859 9823873455 2833163550 7647918535 8932261854 8963213293 3089857064 2046752590 7091548141 6549859461 6371802709 8199430992 4488957571 2828905923 2332609729 9712084433 5732654893 8239119325 9746366730 5836041428 1388303203 8249037589 8524374417 0291327656 1809377344 4030707469 2112019130 2033038019 7621101100 4492932151 6084244485 9637669838 9522868478 3123552658 2131449576 8572624334 4189303968 6426243410 7732269780 2807318915 4411010446 8232527162 0105265227 2111660396 6655730925 4711055785 3763466820 6531098965 2691862056 4769312570 5863566201 8558100729 3606598764 8611791045 3348850346 1136576867 5324944166 8039626579 7877185560 8455296541 2665408530 6143444318 5867697514 5661406800 7002378776 5913440171 2749470420 5622305389 9456131407 1127000407 8547332699 3908145466 4645880797 2708266830 6343285878 5698305235 8089330657 5740679545 7163775254 2021149557 6158140025 0126228594 1302164715 5097925923 0990796547 3761255176 5675135751 7829666454 7791745011 2996148903 0463994713 2962107340 4375189573 5961458901 9389713111 7904297828 5647503203 1986915140 2870808599 0480109412 1472213179 4764777262 2414254854 5403321571 8530614228 8137585043 0633217518 2979866223 7172159160 7716692547 4873898665 4949450114 6540628433 6639379003 9769265672 1463853067 3609657120 9180763832 7166416274 8888007869 2560290228 4721040317 2118608204 1900042296 6171196377 9213375751 1495950156 6049631862 9472654736 4252308177 0367515906 7350235072 8354056704 0386743513 6222247715 8915049530 9844489333 0963408780 7693259939 7805419341 4473774418 4263129860 8099888687 4132604721 5695162396 5864573021 6315981931 9516735381 2974167729 4786724229 2465436680 0980676928 2382806899 6400482435 4037014163 1496589794 0924323789 6907069779 4223625082 2168895738 3798623001 5937764716 5122893578 6015881617 5578297352 3344604281 5126272037 3431465319 7777416031 9906655418 7639792933 4419521541 3418994854 4473456738 3162499341 9131814809 2777710386 3877343177 2075456545 3220777092 1201905166 0962804909 2636019759 8828161332 3166636528 6193266863 3606273567 6303544776 2803504507 7723554710 5859548702 7908143562 4014517180 6246436267 9456127531 8134078330 3362542327 8394497538 2437205835 3114771199 2606381334 6776879695 9703098339 1307710987 0408591337 4641442822 7726346594 7047458784 7787201927 7152807317 6790770715 7213444730 6057007334 9243693113 8350493163 1284042512 1925651798 0694113528 0131470130 4781643788 5185290928 5452011658 3934196562 1349143415 9562586586 5570552690 4965209858 0338507224 2648293972 8584783163 0577775606 8887644624 8246857926 0395352773 4803048029 0058760758 2510474709 1643961362 6760449256 2742042083 2085661190 6254543372 1315359584 50687724602901618766 7952406163 4252257719 5429162991 9306455377 9914037340 4328752628 8896399587 9475729174 6426357455 2540790914 5135711136 9410911939 3251910760 2082520261 8798531887 7058429725 9167781314 9699009019 2116971737 2784768472 6860849003 3770242429 1651300500 5168323364 3503895170 2989392233 4517220138 1280696501 1784408745 1960121228 5993716231 3017114448 4640903890 6449544400 6198690754 8516026327 5052983491 8740786680 8818338510 2283345085 0486082503 9302133219 7155184306 3545500766 8282949304 1377655279 3975175461 3953984683 3936383047 4611996653 8581538420 5685338621 8672523340 2830871123 2827892125 0771262946 3229563989 8989358211 6745627010 2183564622 0134967151 8819097303 8119800497 3407239610 3685406643 1939509790 1906996395 5245300545 0580685501 9567302292 1913933918 5680344903 9820595510 0226353536 1920419947 4553859381 0234395544 9597783779 0237421617 2711172364 3435439478 2218185286 2408514006 6604433258 8856986705 4315470696 5747458550 3323233421 0730154594 0516553790 6866273337 9958511562 5784322988 2737231989 8757141595 7811196358 3300594087 3068121602 8764962867 4460477464 9159950549 7374256269 0104903778 1986835938 1465741268 0492564879 8556145372 3478673303 9046883834 3634655379 4986419270 5638729317 4872332083 7601123029 9113679386 2708943879 9362016295 1541337142 4892830722 0126901475 4668476535 7616477379 4675200490 7571555278 1965362132 3926406160 1363581559 0742202020 3187277605 2772190055 6148425551 8792530343 5139844253 2234157623 3610642506 3904975008 6562710953 5919465897 5141310348 2276930624 7435363256 9160781547 8181152843 6679570611 0861533150 4452127473 9245449454 2368288606 1340841486

3776700961 2071512491 4043027253 8607648236 3414334623 5189757664 5216413767 9690314950 1910857598 4423919862 9164219399 4907236234 6468441173 9403265918 4044378051 3338945257 4239950829 6591228508 5558215725 0310712570 1266830240 2929525220 1187267675 6220415420 5161841634 8475651699 9811614101 0029960783 8690929160 3028840026 9104140792 8862150784 2451670908 7000699282 1206604183 7180653556 7252532567 5328612910 4248776182 5829765157 9598470356 2226293486 0034158722 9805349896 5022629174 8788202734 2092222453 3985626476 6914905562 8425039127 5771028402 7998066365 8254889264 8802545661 0172967026 6407655904 2909945681 5065265305 3718294127 0336931378 5178609040 7086671149 6558343434 7693385781 7113864558 7367812301 4587687126 6034891390 9562009939 3610310291 6161528813 8437909904 2317473363 9480457593 1493140529 7634757481 1935670911 0137751721 0080315590 2485309066 9203767192 2033229094 3346768514 2214477379 3937517034 4366199104 0337511173 5471918550 4644902636 5512816228 8244625759 1633303910 7225383742 1821408835 0865739177 1509682887 4782656995 9957449066 1758344137 5223970968 3408005355 9849175417 3818839994 4697486762 6551658276 5848358845 3142775687 9002909517 0283529716 3445621296 4043523117 6006651012 4120065975 5851276178 5838292041 9748442360 8007193045 7618932349 2292796501 9875187212 7267507981 2554709589 0455635792 1221033346 6974992356 3025494780 2490114195 2123828153 0911407907 3860251522 7429958180 7247162591 6685451333 1239480494 7079119153 2673430282 4418604142 6363954800 0448002670 4962482017 9289647669 7583183271 3142517029 6923488962 7668440323 2609275249 6035799646 9256504936 8183609003 23809293459588970695 3653494060 3402166544 3755890

There are quantities which require an infinite number of digits to represent!

Page 51: CSC1004 Chapter 2

51CSC1004 Chapter 2: Computer

Number Systems & Arithmetic - Real Numbers

4.3 IEEE standard 754 (1985)• Current and adopted by all general-purpose computers.• Real numbers are represented using fixed-length binary numbers,

in a floating-point format. The value of a floating-point number

V = (1)s 1. f 2e–b

where: s = a sign bit, 0 = positive, 1 = negative e = exponent in excess b f = fractional mantissa

• The sign-and-magnitude method is used to represent the sign of a real number (decided by the sign bit s).

• The biased method is used to represent the exponent (e is the stored value, always positive; e-b is the actual value which can become negative after subtracting b).

• A hidden bit (a ‘stolen’ bit) is implied, which increases the representation capacity without any hardware cost.

Hidden bit

Page 52: CSC1004 Chapter 2

52CSC1004 Chapter 2: Computer

Number Systems & Arithmetic - Real Numbers

• Two most popular types:

float : 4 bytes long, s = 1 bit, e = 8 bits, f = 23 bits, b=127

representing a value V = (1)s 1. f 2e–127

with range: 3.4 10+38 ~ 1.4 10–45

+1.4 10–45 ~ +3.4 10+38

double : 8 bytes long, s = 1 bit, e = 11 bits, f = 52 bits, b=1023

representing a value V = (1)s 1. f 2e–1023

with range: 1.7 10+308 ~ 4.9 10–324 +4.9 10–324 ~ +1.7 10+308

s (1 bit) e (8 bits) f (23 bits)

s (1 bit) e (11 bits) f (52 bits)

Page 53: CSC1004 Chapter 2

53CSC1004 Chapter 2: Computer

Number Systems & Arithmetic - Real Numbers

• Example: Inside a computer memory there is a 4-byte binary number representing a float variable

$42 03 00 00 (2 hex digits denoting a byte)

What is the value represented by this binary number?

The corresponding binary number is:

0100 0010 0000 0011 0000 0000 0000 0000

The storage format for float is

So s = 0e = 100 0010 02 = 13210 f = 000 0011 0000 0000 0000 00002

So the represented value is

V = (1)s 1. f 2e–b = (1)01.000 00112 25

= (1 + 1/2-6 + 1/2-7)25 = 32.7510

s (1 bit) e (8 bits) f (23 bits)

Page 54: CSC1004 Chapter 2

54CSC1004 Chapter 2: Computer

Number Systems & Arithmetic - Real Numbers

• Range of representation (e.g., for float)

–Infinity 3.410+38 1.410–45 0 1.410–45 3.410+38 +Infinity

• Limited-length representation has limited precision.

• Use longer units (e.g. double or long double) to reduce the chances of getting into underflow or overflow, and increase the representation precision.

• NaN (not a number). NaN is typically obtained if divide zero by zero (0/0). Any arithmetic operations involving a NaN always gives a NaN answer (e.g. 10/NaN = NaN).

Negative Too small to represent, treated as zero - underflow

Positive Too big to represent - overflow

Too big to represent - overflow

Page 55: CSC1004 Chapter 2

55CSC1004 Chapter 2: Computer

Number Systems & Arithmetic - Real Numbers

Summary – Real number representation• Bits after a binary point describe a combination of 1/2, 1/4, 1/8,

1/16, … to represent a value (as opposed to the digits after a decimal point which describe a combination of 1/10, 1/100, 1/1000, … to represent a value).

• The IEEE standard formats float, double are used to represent real numbers in general-purpose computers.

• Due to the use of limited bits in these standard formats, many real-world numbers may only be approximately represented inside computers.

Page 56: CSC1004 Chapter 2

56CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Characters

132, 1023

-8766, -28

3.1415, -0.3369

A, z, %, č, И, 順 …

?

Computer Memory

What we will do in this Chapter …

Page 57: CSC1004 Chapter 2

57CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Characters

5. Character Codes – ASCII and Unicode

• Fundamentally, computers just deal with numbers. They store letters and other characters by assigning a number for each one.

• The most popular encoding system for English (most common characters, numbers and punctuation) is ASCII, standing for American Standard Code for Information Interchange. ASCII uses a byte to represent characters; only the least significant 7 bits (0 to 127) are used, so it can represent no more than 128 characters. These include printable characters and control or action characters.

• ‘Plain’ text files (.txt) created using Notepad, or Microsoft Word and saved in “text only” format - with no formatting such as tabs, bold or underscoring - are in ASCII format.

• In C++, char is a byte-sized variable which can be used to store an ASCII character code.

Page 58: CSC1004 Chapter 2

58CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Characters

• The standard ASCII table

• Codes for 128 characters (from 0 to 127), the first 32 being non-printable, and the remaining 96 being printable.

• Each character is represented using a byte. Row numbers represent the first hex digit and the column numbers represent the second hex digit. For example, “A” is represented in hexadecimal $41.

Page 59: CSC1004 Chapter 2

59CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Characters

• ASCII may not be suitable for other languages in the world. Before Unicode was invented, there were hundreds of different character encoding systems, for example, the European Union alone requires several different encodings to cover all its languages.

✗✗ ✗✗✗ Vladimir

JelicačačićИгорь

Лукашев

徐順宏ก๊�ก๊เฮงแซ่�แต้

Bjørn Vestergård

Characters not standardized – data exchange limited

Page 60: CSC1004 Chapter 2

60CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Characters

• Unicode provides a unique code for every character in the world− Latest version covers more than 110,000 characters− Unicode text encoding examples:

Page 61: CSC1004 Chapter 2

61CSC1004 Chapter 2: Computer Number Systems & Arithmetic -

Characters

• The most commonly used encodings are UTF-8 and UTF-16− UTF-8: It uses only one byte to encode English characters (the

same as ASCII codes), and uses up to four bytes to encode the other characters. UTF-8 is widely used in email systems and on the Internet.

− UTF-16: It uses two bytes to encode the most commonly used characters, and uses four bytes to encode each of the additional characters.

• Java was created around the time when the Unicode standard had values defined for a much smaller set of characters. Back then it was felt that 16-bits would be more than enough to encode all the characters that would ever be needed. With that in mind Java was designed to use UTF-16. In fact, the char data type was originally used to be able to represent a 16-bit Unicode code point.

• The important thing to remember is a single char data type can no longer represent all the Unicode characters.