introduction to computer systems and software lecture 2 of 2 simon coupland [email protected]

62
Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland [email protected]

Post on 19-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Introduction to Computer Systems and Software

Lecture 2 of 2

Simon Coupland

[email protected]

Page 2: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Representation of Data Within the Computer Contents:

Decimal and Binary Integer Numbers Binary Addition Signed Binary Numbers Overflow Hexadecimal Numbers Number Conversion Real Numbers Character Encoding

Page 3: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Introduction

A few terms: A bit – a single Binary digIT, 0 or 1 A byte – eight bits A word – one or more bytes Integer – whole number Real number – a number with decimal points Binary – Base 2 numbers Octal – Base 8 numbers Decimal – Base 10 numbers (everyday numbers) Hexadecimal – Base 16 numbers

Page 4: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Decimal and Binary Numbers

We all use decimal numbers Base 10 numbers Example: 124

Digit 7 6 5 4 3 2 1 0

Digit Value 107 106 105 104 103 102 101 100

Digit Value 10M 1M 100k 10k 1k 100 10 1

Example 0 0 0 0 0 1 2 4

Page 5: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Decimal and Binary Numbers

Computers use binary numbers Base 2 numbers: Example: 124

Bit 7 6 5 4 3 2 1 0

Bit Value 27 26 25 24 23 22 21 20

Bit Value 128 64 32 16 8 4 2 1

Example 0 1 1 1 1 1 0 0

Page 6: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Decimal and Binary Numbers

More binary numbers:

00010011 = 16 + 2 + 1

= 19

01011101 = 64 + 16 + 8 + 4 + 1

= 93

11111111 = 255

00000000 = 0

Page 7: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

When adding binary numbers we use binary logic Binary Addition Truth Table 1:

A B A + B Carry

0 0 0 0

0 1 1 0

1 0 1 0

1 1 0 1

Page 8: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition Binary Addition Truth Table 2:

A B Carry (in) A + B Carry (out)

0 0 0 0 0

0 0 1 1 0

0 1 0 1 0

0 1 1 0 1

1 0 0 1 0

1 0 1 0 1

1 1 0 0 1

1 1 1 1 1

Page 9: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

Binary Addition Example:

00010001

+ 00011101

=

Page 10: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

Binary Addition Example:

00010001

+ 00011101

= 0

1

Page 11: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

Binary Addition Example:

00010001

+ 00011101

= 10

Page 12: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

Binary Addition Example:

00010001

+ 00011101

= 110

Page 13: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

Binary Addition Example:

00010001

+ 00011101

= 1110

Page 14: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

Binary Addition Example:

00010001

+ 00011101

= 01110

1

Page 15: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

Binary Addition Example:

00010001

+ 00011101

= 101110

Page 16: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

Binary Addition Example:

00010001

+ 00011101

= 0101110

Page 17: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

Binary Addition Example:

00010001

+ 00011101

= 00101110

Page 18: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

Binary Addition Q1:

00101101

+ 00011001

=

Page 19: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

Binary Addition Q1:

00101101

+ 00011001

= 01000110

Page 20: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

Binary Addition Q2:

00001110

+ 00001111

=

Page 21: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

Binary Addition Q2:

00001110

+ 00001111

= 00011101

Page 22: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

Binary Addition Q3:

01011001

+ 00111011

=

Page 23: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Binary Addition

Binary Addition Q3:

01011001

+ 00111011

= 10010011

Page 24: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Negative Binary Numbers

Sign-true Magnitude Left most bit holds sign Example: -10

Bit 7 6 5 4 3 2 1 0

Bit Value sign 26 25 24 23 22 21 20

Bit Value +/- 64 32 16 8 4 2 1

Example 1 0 0 0 1 0 1 0

Page 25: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Negative Binary Numbers

Ones complement All 1’s and 0’s are switched When negative, result = -255 + value Example: -10

Bit 7 6 5 4 3 2 1 0

Bit Value 27 26 25 24 23 22 21 20

Bit Value 128 64 32 16 8 4 2 1

Example 1 1 1 1 0 1 0 1

Page 26: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Negative Binary Numbers

Twos complement Left most bit holds sign When negative, result = -128 + result Example: -10

Bit 7 6 5 4 3 2 1 0

Bit Value Sign 26 25 24 23 22 21 20

Bit Value +/-128 64 32 16 8 4 2 1

Example 1 1 1 1 0 1 1 0

Page 27: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Negative Binary Numbers

Conversion to Twos complement Ones complement the byte/word Add 1

Example: 00001010 +10

11110101 Ones complement + 00000001 = 11110110

Page 28: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Why Use Twos Complement?

Because addition rules still work:

00010110 22

+ 10001000 + -120

= 10011110 = -98

Page 29: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Overflow

Overflow is when the number of bits is too small to store the result of an arithmetic operation

Example (twos complement) :

01011001 89

+ 01111011 + 123

= 11010011 = -45

Page 30: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Overflow

Overflow can be easily detected for signed binary numbers

Errors can then be corrected Adding two positive numbers should give a

positive result Adding two negative numbers should give a

negative result Adding a positive and negative number

together can never result in overflow. Why?

Page 31: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Hexadecimal Numbers

Writing code with long binary numbers would be cumbersome and error prone

A hexadecimal digit can take 16 values (0-F) One hex digit can represent a four bit word Examples:

Decimal Hexadecimal Binary

4 4 0100

10 A 1010

74 4A 01001010

Page 32: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Binary to Hexadecimal: From the least significant (rightmost) bit split the

