CS计算机代考程序代写 scheme Java PowerPoint Presentation

PowerPoint Presentation

Computer Systems
Week 1 – Numbers

Slide #2 of 46

Lecture Objectives

To introduce the fundamentals of number

representations, their conversion and how overflow can

create problems in computer programs.

Slide #3 of 46

Numbers – Lecture Outline

Why do we use binary?

Representing Text

Decimal, Binary, Octal and Hexadecimal Notations

How to convert from Decimal to Binary & vice versa?

Conversion between other notations?

Overflow – and avoiding it

Signed Integers – 2’s complement

 Illustration of Overflow using Factorial

Slide #4 of 46

What are Computers made of?

A collection of “switches” called

transistors.

Can either be “on” or “off”, corresponding

to a particular electrical state (conducting

or not).

Forces “binary” representation

“Off” → 0

“On” → 1

All data must be represented as

sequences of ones and zeros – binary

digits – bits

As must the computer’s “instructions”

Slide #5 of 46

Implications?

 For practical engineering reasons it is better (cheaper, more reliable,

faster …) to stick with basic components that only have two states –

binary

 But, in principle, we could build other types of computer – and

they have been built

 This applies within the processor, memory and all other components

 As an aside, all the data is just a pattern of bits. The interpretation put

on it is down to the program:

 You can add two character strings or instructions

 But, there are safeguards in most operating systems and

programming languages that restrict this.

 Historically, memory and processing was very limited. It still is, but not

to the same extent.

Slide #6 of 46

Representing Text

 Every text character is represented as a “binary string”

A: 1000001

B: 1000010

C: 1000011

 Text Encoding Schemes like ASCII, Unicode

 These usually use 8, 16 or 32 bits to encode characters

 Support internationalisation

 https://en.wikipedia.org/wiki/Character_encoding

 Strings (words, names, addresses etc) are usually represented by a block of

characters. See how Java does it.

 Numbers are more subtle

Need to worry about arithmetic

https://en.wikipedia.org/wiki/Character_encoding

Slide #7 of 46

Decimal Notation (Base = 10)
The key thing is that we

need to do arithmetic

Slide #8 of 46

Different Notations

System Base Symbols
Used by

humans?

Used in

computers?

Decimal 10 0, 1, … 9 Yes No

Binary 2 0, 1 No Yes

Octal 8 0, 1, … 7 No No

Hexa-

decimal

16 0, 1, … 9,

A, B, … F

No No

Slide #9 of 46

Counting in Different Number Systems (1 of 3)

Decimal Binary Octal
Hexa-

decimal

0 0 0 0

1 1 1 1

2 10 2 2

3 11 3 3

4 100 4 4

5 101 5 5

6 110 6 6

7 111 7 7

Slide #10 of 46

Counting in Different Number Systems (2 of 3)

Decimal Binary Octal
Hexa-

decimal

8 1000 10 8

9 1001 11 9

10 1010 12 A

11 1011 13 B

12 1100 14 C

13 1101 15 D

14 1110 16 E

15 1111 17 F

Slide #11 of 46

Counting in Different Number Systems (3 of 3)

Decimal Binary Octal
Hexa-

decimal

16 10000 20 10

17 10001 21 11

18 10010 22 12

19 10011 23 13

20 10100 24 14

21 10101 25 15

22 10110 26 16

23 10111 27 17 Etc.

Slide #12 of 46

Conversion among Different Notations

Hexadecimal

Decimal Octal

Binary

2510 = 110012 = 318 = 1916

Base

Slide #13 of 46

Conversion – Decimal to Binary

Technique

 Divide by two, keep

track of the

remainder

 First remainder is bit

0 (LSB, Least-

Significant Bit)

 Second remainder is

bit 1

 Etc.

125
10

= ?
2

Example

125
10

= 1111101
2

2 125

62 1
2

31 0
2

15 1
2

7 1
2

3 1
2

1 1
2

0 1

Binary

Decimal Octal

Hex

Slide #14 of 46

