ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel:...

44
ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: http://portal.ece.umassd.edu/~pviall/ece160 Book: Computer Science: A Structured Programming Approach Using C B. A. Forouzan and Richard F. Gilberg Second Edition (or latest); Brooks/Cole, 2001.

Transcript of ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel:...

Page 1: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

ECE 160 – Comp Programming I Data Structures with C

Prof. P. H. Viall Office: Research 217

Tel: (508) 999-8240

Web: http://portal.ece.umassd.edu/~pviall/ece160

Book: Computer Science: A Structured Programming Approach Using CB. A. Forouzan and Richard F. GilbergSecond Edition (or latest); Brooks/Cole, 2001.

Page 2: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Tentative Grading• Exams (75%)

Best 3 of 4 exams (no makeups)Exams are 1 hour in class examsExams MAY be open book/note

• Homework/Programs/Labs (25%)

Note – scheme is tentative – grading may be adjusted ±2%

Page 3: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Working Together (programs)• is OK - TO A POINT• Working together appropriately means…

– Talking about a problem in general terms– One person helping another to fix a particular error

• Working together INAPPROPRIATLY means…– One person working, and one copying.– Person A does program 1 and person B copies, then

person B does program 2 and A copies.– One person using the mouse and one using the

keyboard to input a program, and then submit two copies of the same program (with just the persons name altered).

Page 4: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Working Together (programs)• If you do work together, please indicate

who you were working with• Penalty for INAPPROPRIATLY working

together– first offense: written warning; both on

homework and email to UMD account– second offense: grade of homework halved– third offense: grade of zero on homework– further offenses will result in lowering of

course grade, possibly to 'F'

Page 5: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

UMD Policy on Academic DishonestyTaken from UMD Catalog 2003-2004, page 51

• All UMass Dartmouth students are expected to maintain high standards of academic integrity and scholarly practice. The University does not tolerate academic dishonesty of any variety, whether as a result of a failure to understand proper academic and scholarly procedure or as an act of intentional dishonesty.

• A student found guilty of academic dishonesty is subject to severe disciplinary action which may include expulsion from the University. Refer to the Student Handbook and Student Judicial Code for due process.

• A high standard of academic integrity promotes the pursuit of truth and learning and respect for the intellectual accomplishments of others. These are values that are fundamental to the mission of this University. Such values are undermined by academic dishonesty.

• Academic dishonesty is defined as attempting to obtain academic credit for work that is not one’s own. Examples include:

1. copying another student’s answers on an examination; 2. obtaining, or attempting to obtain, the answers to an examination in advance; 3. submitting a paper that was written by someone else; 4. submitting a paper that includes phrases, sentences and paragraphs that were copied verbatim, or almost

verbatim, from a work written by someone else, without making this clear without indicating that these words were someone else’s through the use of quotation marks or other appropriate citation conventions;

5. collaborating on a homework assignment when this has been expressly forbidden by the professor; 6. using unauthorized materials in completing assignments or examinations; 7. submitting the same paper for more than one class without the express permission of the instructors

involved. This list of examples should not be considered exhaustive.

Page 6: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

UMD Policy on Academic Dishonesty• This definition of academic dishonesty applies to information submitted in other forms besides

paper. Submitting a project of a musical or artistic nature where all or part of the project is someone else’s work, without acknowledging this fact, constitutes academic dishonesty. Submitting computer files that do not represent one’s own work is also considered to be academic dishonesty; examples of computer-based academic dishonesty would include submitting a computer program or text file created by someone else as one’s own, or submitting the output of a computer program written by someone else, and claiming to have written the program that generated the output.

• For all forms of academic dishonesty, students who knowingly allow other students to use their work are themselves considered to be academically dishonest. Examples would include students who knowingly allow other students to copy their exam answers, and students who give papers that they have written to other students so that the other students can submit them for credit.

• A faculty member is appointed by the Faculty Senate to act as an Academic Ethical Matters Facilitator. This individual will offer advice to both students and faculty about the issues involved in penalizing academic dishonesty, and the process of appealing such penalties.

