79

Representation of Data Within the Computer All data within is stored and processed as Binary Base 2 Number system only TWO characters are allowed 0 or

Embed Size (px)

Citation preview

Representation of Data Within the Computer

• All data within is stored and processed as Binary

• Base 2 Number system only TWO characters are allowed 0 or 1

• Binary digits = “Bits”• Two state system is ideal for a variety of

storage/processing methods e.g.– CDROMs Lands or pits– Barcodes black or white lines– Paper tape holes or no holes– Cards pencil mark or no pencil

mark

Denary (Decimal) Number System

• Placement of each digit determines its value• In Decimal, powers of 10 provide the value of each column

Decimal Number

103=1000 102=100 101=10 100=1

3407 3 4 0 7

968 0 9 6 8

1082 1 0 8 2

6600 6 6 0 0

123 0 1 2 3

Binary Number System

• In Binary, being a base 2 number system, powers of 2 provide the value of each column.

Binary Number

27=128 26=64 25=32 24=16 23=8 22=4 21=2 20=1

11010111 1 1 0 1 0 1 1 1

00101111 0 0 1 0 1 1 1 1

01110111 0 1 1 1 0 1 1 1

Counting in Binary

• Therefore decimal numbers can be represented as digital and visa versa

• A subscript number is used to indicate the number system being represented.

23=8 22=4 21=2 20=1

1 0 1 02 = 1010

1 1 1 02 = 1410

0 1 1 12 = 710

Counting in Binary

Decimal Binary

1 1

2 10

3 11

4 100

5 101

6 110

7 111

Hexadecimal Numbers

• Base 16 number system• Hex meaning six, dec meaning ten• Simplify task of writing binary; “shorthand

binary”• 8 digit binary number can be written as a two

digit hexadecimal number• We need 16 unique symbols to represent the

hexadecimal system, hence 0-9, A-F are used• Hexadecimal numbers are indicated by the

subscript number 16 e.g. 6E16

• Alternatively…. &H6E

Hexadecimal V Binary

Binary Hexadecimal

0000 0

0001 1

0010 2

0011 3

0100 4

0101 5

0110 6

0111 7

1000 8

1001 9

1010 A

1011 B

1100 C

1101 D

1110 E

1111 F

100010102 = 8A16

001011102 = ?

111001012 = ?

Hexadecimal to Decimal

• In Hexadecimal, being a base 16 number system, powers of 16 provide the value of each column.

Hexadecimal number

163=4096 162=256 161=16 160=1

113A16 1 1 3 A

4B3F16 4 B 3 F

Converting between hexadecimal, binary and decimal

• Binary to decimal conversion1101112 = 32 +16 + 0 + 4 + 2 + 1

= 5510

• Hexadecimal to decimal conversion5C316 = (5 x 162) + (C x 161) + (3 x 160)

= (5 x 256) + (12 x 16) + (3 x 1)= 1280 + 192 + 3= 147510

• Decimal to Binary conversion– Method 1 subtraction & Method 2 repeated division

Decimal to Binary conversion

Binary equivalent of decimal 109

109Method 1 Subtraction-64 1

45-32 1

130

13-8 1

5-4 1

10

1-1 1

0

11011012 = 10910

Result from top to bottom

Decimal to Binary conversion

Binary equivalent of decimal 109

Method 1 Repeated Division109

÷2 1

54÷2 0

271÷2

13÷2 1

6÷2 0

3÷2 1

1÷2 1

0

11011012 = 10910

Result from bottom to top

Character Representation

• Characters must be represented using binary code if it is to be stored and processed by computers.

• Two types of character require representation– Printable characters – A-Z,a-z, 0-9,

punctuation marks and special symbols i.e. $, %

– Non-printable characters or control characters• backspace, delete to remove data• Carriage returns and linefeed characters

influence formatting• Acknowledge (ACK) and neg. acknowledge

(NACK) characters are used in communication

Character Representation

• The TWO most common methods for coding characters into binary are:

• ASCII (American Standard Code for Information Interchange) ass-kee

– Used extensively on personal computers and for communication on the WWW.

• EBCDIC (Extended Binary Coded Decimal Interchange Code) eb-sih-dik

– Used extensively on large IBM computers

ASCII Code

