Topic 4 Computer Mathematics and Logic

Slides:



Advertisements
Similar presentations
Number Bases Informatics INFO I101 February 9, 2004 John C. Paolillo, Instructor.
Advertisements

The Binary Numbering Systems
Boolean Algebra. Starter Task State 5 basic data types together with one operation that can be performed on each. We have looked at 4 in the past few.
Digital Fundamentals Floyd Chapter 2 Tenth Edition
Assembly Language and Computer Architecture Using C++ and Java
Number Systems Standard positional representation of numbers:
Review Two’s complement
Introduction to Programming with Java, for Beginners
Assembly Language and Computer Architecture Using C++ and Java
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University See: P&H Chapter 2.4, 3.2, B.2, B.5, B.6.
Number Systems Lecture 02.
Binary Numbers 1.Binary and Denary RecapBinary.
COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter.
Operations on data CHAPTER 4.
4 Operations On Data Foundations of Computer Science ã Cengage Learning.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Data Representation Number Systems.
© 2009 Pearson Education, Upper Saddle River, NJ All Rights ReservedFloyd, Digital Fundamentals, 10 th ed Digital Fundamentals Tenth Edition Floyd.
Simple Data Type Representation and conversion of numbers
Lecture for Week Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday.
Numbering systems.
Copyright © Cengage Learning. All rights reserved. CHAPTER 2 THE LOGIC OF COMPOUND STATEMENTS THE LOGIC OF COMPOUND STATEMENTS.
Number Systems Binary and Hexadecimal. Base 2 a.k.a. Binary  Binary works off of base of 2 instead of a base 10 like what we are taught in school 
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
Logic and data representation Revision. AND gate A B A B All inputs have to be true ( i.e 1) for the output of the gate to be high, in all other cases.
Computer Organization and Architecture Computer Arithmetic Chapter 9.
Computer Arithmetic Nizamettin AYDIN
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Today’s Topics How information.
Basic Arithmetic (adding and subtracting)
IT253: Computer Organization
Lecture Overview Introduction Positional Numbering System
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Engineering 1040: Mechanisms & Electric Circuits Spring 2014 Number Systems.
IT101: INFORMATION TECHNOLOGY FUNDAMENTALS 1 Edited By Dr. Ahmed Abo-Bakr Information Technology Dept. Faculty of Computers & Information.
1 DLD Lecture 18 Recap. 2 Recap °Number System/Inter-conversion, Complements °Boolean Algebra °More Logic Functions: NAND, NOR, XOR °Minimization with.
Binary Numbers. Why Binary? Maximal distinction among values  minimal corruption from noise Imagine taking the same physical attribute of a circuit,
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
Number Systems and Logic Prepared by Dr P Marais (Modified by D Burford)
How a Computer Processes Information. Java – Numbering Systems OBJECTIVE - Introduction to Numbering Systems and their relation to Computer Problems Review.
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
CSC 370 (Blum)1 Binary Numbers Material on Data Representation can be found in Chapter 2 of Computer Architecture (Nicholas Carter)
Kavita Bala CS 3410, Spring 2014 Computer Science Cornell University.
Computer Science Introduction to the Number Base Unit Adapted from Slides by John Owen Computer Science Instructor, Rockport-Fulton High School, Rockport,
Extending Binary In today’s lesson we will look at: representing different types of numbers possible errors binary coded decimal (BCD) comparing BCD with.
Digital Fundamentals Tenth Edition Floyd Chapter 2 © 2008 Pearson Education.
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
Binary Arithmetic.
1 Digital Logic Design Lecture 2 More Number Systems/Complements.
Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
1 Ethics of Computing MONT 113G, Spring 2012 Session 4 Binary Addition.
PHY 201 (Blum)1 Shift registers and Floating Point Numbers Chapter 11 in Tokheim.
09/03/20161 Information Representation Two’s Complement & Binary Arithmetic.
Computing Systems Lecture 3 Binary Representation & Boolean Logic Binary and Logic 1.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
Floating Point Numbers
Department of Computer Science Georgia State University
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
A Level Computing Component 2
Binary Numbers Material on Data Representation can be found in Chapter 2 of Computer Architecture (Nicholas Carter) CSC 370 (Blum)
Storing Negative Integers
Copyright © Cengage Learning. All rights reserved.
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Presentation transcript:

Topic 4 Computer Mathematics and Logic IB Computer Science