• Penalties assessed by faculty members for academic dishonesty generally consist of a reprimand, a requirement to resubmit the work in a more acceptable form, a lowering of a grade, failure in the course in which the alleged infraction took place, or a combination of these.

• Instead of (or in addition to) assessing such penalties, a faculty member may refer the matter to the UMass Dartmouth Student Judiciary. Decisions made by the Student Judiciary may be appealed to the University Appellate Board.

• A form acknowledging that each student has read, understands, and will abide by these standards will be signed by the student and placed into the student’s permanent file.

Page 7: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Working Together (exams)• Exams are definitely an individual endeavor.

• You may not work together during exams.

• You may not share any materials during an exam.

• In general the penalty for cheating on an exam or otherwise covertly attempting to raise your grade on an exam shall be an ‘F’ for the course.

Page 8: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Canceled Classes and Tests

• If class is cancelled the day an exam is scheduled, we will have the exam the next time the class meets

• If class is cancelled the session prior to the exam (the day you have to ask questions), then the next class meeting will be the “question time”, and the class meeting after that will be the exam

Page 9: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Course Objectives1. To learn how a computer stores data, and conversion between

various number bases2. To learn the fundamentals of using the c compiler and c

preprocessor3. To learn how to use a modern environment to create, compile,

execute and debug C programs4. To learn how to use procedures to modularize a program, and

how to pass parameters by value and by address5. To learn the syntax of the c language, including arrays,

structures, dynamic allocation, and file access6. To learn how to use and manipulate strings using the C language7. To learn to use system libraries within a program 8. To learn how to program, and how to design well written,

maintainable programs

Page 10: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Base ConversionsConvert 123|10 to Base 10 (gotta start someplace)

1 2 3 |10 = ? |10

| | |

| | `--- 3 x 100 = 3 x 1 = 3

| `----- 2 x 101 = 2 x 10 = 20

`------- 1 x 102 = 1 x 100 = 100

123

Therefore, 123|10 = 123|10

Page 11: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Base ConversionsConvert 123|8 to Base 10

1 2 3 |8 = ? |10

| | |

| | `--- 3 x 80 = 3 x 1 = 3

| `----- 2 x 81 = 2 x 8 = 16

`------- 1 x 82 = 1 x 64 = 64

83

Therefore, 123|8 = 83|10

Page 12: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Base ConversionsConvert 2BAD|16 to Base 10

2 B A D |16 = ? |10

| | | |

| | | `--- D x 160 = 13 x 1 = 13

| | `----- A x 161 = 10 x 16 = 160

| `------- B x 162 = 11 x 256 = 2816

`--------- 2 x 163 = 2 x 4096 = 8192

11181

Therefore, 2BAD|16 = 11181|10

Page 13: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Base ConversionsConvert 20DD|16 to Base 10

??

Page 14: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Base ConversionsConvert 20DD|16 to Base 10

2 0 D D |16 = ? |10

| | | |

| | | `--- D x 160 = 13 x 1 = 13

| | `----- D x 161 = 13 x 16 = 208

| `------- 0 x 162 = 0 x 256 = 0

`--------- 2 x 163 = 2 x 4096 = 8192

8413

Therefore, 20DD|16 = 8413|10

Page 15: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Base ConversionsConvert FACE|16 to Base 10

??

Page 16: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Base ConversionsConvert FACE|16 to Base 10

F A C E |16 = ? |10

| | | |

| | | `--- E x 160 = 14 x 1 = 14

| | `----- C x 161 = 12 x 16 = 192

| `------- A x 162 = 10 x 256 = 2560

`--------- F x 163 = 15 x 4096 = 61440

64206

Therefore, FACE|16 = 64206|10

Page 17: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Convert 123|10 to Base 8

8 )123

8 )15 R 3

8 )1 R 7

0 R 1

Therefore, 123|10 = 173|8

Base Conversions

Base you are converting to

Read Up!

Page 18: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Read Up!