• A seven bit code which means there are 128 different characters in the standard ASCII character set

• Extensions to standard ASCII are in use:– Extended ASCII is used within MS-DOS– ANSI characters are used within Windows

and Mac OS’s– ISO Latin 1 is an 8-bit system that includes

ASCII (developed by ISO for use on WWW)– These systems use values above 127 to

represent various foreign language, graphical and mathematical characters

ASCII Code

• Standard ASCII simplifies the process of sorting

• Code for “A” = 65 and is less than the code for “B” = 66

• Code for “a” = 97 as each capital letter has a code which is 32 less than its lower case equivalent

• A = 1000001• a = 1100001

Integer Representation

Whole numbers, both positive and negativeA subset of real numbersPositive integers and zero can easily be

represented using binary, but what about representing negative integers

Can’t use “-” as we can only work with 1 & 0Two methods• Sign and Modulus• Two’s complement

Sign and Modulus

• Adds an extra bit to the front of each number to represent the sign of the number

• 0 is positive• 1 is negative• Modulus is the magnitude of the number or

absolute valuee.g. 7 bits used to display number

12 = 00001100-12 = 10001100

Sign and Modulus

• How do we represent zero?• Two ways 00000000 or 10000000• Write the sign modulus for the following using

7 bits for the modulus:

-27

15

-75

=

=

=

10011011

00001111

11001011

Sign Modulus method is not generally used on modern computers

Two’s Complement

0 250 500 750 1000

Wrap to form a circle

0

250

500

750

- +Moving to the right or clockwise direction the integers increase in value

e.g. 1,2,3

Moving to the left or anti-clockwise direction the integers decrease in value by -1,-2,-3; but it is represented by the numbers 999,998,997… These numbers are the complements of their positive equivalents

Odometers

Another way to look at two’s complement is to consider the odometer on a car. Imagine that the odometer moves backwards when the car is in reverse

Two’s Complement

• Therefore positive integers 0 to 499 and negative from 999 down to 500

• Addition

-3

+ -5

997

995+

1992

Discard the leading 1 we get the correct answer 992 or -8

-8

Two’s Complement

-6

+ 19

994

19+

1013

Discard the leading 1 we get the correct answer 013 or 13

13

Two’s Complement

Binary odometer

Two’s Complement

Positive

Decimal Binary

1 00000001

2 00000010

3 00000011

4 00000100

5 00000101

6 00000110

7 00000111

8 00001000

9 00001001

10 00001010

11 00001011

12 00001100

13 00001101

14 00001110

15 00001111

16 00010000

Negative

Decimal Binary

-1 11111111

-2 11111110

-3 11111101

-4 11111100

-5 11111011

-6 11111010

-7 11111001

-8 11111000

-9 11110111

-10 11110110

-11 11110101

-12 11110100

-13 11110011

-14 11110010

-15 11110001

-16 11110000

Each positive number begins with a ‘0’ and each negative number begins with a ‘1’

Exchanging 1s for 0s and 0s for 1s almost results in the correct two’s complement, it is out by 1

Two’s Complement

To find the two’s complement of a number:1. Write down the binary equivalent of the

number.2. Perform a one’s complement i.e. switch 1s for

0s and 0s for 1s3. Add 1 to it.

Eg. Two’s complement of -14 00001110

111100011

+

11110010

Value of 14

Two’s Complement

-128 64 32 16 8 4 2 1 Decimal

1 1 1 1 0 1 0 0 -12

0 1 0 1 1 0 1 0 90

1 0 0 0 0 1 0 1 -123

0 0 0 0 1 1 0 1 13

Highest order bit

Lowest order bit

Representation of Fractions and Real Numbers

• Computers cannot represent all the possible real numbers precisely. It can work with approximations as we do.

E.g. 1.33333 -> 1.3

+∞-∞0

Negative Nos Positive Nos

Too largeToo large Too small

Rounding Off 8.426759 = 8.42676

Truncation 8.426759 = 8.42675

Representation of Fractions and Real Numbers

• Need a system that will provide a satisfactory level of accuracy for the majority of applications.

• There are two methods used for representing fractions:– Fixed Point– Floating Point

Fixed Point

• Refers to the position of the decimal point or in binary it is called the “radix point”