A reminder of how we count… 103 102 101 100 10-1 10-2 9 2  4  6  3  5 9  1000 = 9000 2 100 200 4 10 40 6 1 3 0.1 0.3 5 0.01 0.05 Total 9346.35 This is how we represent the number 9246.35. We have a 1000s column, a 100s column, etc. All the columns are powers of 10. We count the number of 1000s, 100s, 10s, etc, and sum them up.

You can do the same with powers of 2 23 22 21 20 2-1 2-2 Or powers of 3 33 32 31 30 3-1 3-2 Or any base you like… n3 n2 n1 n0 n-1 n-2

We only count in base 10 because we have ten fingers The Simpsons count in base 8.

In computing We often count in: base 2 (binary) base 8 (octal) base 16 (hexadecimal) You need to know: binary hexadecimal 23 22 21 20 2-1 2-2 1 163 162 161 160 16-1 16-2 F

Counting in other bases Counting in base 10 9 is the biggest number Then we change columns 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 etc 1 2 3 4 5 6 7 8 9 10 11 12 13 etc Counting in base 2 1 is the biggest number Then we change columns

Counting in other bases Counting in base 10 9 is the biggest number Then we change columns 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 etc 1 2 3 4 5 6 7 8 9 10 11 12 13 etc Counting in base 2 1 is the biggest number Then we change columns

Counting in hex 1 2 3 4 5 6 7 8 9 A B C D E F 1 0 Counting in base 16 means that 15 is the biggest number we get to before we change columns Counting in base 16 presents a problem because we don't have fifteen different digits to make our numbers out of! So we use letters, as shown… Don't forget that 10 doesn't mean "ten" if we are counting in hex. It means "one in the sixteens column and nothing in the units column".

Converting between decimal and binary Convert 45dec to binary What is the biggest power of 2 in 45? Answer 32, so your first 1 represents 1 x 32, giving 1 Next, go down the powers of 2 and add on what you need to make 45. Do we need any 16s? No, because we already have a 32 and 16 would give us 48, so our next number is 0, which represents 0 x 16, giving 10 Do we need any 8s? Yes, because 32 + 8 is only 40, so our next number is 1, which represents 1 x 8, giving 101 Do we need any 4s? Yes, because 32 + 8 + 4 is only 44, so our next number is 1, which represents 1 x 4, giving 1011 Do we need any 2s? No, because adding 2 to our 44 would give us 46, so our next number is 0, which represents 0 x 2, giving 10110 Do we need any 1s? Yes, because we currently have 44 and we need 1 more to give us 45. So our last number is 1, representing 1 x 1 and giving 101101. So 45dec in binary is 101101. We will check this result on the next page.

Check 45dec in binary is 101101 Check: 1  32 = 16 8 4 2 Total 45

Convert the following to binary 3dec 7dec 12dec 16dec 20dec 31dec 53dec 64dec 127dec 11 111 1100 10000 10100 11111 110101 1000000 1111111

Convert the following to decimal 10bin 101bin 1101bin 10010bin 11001bin 101010bin 111111bin 10000000000bin 1111111111111bin 2 5 13 18 25 42 63 1024 8191

Converting between decimal and hex Convert 81dec to hex Hex powers go 1, 16, 256, 4096, etc. They get big quite quickly. What is the biggest power of 16 in 81dec? Answer 16. How many sixteens are there in 81dec? Answer 5, with one left over. So our answer is 51, ie 5 sixteens, plus 1 unit. We will check this result on the next page.

Check 81dec in hex is 51 Check: 5  16 = 80 1 Total 81

Convert the following to hex 4dec 11dec 14dec 23dec 31dec 45dec 66dec 240dec 301dec 4 B E 17 1F 2C 42 F0 11D

Convert the following to decimal 5 C 10 16 2A 32 FF 100 1AC 5 12 16 22 42 50 255 256 428

The good news… Bin Hex 0000 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 Converting directly between binary and hex is easier than between binary and decimal, or hex and decimal. You just need to be able to re-create this table in your head.

3 8 B 5 Convert A4Fhex to binary Convert 11100010110101bin to hex 1 1 1 So 34Fhex = 001101001111bin Notice leading zeroes in answer are not required, but you won't lose marks if you leave them in. Convert 11100010110101bin to hex 1 Notice padding with zeroes enables use of conversion table on previous slide. 3 8 B 5 So 11100010110101bin is 38B5hex

Convert to binary 9 D 13 A1 7CD 1001 1101 00010011 10100001 011111001101

How many values can n bits represent? Imagine you have 8 bits to play with The lowest value is 00000000 The highest value is 11111111 That’s 0 up to 255, ie 256 different values 256 is 28 In general, n bits can represent 2n different values