Convert 64206|10 to Base 16

16 )64206

16 )4012 R 14 (E)

16 )250 R 12 (C)

16 )15 R 10 (A)

16 )0 R 15 (F)

Therefore, 64206|10 = FACE|16

Base Conversions

Base you are converting to

Page 19: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Step 1: 123|7=?|10

1 2 3

| | |

| | `-3x70= 3

| `--2x71=14

`-----1x72=49

66

123|7=66|10

Step 2: 66|10=?|9

9 )66

9 ) 7 R 3

0 R 7 66|10=73|9

123|7=73|9

Base Conversions123|7 = ?|9 (Can’t do directly)

Page 20: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Base Conversions• Converting between

bases which are powers of 2 (base 2, 4, 8, and 16)

• Each Digit in base 4, 8 or 16 represents 2, 3 or 4 bits

Dec Bin Base 4 Octal Hex

0 0000 00 00 0

1 0001 01 01 1

2 0010 02 02 2

3 0011 03 03 3

4 0100 10 04 4

5 0101 11 05 5

6 0110 12 06 6

7 0111 13 07 7

8 1000 20 10 8

9 1001 21 11 9

10 1010 22 12 A

11 1011 23 13 B

12 1100 30 14 C

13 1101 31 15 D

14 1110 32 16 E

15 1111 33 17 F

Page 21: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

10101110001101|2=?|8

#10 101 110 001 101

2 5 6 1 5

10101110001101|2=25615|8

NOTE: # is a place holder for zero

Base ConversionsDec Bin Bs 4 Oct Hex

0 0000 00 00 0

1 0001 01 01 1

2 0010 02 02 2

3 0011 03 03 3

4 0100 10 04 4

5 0101 11 05 5

6 0110 12 06 6

7 0111 13 07 7

8 1000 20 10 8

9 1001 21 11 9

10 1010 22 12 A

11 1011 23 13 B

12 1100 30 14 C

13 1101 31 15 D

14 1110 32 16 E

15 1111 33 17 F

Work from right to left Divide into 3 bit groups

Page 22: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

10101110001101|2=?|16

##10 1011 1000 1101

2 B 8 D

10101110001101|2=2B8D|16

NOTE: # is a place holder for zero

Base ConversionsDec Bin Bs 4 Oct Hex

0 0000 00 00 0

1 0001 01 01 1

2 0010 02 02 2

3 0011 03 03 3

4 0100 10 04 4

5 0101 11 05 5

6 0110 12 06 6

7 0111 13 07 7

8 1000 20 10 8

9 1001 21 11 9

10 1010 22 12 A

11 1011 23 13 B

12 1100 30 14 C

13 1101 31 15 D

14 1110 32 16 E

15 1111 33 17 F

Work from right to left Divide into 4 bit groups

Page 23: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

1357|8=?|2

1 3 5 7

001 011 101 111

|8=001011101111|2

Base ConversionsDec Bin Bs 4 Oct Hex

0 0000 00 00 0

1 0001 01 01 1

2 0010 02 02 2

3 0011 03 03 3

4 0100 10 04 4

5 0101 11 05 5

6 0110 12 06 6

7 0111 13 07 7

8 1000 20 10 8

9 1001 21 11 9

10 1010 22 12 A

11 1011 23 13 B

12 1100 30 14 C

13 1101 31 15 D

14 1110 32 16 E

15 1111 33 17 F

Note: one need not write the leading zeros

Page 24: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

FACE|16=?|2

F A C E

1111 1010 1100 1110

FACE|16=1111101011001110|2

Base ConversionsDec Bin Bs 4 Oct Hex

0 0000 00 00 0

1 0001 01 01 1

2 0010 02 02 2

3 0011 03 03 3

4 0100 10 04 4

5 0101 11 05 5

6 0110 12 06 6

7 0111 13 07 7

8 1000 20 10 8

9 1001 21 11 9

10 1010 22 12 A

11 1011 23 13 B

12 1100 30 14 C

13 1101 31 15 D

14 1110 32 16 E

15 1111 33 17 F

Page 25: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

A1CE|16=?|8

2 1 C E

1010000111001110

1 2 0 7 1 6

A1CE|16=120716|8

Base ConversionsDec Bin Bs 4 Oct Hex

0 0000 00 00 0

1 0001 01 01 1

2 0010 02 02 2

3 0011 03 03 3

4 0100 10 04 4

5 0101 11 05 5

6 0110 12 06 6

7 0111 13 07 7

8 1000 20 10 8

9 1001 21 11 9

10 1010 22 12 A

11 1011 23 13 B

12 1100 30 14 C

13 1101 31 15 D

14 1110 32 16 E

15 1111 33 17 F

Page 26: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Terminology

• Bit – a 0 or a 1

• Nibble – 4 bits

• Byte – 8 bits

• Word – Machine dependent(8086/88 = 16 bits; 386/486/Pent = 32 bits)

• Doubleword – Machine dependent(8086/88 = 32 bits; 386/486/Pent = 64 bits)

Page 27: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Terminology

• K – Kilo; 210 = 1,024• M – Mega; 220 = 1,048,576• G – Giga; 230 = 1,073,741,824• T – Tera; 240 = 1,099,511,627,776• P – Penta; 250 = 1,125,899,906,842,624• E – Exa; 260 = 1,152,921,504,606,846,976

Page 28: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Rough Cost for RAM/DiskR A M

1956 1978 1985 2000 2004 2005K $19.00 $ 0.16

M $19000. $160.00 $ .50 $ .20

G $500.00 $200.00 80.00

T

D i s kK

M $9999.99 $250 $7.00

G $7000.00 $10.00 $ .83 .10

T $9999.99 $830.00 100.00

See http://www.littletechshoppe.com/ns1625/winchest.html for more info

Page 29: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

ASCII-American Standard Code for Information Interchange

• Used to represent characters

• Seven bit code used to represent character

• ‘0’ through ‘9’ are characters

• IBM used 8 bits in PC and added 128 additional characters (line drawing, Greek)

• Problem with ASCII – the A

• Unicode addresses this problem

Page 30: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

0 00 C-@ 32 20 64 40 @ 96 60 `1 01 C-A 33 21 ! 65 41 A 97 61 a2 02 C-B 34 22 " 66 42 B 98 62 b3 03 C-C 35 23 # 67 43 C 99 63 c4 04 C-D 36 24 $ 68 44 D 100 64 d5 05 C-E 37 25 % 69 45 E 101 65 e6 06 C-F 38 26 & 70 46 F 102 66 f7 07 C-G 39 27 ' 71 47 G 103 67 g8 08 C-H 40 28 ( 72 48 H 104 68 h9 09 C-I 41 29 ) 73 49 I 105 69 i