• When using fixed point numbers the radix point remains in the same position for all numbers

• E.g. a 16 bit fixed point system may have 8 bits for the whole number part and 8 bits for the fractional part

• Problem is that very large numbers and very small numbers can’t be represented

Fixed Point

22

4

21

2

20

1

Radix point

2-1

1/2

2-2

1/4

2-3

1/8

Decimal number

1 0 0 . 1 0 0 41/2

1 0 1 . 1 0 1 5 5/8

1 1 0 . 1 1 1 6 7/8

Floating Point

• Floating point can be thought of as a generalised fixed point numbers

• The position of the radix point is encoded within the format. In this way the radix point can be placed in a different position within a number

• Number is stored using scientific notation 2345.678 can be written as 2.345678 x 103

which can be written as 2.345678E+3• Three components to each number

+ 2.234567 x 103

mantissasign exponent

Floating Point

• The value of the exponent determines the position of the radix point

• Floating point system allows us to represent a much larger range of numbers using less storage space

E.g. 0.000000000000000000000052 Can be written as +5.2E-23• Working with floating point numbers,

compared to integers, is a complicated and processor intensive process

• Today’s microprocessors contain a special maths co-processor called a Floating Point Unit or FPU

Floating Point

• The Institute of Electrical and Electronics Engineers (IEEE) has produced a standard for representing binary numbers using the floating point system

• Standard 754 specifies both a single precision format (using 32 bits) and a double precision format (using 64 bits)

• All major hardware and software developers have accepted these standards

IEEE 754 Single Precision Floating Point Standard

• Requires 32 bits• First bit is the sign bit• Next 8 bits are used to store the exponent• Final 23 bits are the fractional part 1 11001010 00000000000000000011001

-1.11001 x 21101

mantissasign exponent

0 = +ive1 = -ive Its decimal

value of 13 plus 127

Same as original number but with the leading 1 ignored

IEEE 754 Single Precision Floating Point Standard

• Largest number in binary 2 x 2127 or 2128

On a calculator, in decimal that equates to

-3.4 x 1038 to 3.4 x 1038

IEEE 754 Single Precision Floating Point Standard

Special Cases

Infinity 0 11111111 00000000000000000000000

Negative infinity

1 11111111 00000000000000000000000

Zero 0 00000000 00000000000000000000000

Negative zero 1 00000000 00000000000000000000000

Not a number* x 11111111 xxxxxxxxxxxxxxxxxxxxxxx

*used for meaningless operations such as division by zero and square roots of negative numbers

IEEE 754 Double Precision Floating Point Standard

• Uses 64 bits to represent each number• A sign bit• 11 bits for the exponent• 52 bits for the mantissa

Binary Arithmetic - Addition

• Perform the operation similarly to decimal numbers

• Starting with the RHS add the column and if the result is 10 or 11 ‘carry’ the 1 to the next column

Binary Arithmetic - Addition

01010111 01011101 11000000

00011100 01110100 01110000

+ + +

110

1

0

1

11

1

10 100

1

0

1

1

1

0

1

1

1

1This result is a negative number in 8 bit two’s complement, as the leading bit is one. How come?

The addition in decimal is 93 + 116 = 209

But 209 is outside the range of values available in 8 bit two’s complement. The result has wrapped around into the negatives

00001100

1 1

This results in a carry in the final stage of the addition. As we only have 8 bits to work with we ignore this last carry

Binary Arithmetic - Subtraction

• Computers do not subtract, but they do add negative numbers

• i.e 5 – 3 is the equivalent of 5 + (-3)• Hence all subtractions are additions• In Two’s Complement we convert the number

being subtracted to its two’s complement form and then add

01010111

00011100

– becomes

0101011111100100

+

1 1 0 1 1 1 0 0

111

Binary Arithmetic - Subtraction

00011001

11111011

- becomes 00011001

00000101

+

01111000

1

25 – (-5) 25 + 5 = 30

Binary Arithmetic - Multiplication

• Multiplication in binary is the same as that in decimal but we only have two digits to be concerned with

• Used the “Shift and add” method

1001 x111

100110010

100100

111111

1001101

x

100100000

100100

101101

Binary Arithmetic - Multiplication

• A microprocessor need not know how to multiply if it is able to shift bits to the left and it can add