binary number into groups of four bits. Each 4 bits has a hexadecimal equivalent Example:0001001110011110

0001 0011 1001 1110

1 3 9 E

139E

Page 33: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Hexadecimal to Binary: Convert each hexadecimal digit into its 4 bit binary

equivalent Join the all 4 bit words together Example:A42F

A 4 2 F

1010 0100 0010 1111

1010010000101111

Page 34: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Question Hexadecimal to Binary:

Convert D36B into binary

Page 35: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Question Hexadecimal to Binary:

Convert D36B into binaryD 3 6 B

1101 0011 0110 1011

Page 36: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Question Binary to Hexadecimal:

Convert 1001101001001010 into hexadecimal

Page 37: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Question Binary to Hexadecimal:

Convert 1001101001001010 into hexadecimal1001 1010 0100 1010

9 A 8 A

9A8A

Page 38: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Decimal to Binary Use the following algorithm, begin with LSB

int dec_value = some_number;

int next_bit;

while(dec_value > 0)

{

next_bit = dec_value % 2;

dec_value = dec_value / 2;

}

Page 39: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Decimal to Binary Convert 42 to binary:

dec_value /2 %2 Result

42 21 0 0

21 10 1 10

10 5 0 010

5 2 1 1010

2 1 0 01010

1 0 1 101010

Page 40: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Question Convert 39 to binary:

Page 41: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Question Convert 39 to binary:

dec_value /2 %2 Result

39 19 1 1

19 9 1 11

9 4 1 111

4 2 0 0111

2 1 0 00111

1 0 1 100111

Page 42: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Binary to Decimal Use the following algorithm, begin with MSB

int dec_value = 0;

int bit_value;

int bit_index = MSB_index;

while(bit_index >= 0)

{

bit_value = word[bit_index];

dec_value = dec_value * 2 + bit_value;

bit_index--;

}

Page 43: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Binary to Decimal Convert 011010 to decimal:

bit_index bit_value dec_value

5 0 0

4 1 1

3 1 3

2 0 6

1 1 13

0 0 26

Page 44: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Binary to Decimal Question, convert 101110 to decimal:

Page 45: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Binary to Decimal Question, convert 101110 to decimal:

bit_index bit_value dec_value

5 1 1

4 0 2

3 1 5

2 1 11

1 1 23

0 0 46

Page 46: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Decimal to Hexadecimal Use the following algorithm, begin with LSB

int dec_value = some_number;

char hex_digit;

while(dec_value > 0)

{

hex_digit = to_hex(dec_value % 16);

dec_value = dec_value / 16;

}

Page 47: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Decimal to Hexadecimal Convert 1863 to hexadecimal

dec_value dec_value / 16 dec_value % 16 hex_digit

1863 116 7 7

116 7 4 4

7 0 7 7

Page 48: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Decimal to Hexadecimal Question, convert 1437 to hexadecimal

Page 49: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Decimal to Hexadecimal Question, convert 1437 to hexadecimal

dec_value dec_value / 16 dec_value % 16 hex_digit

1437 89 13 D

89 5 9 9

5 0 5 5

Page 50: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Hexadecimal to Decimal Use the following algorithm, begin with MSB

int dec_value = 0;

char digit_value;

while(!word.off)

{

digit_value = to_dec(hex_digit);

dec_value = dec_value * 16 + digit_value;

}

Page 51: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Hexadecimal to Decimal Convert F2AC to decimal

Index hex_value dec_value result

3 F 15 15

2 2 2 242

1 A 10 3882

0 C 12 62124

Page 52: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Hexadecimal to Decimal Convert E59A to decimal

Page 53: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Number Conversion

Hexadecimal to Decimal Convert E59A to decimal

Index hex_value dec_value result

3 E 14 14

2 5 5 229

1 9 9 3673

0 A 10 58778

Page 54: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Real Numbers

A number n is a real number if n % 1 != 0 Take a lot of processing power/time Often math co-processors are used to

perform arithmetic on these numbers

Page 55: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Fixed Point Real Numbers

Similar to integer representation 8 bit example:

Bit 8 7 6 5 4 3 2 1

value -/+ 23 22 21 20 2-1 2-2 2-3

value -/+ 8 4 2 1 0.5 0.25 0.125

Page 56: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Fixed Point Real Numbers

Maximum number is small Accuracy is limited Not used because of these reasons

Page 57: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Floating Point Real Numbers

Floating point numbers are held in two parts Mantissa Exponent

In 0.3876 104

The mantissa is 0.3876 The exponent is 4

Page 58: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Floating Point Real Numbers

Example (not IEEE float):

00000000000000110010100111110011mantissa exponent

Mantissa = 809 Exponent = -13 809 10-13

0.0000000000809

Page 59: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Character Encoding

ASCII EBCDIC UNICODE ISO-8859-1

Page 60: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Character Encoding

ASCII American Standard Code for Information Interchange Characters are represented by integers Table in your notes

A = 65 B = 66 a = 96 z = 123 @ = 64

Page 61: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Character Encoding

ASCII Special functions characters are also encoded Examples:

Carriage return = 13 Escape = 27 Space = 32

Page 62: Introduction to Computer Systems and Software Lecture 2 of 2 Simon Coupland simonc@dmu.ac.uk

Recap

Binary numbers can be signed of unsigned Twos complement gives simple addition Overflow - when the result of a arithmetic

operation is too large to be represented Hexadecimal numbers – base 16 Real numbers use floating point Characters are represented as intergers