10 0A C-J 42 2A * 74 4A J 106 6A j11 0B C-K 43 2B + 75 4B K 107 6B k12 0C C-L 44 2C , 76 4C L 108 6C l13 0D C-M 45 2D - 77 4D M 109 6D m14 0E C-N 46 2E . 78 4E N 110 6E n15 0F C-O 47 2F / 79 4F O 111 6F o16 10 C-P 48 30 0 80 50 P 112 70 p17 11 C-Q 49 31 1 81 51 Q 113 71 q18 12 C-R 50 32 2 82 52 R 114 72 r19 13 C-S 51 33 3 83 53 S 115 73 s20 14 C-T 52 34 4 84 54 T 116 74 t21 15 C-U 53 35 5 85 55 U 117 75 u22 16 C-V 54 36 6 86 56 V 118 76 v23 17 C-W 55 37 7 87 57 W 119 77 w24 18 C-X 56 38 8 88 58 X 120 78 x25 19 C-Y 57 39 9 89 59 Y 121 79 y26 1A C-Z 58 3A : 90 5A Z 122 7A z27 1B ESC 59 3B ; 91 5B [ 123 7B {28 1C 60 3C < 92 5C \ 124 7C |29 1D 61 3D = 93 5D ] 125 7D }30 1E 62 3E > 94 5E ^ 126 7E ~31 1F 63 3F ? 95 5F _ 127 7F DEL