• 8086 chip has assembler instructions including SAL “Shift Arithmetic Left” and SAR “Shift Arithmetic Right” used in conjunction with the addition instructions ADD (addition) and ADC (addition with carry)

• All microprocessors have the equivalent of these instructions “hardwired” as part of their micro-code instruction set

Binary Arithmetic - Division

1 1 0 1 1 1 0 1 0

1

1 1 0

1 0

0

1

0

0

1 1 0

1

1 0 0

58 / 6 = 9 r 4

Binary Arithmetic - Division

• Most complex of the four operations• One method involves using partial fractions• Another method uses a “shift and subtract”

technique

2003 HSC Question

(i) Explain how a fraction is represented in single precision floating point binary representation.

(ii) Convert the decimal number 45 (ie 4510) to a hexadecimal number.

(iii) Using four-bit binary representation and two’s complement, perform the following subtraction: 1110–0111.

Electronic Circuits to Perform Standard Software Operations

• Electronic circuit – a circular path along which electrons (electricity) can flow. The circuit must be complete for this to occur.

• Logic Gate – an electronic circuit that transforms inputs into outputs based on strict logical rules

• Integrated circuits are made up of combinations of logic gates

Logic Gates

Logic gates are electric circuits that produce a true or false value (1 or 0 output signal) depending upon the input requirements of the circuit.

They are usually implemented in the computer using integrated circuits; a small silicon chip containing components such as resistors, diodes, transistors, and capacitors.

AND Gate

The output value of C is 1 only when BOTH A and B are 1.

Truth Table

OR Gate

The output value of C is 1 when EITHER A or B (or both) is 1.

Truth Table

NOT Gate

The output value of output (C) is the inverse (opposite) of the input (A)

Truth Table

NAND (Not And) Gate

The opposite of the AND gate. The output at C is 1 except when BOTH A and B are 1

Truth Table

NOR (Not Or) Gate

The opposite of the OR gate. The output at C is 1 only when BOTH A and B are 0

Truth Table

XOR Gate

The output at C is 1 except when BOTH A and B are the SAME.

Truth Table

Circuit Design

Designing a circuit to perform a particular function requires a systematic procedure:

1. Identify inputs and outputs: Determine the number of inputs and outputs needed.

2. Identify required components: Determine which logic gates are needed.

3. Check the solution with a truth table: Test whether the solution works as required.

4. Evaluate the circuit design: Ensure circuit is efficient, and meets design criteria.

Sample Circuit

Evaluate the circuit above and draw a truth table for the logic design.

Sample Circuit

A B C D = A AND B E = C OR D

Sample Circuit

A B C D = A AND B E = C OR D

0 0 0 0

0 0 1 0

0 1 0 0

0 1 1 0

1 0 0 0

1 0 1 0

1 1 0 1

1 1 1 1

Sample Circuit

A B C D = A AND B E = C OR D

0 0 0 0 0

0 0 1 0 1

0 1 0 0 0

0 1 1 0 1

1 0 0 0 0

1 0 1 0 1

1 1 0 1 1

1 1 1 1 1

Digital Circuits

The integrated circuits that comprise the CPU and RAM are made up of millions of tiny logic gates.

In this course we concentrate on two types of circuits

• Adders : Circuits that add numbers

• Flip-Flop : Circuits that store results

Binary Adders

The result from the addition of two digits in binary can best be described as two digits called the Sum and the Carry.

A B Carry Sum Explanation

0 0 0 0 0 + 0 = 00

0 1 0 1 0 + 1 = 01

1 0 0 1 1 + 0 = 01

1 1 1 0 1 + 1 = 10

A digital circuit called a half-adder can be created to perform this calculation.

Half Adder

The half adder is a simple digital circuit that will add two binary digits together and give the result as a sum digit and a carry digit.

In the HSC you may encounter two different (though equivalent) circuits to represent the half adder.

Half adder using the XOR structure.

Half adder using only AND, OR and NOT gates. Complete a truth table for this structure to confirm that the two digital circuits are functionally equivalent.

Binary Adders

Although the Half-Adder can add two binary digits together, it is not useful in practical application as the half-adder cannot add the carry from a previous addition. For example consider the addition of the following binary numbers : 1010 + 011