Conversion – Binary to Decimal

Technique

 Multiply each bit by

2n, where n is the

“weight” (“positional

value”) of the bit.

 The weight is the

position of the bit,

starting from 0 on the

right

 Add the results

Example

1010112 => 1 x 2
0 = 1

1 x 21 = 2

0 x 22 = 0

1 x 23 = 8

0 x 24 = 0

1 x 25 = 32

4310

Bit “0”

Binary

Decimal Octal

Hex

Slide #15 of 46

Conversion – Decimal to Octal

Technique

 Divide by 8

 Keep track of the

remainder

Example

1234
10

= 2322
8

1234
10

= ?
8

8 1234

154 28

19 2
8

2 3
8

0 2

Binary

Decimal Octal

Hex

Slide #16 of 46

Conversion – Octal to Decimal

Technique

 Multiply each digit by

8n, where n is the

“weight” of the digit

 The weight is the

position of the digit,

starting from 0 on the

right

 Add the results

Example

7248 => 4 x 8
0 = 4

2 x 81 = 16

7 x 82 = 448

46810

Binary

Decimal Octal

Hex

Slide #17 of 46

Conversion – Decimal to Hexadecimal

Technique

 Divide by 16

 Keep track of the

remainder

Example

1234
10

= 4D2
16

1234
10

= ?
16

16 1234

77 216

4 13 = D
16

0 4

Binary

Decimal Octal

Hex

Slide #18 of 46

Conversion – Hexadecimal to Decimal

Technique

 Multiply each digit by

16n, where n is the

“weight” of the digit

 The weight is the

position of the digit,

starting from 0 on the

right

 Add the results

Example

ABC
16

=>

C x 160 = 12 x 1 = 12

B x 161 = 11 x 16 = 176

A x 162 = 10 x 256 = 2560

2748
10

Binary

Decimal Octal

Hex

Slide #19 of 46

Conversion – Binary to Octal

Technique

 Group bits in threes,

starting on right

 Convert to octal

digits

Example

1011010111
2
= ?

8

1 011 010 111

1 3 2 7

1011010111
2
= 1327

8

Binary

Decimal Octal

Hex

Slide #20 of 46

Conversion – Octal to Binary

Technique

 Convert each octal

digit to a 3-bit

equivalent binary

representation

Note: Every digit will be

converted to 3 bits,

even if all bits are zeros

Example

705
8
= ?

2

7 0 5

111 000 101

705
8
= 111000101

2

Binary

Decimal Octal

Hex

Slide #21 of 46

Conversion – Binary to Hexadecimal

Technique

 Group bits in fours,

starting on right

 Convert to

hexadecimal digits

Example

1010111011
2
= ?

16

10 1011 1011

2 B B

1010111011
2
= 2BB

16

Binary

Decimal Octal

Hex

Slide #22 of 46

Conversion – Hexadecimal to Binary

Technique

 Convert each

hexadecimal digit to

a 4-bit equivalent

binary representation

Note: Every digit will be

converted to 4 bits,

even if all bits are zeros

Example

10AF
16

= ?
2

1 0 A F

0001 0000 1010 1111

10AF
16

= 0001000010101111
2

Binary

Decimal Octal

Hex

Slide #23 of 46

Conversion – Octal to Hexadecimal

Technique

 Use binary notation

as an intermediate

representation.

Example

1076
8
= ?

16

1 0 7 6

001 000 111 110

2 3 E

1076
8
= 23E

16

Binary

Decimal Octal

Hex

Slide #24 of 46

Conversion – Hexadecimal to Octal

Technique

 Use binary notation

as an intermediate

representation.

Example

1F0C
16

= ?
8

1 F 0 C

0001 1111 0000 1100

1 7 4 1 4

1F0C
16

= 17414
8

Binary

Decimal Octal

Hex

Slide #25 of 46

Exercise – Conversions (Home Work)

Decimal Binary Octal
Hexa-

decimal

33

1110101

703

1AF

Don’t use a calculator!