ASCII-points to note•Values 30-39=Chars ‘0’-’9’

•‘a’ = ‘A’ + 32 (Bit 5)

•‘A’ = Ctrl/A+64 (bit 6)

•‘A’ < ‘Z’ < ‘a’ < ‘z’

•If character is represented in a byte, bit 7 is usually 0

•Chars 0-31d are not printable, but may influence how other chars are printed.

Page 31: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

0000

000

001

0001

000

011

0010

000

101

0011

000

111

0100

001

001

0101

001

011

0110

001

101

0111

001

111

1000

010

001

1001

010

011

1010

010

101

1011

010

111

1100

011

001

1101

011

011

1110

011

101

1111

011

111

000

001

010

011

100

101

110

111

lower 5 bits

uppe

r 3

bits

Page 32: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Binary Addition

0 + 0 = 0

0 + 1 = 1

1 + 0 = 1

1 + 1 = 0 (with carry of 1)

Page 33: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

1011011 + 10110 = ?

1 0 1 1 0 1 1+ 1 0 1 1 0---------------- 1 1 1 0 0 0 1

Page 34: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

100111001 + 11011 = ?

Page 35: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

100111001 + 11011 = ?

1 0 0 1 1 1 0 0 1+ 1 1 0 1 1 -----------------

Page 36: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

100111001 + 11011 = ?

1 0 0 1 1 1 0 0 1+ 1 1 0 1 1 ----------------- 1 0 1 0 1 0 1 0 0

Page 37: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Hex Addition

4BA978

+ D2B84

58D4FC

• 8+4=C• 7+8=F• 9+B=4 w/ carry• 1+A+2=D• B+D=8 w/ carry• 1+4+0=5

Page 38: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Hex Addition Table

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10

00 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 1001 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 1102 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 1203 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 1304 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 1405 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 1506 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 1607 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 1708 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 1809 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 190A 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A0B 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B0C 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C0D 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D0E 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E0F 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F10 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20

Page 39: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Positive/Negative Numbers

Notes• We are in “4-bit” mode• Think odometer math (-1=1111, -2=1110,

-3=1101, -4=1100, -5=1011, -6=1010, etc.)• At some point a big positive number+1

becomes a big negative number• Really a number “circle”

00000001

00100011

010001011011

1100 111011111101

0 1 2 3 4 5-5 -4 -2 -1-3

Page 40: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

4-bit Number Circle0000

0001

0010

0011

0100

01011011

1100

1110

1111

1101

1010

1001 1000 0111

0110

01

2

3

4

5-5

-4

-2

-1

-3

67

-8-7

-6

Page 41: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Observations (Part 1)

0110 6+ 0011 + 31001 9

1010 -61101 +-3

10001 9

0100 40010 + 20110 6

1100 -41110 +-2

11010 -6

0110 61101 +-3

10011 3

1010 -60011 + 31101 -3

Page 42: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Observations (Part 2)

0110 6+ 0011 + 31001 9

1010 -61101 +-3

10001 9

0100 40010 + 20110 6

1100 -41110 +-2

11010 -6

0110 61101 +-3

10011 3

1010 -60011 + 31101 -3

Page 43: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Observations (Part 3) & Conclusions• Note when a carry into the sign bit occurs

• Note when a carry out of the sign bit occurs

• If one occurs and not the other – WRONG

• If both occur – RIGHT

• If neither occur – RIGHT

• Therefore:

OF = carry into sign bit carry out of sign bit

Page 44: ECE 160 – Comp Programming I Data Structures with C Prof. P. H. Viall Office: Research 217 Tel: (508) 999-8240 Web: pviall/ece160.

Organization of a computer

CPU RAM ROM Printer Disk CRT Kbd

Address Bus

Data Bus

Control Bus