A Full-Adder can be made by combining two Half-Adders together. Using a full-adder, the carry (as shown above) can be accommodated in the addition procedure.

Full Adder

A full adder is a digital circuit made by combining two half adders and introducing an OR gate. It has three inputs (two data bits plus an optional carry from a previous addition) and two outputs (Sum and Carry). It has the advantage that it can accept a carry bit from a previous addition.

Multiple full adders can be joined together to carry out the addition of multiple binary digits – for example, eight would be needed to add together an eight bit number.

Storage of Binary Data

Binary data can be stored in logical circuits called Flip-Flops. A flip-flop’s basic characteristic is that it can be only in one of two possible states at any given time and that it will remain in this state until commanded to change. Because flip-flops stay in one of two stable states they are called bistable devices

Memory modules like RAM are made up of millions of tiny flip-flops

Flip-Flop

The simplest flip-flop is called the reset-set or RS Flip-Flop.

Like most flip-flops, the RS flip-flop has two outputs – however one will always be the opposite (complement) of the other.

Flip-Flops are hard to understand until you see them working. Use the java applet (XLogicCircuits) on the web page below to help you. Create the flip-flop circuit and draw a truth table

http://www.muirfield-h.schools.nsw.edu.au/staff/intranet/coruscant/computing/Misc/tmcm-java-web-site/tmcm-java/labs/xLogicCircuitsLab1.html

When a 1 is applied to both the set and reset inputs of the flip-flop, both Q1 and Q2 outputs go to 0. This condition violates the fact that both outputs are complements of each other. In normal operation this condition must be avoided by making sure that 1's are not applied to both inputs simultaneously.

Flip-Flop

The RS Flip-Flop can also be created using AND gates

Flip-Flops are hard to understand until you see them working. Use the java applet (XLogicCircuits) on the web page below to help you. Create the flip-flop circuit and draw a truth table

http://www.muirfield-h.schools.nsw.edu.au/staff/intranet/coruscant/computing/Misc/tmcm-java-web-site/tmcm-java/labs/xLogicCircuitsLab1.html

2003 HSC Question

Programming of Hardware Devices

• Input into Computer systems is via traditional devices such as keyboard, mouse or CD-ROM

• It may come from external sensors that react to light, temp, flow, pressure or movement.

• Sensor – any device that converts input from the environment into a digital signal. Uses ADC

• Output from computer systems include monitors, printers and sound or..

• Actuators are output devices that perform a mechanical action e.g. Robot arm, unlocking a door.

Input and Output Streams

• Data Streams refer to the string of bits that move into computer systems (input) and out of computer systems (output)

• Data Streams consist of:– Actual data– Information to ensure its correct delivery

• Format commences with Header information followed by the data itself, followed by Trailer information

Header Data Trailer

Input and Output Streams

• Manufacturers of hardware devices produce specifications detailing the precise nature and format of the data stream

• It is the responsibility of the software developer to interpret and create these data streams correctly

Detail of Data Stream

Header can consist of :• Protocol including Error checking method• Startbit• Date/Time information• Expected size of data packet

Data can consist of :• Data including control characters

Trailer can consist of :• Error checking data including parity• Stopbit

Header Data Trailer

Computer Mouse

• Standard mouse (mechanical)• Motion sensor X and Y position• Button sensors• X and Y sensors send a series of 1s and 0s to

the mouse’s IC which converts it into coded form and sends to PC via a port.

• PS2 port (IBM) receives synchronous data (timed) Pin No Name

1 Data

2 Reserved

3 Ground

4 Power

5 Clock

6 Reserved

4 3

5

12

6

Computer Mouse

• Each packet is eleven bits in length containing start bit, eight data bits, an odd parity bits, and a stop bit.

• Movement in each direction is sent using 8 bits• All data packets are made up of three distinct

components: header info, data and trailer info.

• Once streamed has been received by PC it will require processing

0 00101010 01 0 00011000 11 0 01100110 11

Start bit data bits Odd parity bit Stop bit

Control Systems

• Control system are made up of:• Sensors • Actuators• Open Control systems are NOT able to react to

its environment – No Feedback e.g Hotel Room locking/unlocking systems using cards

• Closed Loop systems are able to react to their environments – have feedback

2003 HSC Question