Negative numbers All numbers in the computer are represented in binary, but how are negative numbers represented? (We have no "minus sign" in computer memory!) Answer: the two’s complement convention: Everything is as normal, but the most significant bit (MSB) is taken to be negative. -28 27 26 25 24 23 22 21 20

Practice with two’s complement Here we are using 8-bit two’s complement Check you understand: 00000001 = 20 = 1 01000000 = 26 = 128 01000001 = 26 + 20 = 64 + 1 = 65 10000000 = -27 = -128 10000001 = -27 + 20 = -128 + 1 = -127 -27 26 25 24 23 22 21 20

Range of values What is the highest value that can be expressed in 8-bit two’s complement? Well, you want all the positive values, ie 0111111, which is 26 + 25 + 24 + 23 + 22 + 21 + 20 = 127 What is the lowest value that can be expressed in 8-bit two’s complement? Well, you want the negative bit, and NONE of the positive bits, ie 10000000, which is -27 = -128 What is the highest value that can be expressed in n-bit two’s complement? 2n-1-1 What is the lowest value that can be expressed in n-bit two’s complement? -2n-1

Range of values 3 23 22-1 -22 4 24 23-1 -23 5 25 24-1 -24 6 26 25-1 bits no diff values highest lowest 3 23 22-1 -22 4 24 23-1 -23 5 25 24-1 -24 6 26 25-1 -25 7 27 26-1 -26 8 28 27-1 -27 9 29 28-1 -28 n 2n 2n-1-1 -2n-1

Expressing fractions 1: Fixed-Point Binary 23 22 21 20 2-1 2-2 2-3 Bits beyond the “bicimal” point are negative powers of 2, ie 1 2 , 1 4 , 1 8 etc The bicimal point doesn’t move, hence “fixed point”.

Fixed point binary practice Convert to binary: 1.5dec 2.5dec 4.25dec 7.625dec Answers: 1.1 10.1 100.01 111.101 Convert to decimal: 1.01bin 101.1bin 110.011bin 1000.11bin Answers: 1.25 5.5 6.375 8.75

Combining Fixed Point and Two's Complement -23 22 21 20 2-1 2-2 2-3 Convert to binary: -4.75dec Answer: 1011.01 (-8 + 2 + 1 + 0.25) Convert to decimal: 1001.001bin Answer: -6.875 (-8 + 1 + 0.125) What is the highest value that can be expressed in this format? What is the lowest values? Answers: 111.111 = 7.875 and 1000.000 = -8

Floating Point Remember scientific notation in maths? 123.4 becomes 1.234 x 102 right? The same happens in binary. e.g. 110.1 can be written as 1.101 x 22 This is called floating point representation, because the decimal point moves. Test your understanding. Convert to the following to floating point representation: 10.1 1100.1 0.00011 Answers: 1.01 x 21 1.1001 x 23 1.1 x 2-4

Storing floating point numbers 1.011 x 23 exponent mantissa There are two more things to think about: How do we store the exponent? (We can't store a 3 in binary) How do we represent negative numbers? We actually store both the mantissa and the exponent in two's complement form. Note the position of the bicimal point. -20 2-1 2-2 2-3 2-4 2-5 2-6 2-7 -24 23 22 21 20

Floating point example: positive mantissa Positive mantissas are relatively easy: Convert to decimal the floating-point binary number 010011 0100, if 6 bits are allocated to the mantissa and 4 bits to the exponent. [2 marks] Remember that bicimal point is after the first bit of the mantissa (ie 0.10011) Now calculate the exponent. 0100 is (positive) 4, so we shift the bicimal point 4 places to the right, giving 1001.1 1001.1 = 8 + 1 + ½ so the final answer is 9.5 (With a negative exponent, you would just need to shift the bicimal point to the left instead.) -20 2-1 2-2 2-3 2-4 2-5 -23 22 21 20 1

Floating point example: negative mantissa Recall that we use two's complement because we have no minus sign in the computer's memory. Well, when you are dealing with a negative mantissa, it is much easier to imagine that you HAVE got a minus sign. For instance, if you are using 6-bit two's complement, you can consider a mantissa of 1.10000 (-1 + ½ = -½) as -0.10000 (-½) To easily convert from two's complement to our cheating minus sign format is straightforward: Flip all the bits Add 1 to the least significant bit Put a minus sign in front To convert back you just do the same thing again and remove the minus sign – it works both ways

Practice complementing What is 10110 (5-bit two's complement) in our cheating minus sign format? Flip the bits, giving 01001 Add one to the least significant bit (the right-most) giving: 01010 Put a minus sign in front: - 01010 Check: 10110 (two's complement) is -1 + 0.25 + 0.125 = -0.675 -01010 is –(0.5 + 0.125) = -0.675 Check that the same procedure takes you back to the two's complement format.

Negative mantissa revisited It's important to know how to complement the mantissa because it makes moving the bicimal point much more straightforward. Convert to decimal the floating-point binary number 11011 0011, if 6 bits are allocated to the mantissa and 4 bits to the exponent. [2 marks] First find the cheating format of the mantissa: 1.1011  0.0100  -0.0101 (Now it's just like with a positive mantissa a few slides back) Find the exponent: 0011 = 3 Move the bicimal point 3 to the right, giving -10.1 Convert to decimal. Answer -2.5

Converting from decimal to floating point This is just like converting to scientific notation: Convert 126.25 to scientific notation. Move decimal point two to the left 126.25  1.2625 Add an exponent to compensate 1.2625 x 102 Convert 5.75 to normalized floating point binary if the mantissa is 6 bits and the exponent is 4 bits: Convert to fixed point binary: 101.11 Move bicimal point 3 to the left: 0.10111 (must have a zero outside the bicimal point in case you have a negative mantissa and you need to complement – see next example) Set the 4-bit exponent to 3 to compensate: 0011 Full answer: 010111 0011

More examples Here's how you handle a negative mantissa: Convert -3.625 to normalized floating point binary if the mantissa is 6 bits and the exponent is 4 bits: Convert to 6-bit fixed point binary with minus sign: -011.101 Move bicimal point 2 to the left: -0.11101 Set the 4-bit exponent to 2 to compensate: 0010 Complement the mantissa: -0.11101  100010  100011 Full answer: 100011 0010 And finally, a negative mantissa and a negative exponent: Convert − 3 16 to normalized floating point binary if the mantissa is 6 bits and the exponent is 4 bits: Convert to 6-bit fixed point binary with minus sign: -0.00110 Move bicimal point 2 to the right: -0.11000 Set the 4-bit exponent to -2 to compensate: -0010 Complement the mantissa: -0.00110  111001  111010 Complement the exponent: -0010  1101  1110 Full answer: 111010 1110

Important points Students often find this hard. There will probably be relatively few marks dedicated to the advanced aspects of this in the exam. Don't spend a disproportionate amount of time on it. If you don't like my explanation, try this guy's: http://www.ib-computing.net/html/program/topic_4/floating_point.html

Possible errors Learn the definitions plus an example for each Truncation error: Some numbers require infinite-length mantissas, e.g. one third is 0.0101010101… If you try to store this in a computer then some of the digits get "chopped off" (truncated), with an associated loss of precision. Overflow error: If you have 3 bits, you can't do the sum 101+ 011 because the answer is 1000, which bigger than the biggest number you can represent. Underflow error: If you have 4-bit two's complement then the smallest number you can represent is 1000 (or -8). Therefore you can't so the sum -4 - 5, because the answer is 11111 (or -9) which is smaller than the smallest number you can represent. Learn the definitions plus an example for each

Truth tables A B A nand B 1 AND A B A  B 1 NAND NOT A 1 XOR A B A  B 1 AND A B A  B 1 NAND NOT A 1 XOR A B A  B 1 OR A B A + B 1 NOR A B A nor B 1

Boolean algebra into words and vice versa (A) Jessica will go to the party (B) Fred will go to the party (C) Chen will be happy Construct an expression using Boolean algebra for the sentence "Either of Jessica or Fred will go to the party, and Chen will be unhappy." (A  B)  C (A xor B) and not C You need to know the symbols:  AND + OR  XOR [overbar] NOT

Logic circuits You can construct logic circuits out of boolean algebra and vice versa. This circuit corresponds with the statement on the last slide. It will only produce an output of true if (A xor B) and not C. A inputs B output C Nand is equivalent to AND followed by NOT (the AND truth table with all the bits flipped) and so has the sense of "anything except both". Nor is equivalent to OR followed by NOT (the OR truth table with all the bits flipped) and so has the sense of "neither one nor the other".

You should… Be able to convert between boolean algebra (words or symbols) and logic circuits (maximum of three inputs) Show that a logic circuit and a boolean algebraic expression are equivalent to each other by comparing their truth tables

(Not A And B) Or (Not(Not A Or (Not A Or B))) (Not A And B) Or (Not(Not A Or B)) (Not A And B) Or (A And Not B)) A xor B Karnaugh maps sum of products