Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University.

Slides:



Advertisements
Similar presentations
COMP 2130 Intro Computer Systems Thompson Rivers University
Advertisements

Floating Point Numbers
Computer ArchitectureFall 2007 © September 5, 2007 Karem Sakallah CS 447 – Computer Architecture.
Assembly Language and Computer Architecture Using C++ and Java
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.

Introduction to Programming with Java, for Beginners
Assembly Language and Computer Architecture Using C++ and Java
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems.
Floating Point Numbers.  Floating point numbers are real numbers.  In Java, this just means any numbers that aren’t integers (whole numbers)  For example…
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.
Mantıksal Tasarım – BBM231 M. Önder Efe
Number Systems Lecture 02.
Binary Number Systems.
Topic 4 Computer Mathematics and Logic
Introduction to Computing Systems from bits & gates to C & beyond Chapter 2 Bits, Data Types & Operations Integer Representation Floating-point Representation.
Data Representation Number Systems.
(2.1) Fundamentals  Terms for magnitudes – logarithms and logarithmic graphs  Digital representations – Binary numbers – Text – Analog information 
© 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
ES 244: Digital Logic Design Chapter 1 Chapter 1: Introduction Uchechukwu Ofoegbu Temple University.
ACOE1611 Data Representation and Numbering Systems Dr. Costas Kyriacou and Dr. Konstantinos Tatas.
Number Representations and Computer Arithmetic. CS 21a 9/23/02 Odds and Ends Slide 2 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University.
Computer Arithmetic Nizamettin AYDIN
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
Logic and Digital System Design - CS 303
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Idea 1: Bits Two states in a digital machine: 1.presence of.
Number Systems Spring Semester 2013Programming and Data Structure1.
Binary, Decimal and Hexadecimal Numbers Svetlin Nakov Telerik Corporation
Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
Information Representation. Digital Hardware Systems Digital Systems Digital vs. Analog Waveforms Analog: values vary over a broad range continuously.
PART 2 Data Types 1. Integers 2. 2’s Complement 3. Conversion
9.4 FLOATING-POINT REPRESENTATION
Bits, Data Types, and Operations Slides based on set prepared by Gregory T. Byrd, North Carolina State University.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
CSC 221 Computer Organization and Assembly Language
AEEE2031 Data Representation and Numbering Systems.
Instructor:Po-Yu Kuo 教師:郭柏佑
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
Dr Mohamed Menacer College of Computer Science and Engineering Taibah University CE-321: Computer.
1 Digital Logic Design Lecture 2 More Number Systems/Complements.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
BINARY SYSTEMS ENGR. KASHIF SHAHZAD 1. BINARY NUMBERS 1/2 Internally, information in digital systems is of binary form groups of bits (i.e. binary numbers)
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.
Data Representation Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Chapter 9 Computer Arithmetic
William Stallings Computer Organization and Architecture 8th Edition
CS2100 Computer Organisation
Data Representation Binary Numbers Binary Addition
Integer Real Numbers Character Boolean Memory Address CPU Data Types
William Stallings Computer Organization and Architecture 7th Edition
Data Structures Mohammed Thajeel To the second year students
ECEG-3202 Computer Architecture and Organization
Chapter 8 Computer Arithmetic
Presentation transcript:

Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University

TRU-COMP1380 Number Systems2 Course Contents Speaking Mathematically Number Systems and Computer Arithmetic Logic and Truth Tables Boolean Algebra and Logic Gates Vectors and Matrices Sets and Counting Probability Theory and Distributions Statics and Random Variables

TRU-COMP1380 Number Systems3 Unit Learning Objectives Convert a decimal number to binary number. Convert a decimal number to hexadecimal number. Convert a binary number to decimal number. Convert a binary number to hexadecimal number. Convert a hexadecimal number to binary number. Add two binary numbers. Compute the 1’s complement of a binary number. Compute the 2’s complement of a binary number. Understand the 2’s complement representation for negative integers. Subtract a binary number by using the 2’s complement addition. Multiply two binary numbers. Use of left shift and right shift. Binary division

TRU-COMP1380 Number Systems4 Unit Contents The number systems: The decimal system The binary system The hexadecimal system Computer Arithmetic: Binary addition Representation of negative integers Binary multiplication Binary division Representation of fractions

TRU-COMP1380 Number Systems5 1. Number Systems

The Decimal System Uses 10 digits 0, 1, 2,..., 9. Decimal expansion: 83 = 8× = 4× × × × = ??? = ??? Do you know addition, subtraction, multiplication and division? – × / TRU-COMP1380 Number Systems6

The Binary System In computer systems, the most basic memory unit is a bit that contains 0 or 1. The data unit of 8 bits is referred as a byte that is the basic memory unit used in main memories and hard disks. All data are represented by using binary numbers. Data types such as text, voice, image and video have no meaning in the data representation. 8 bits are usually used to express English alphabets. A collection of n bits has 2 n possible states(, i.e., numbers). Is it true? E.g., How many different numbers can you express using 2 bits? How many different numbers can you express using 4 bits? How many different numbers can you express using 8 bits? How many different numbers can you express using 32 bits? TRU-COMP1380 Number Systems7

How can we store integers (i.e., positive numbers only) in a computer? E.g., A decimal number 329? Is it okay to store 3 chracters ‘3’, ‘2’, and ‘9’ for 329? How do we store characters? = ??? 2 TRU-COMP1380 Number Systems8

Uses two digits 0 and = 0×2 0 = = 1×2 0 = = 1× ×2 0 = = 1× ×2 0 = = 1× × ×2 0 = = 1× × ×2 0 = = 1× × ×2 0 = = 1× × ×2 0 = = 1× × × ×2 0 = = 1× × × ×2 0 = TRU-COMP1380 Number Systems9

Powers of = 1×2 0 = = 1× ×2 0 = = 1× × ×2 0 = = 1× × × ×2 0 = = 2 4 = = 2 5 = = 2 6 = TRU-COMP1380 Number Systems10

= 2 7 = ??? = 2 8 = ??? = 2 9 = ??? = 2 10 = ??? = 2 11 = ??? = 2 12 = ??? 10 Can you memorize the above powers of 2? Converting to decimals = ??? = ??? = ??? 10 TRU-COMP1380 Number Systems11

Converting Decimal to Binary QuotientRemainder 23 / / / / / 201 => = = ??? = ??? 2 TRU-COMP1380 Number Systems12

Another similar idea = ??? = = = = => 271 = = = = = ??? 2 TRU-COMP1380 Number Systems13

Hexadecimal Number System 0 10 = = 0 16 = 0x = = 1 16 = 0x = = 2 16 = 0x = = 3 16 = 0x = = 4 16 = 0x = = 5 16 = 0x = = 6 16 = 0x = = 7 16 = 0x = = 8 16 = 0x = = 9 16 = 0x = = A 16 = 0xA = = B 16 = 0xB = = C 16 = 0xC = = D 16 = 0xD = = E 16 = 0xE = = F 16 = 0xF 0x23c9d6ef = ??? 10 0x23c9d6ef = ??? = ??? = ??? 2 TRU-COMP1380 Number Systems14 4 bits can be used for a hexadecimal number, 0,..., F. Please memorize it!

Converting Decimal to Hexadecimal QuotientRemainder 328 / 16 = 20 × / 16 = 1 × / 16 = 0 × => = = ??? = (1 × × × 16 0 ) = ??? 16 TRU-COMP1380 Number Systems15

Converting Binary to Hexadecimal 4DA9 16 = ??? = ??? 16 = = 4DA = 0x??? = ??? = 0x??? = ??? 10 TRU-COMP1380 Number Systems16

2. Computer Arithmetic TRU-COMP1380 Number Systems17

Binary Addition How to add two binary numbers? Let’s consider only unsigned integers (i.e., zero or positive numbers only) for a while. Just like the addition of two decimal numbers. E.g., ??? ??? carry TRU-COMP1380 Number Systems18

Binary Subtraction How to subtract a binary number? Just like the subtraction of decimal numbers. E.g., ?1 ? Try: How to do? 1 34–79=? Is subtraction easier or more difficult than addition? Why? TRU-COMP1380 Number Systems19

In the previous slide, – 11 = 1111 What if we add Is there any relationship between 11 2 and ? The 8-bit 1’s complement of 11 2 is ???Switching 0  1 This type of addition is called 1’s complement addition. Find the 8-bit one’s complements of the followings > > 10 -> > 101 -> > TRU-COMP1380 Number Systems20

In the previous slide, – 11 = 1111 What if we add Is there any relationship between 11 and ? The 8-bit 2’s complement of 11 is ??? 2’s complement ≡ 1’s complement + 1 -> = This type of addition is called 2’s complement addition. Find the 16-bit two’s complements of the followings > > TRU-COMP1380 Number Systems21

Another example ??? What if we use 1’s complement addition or 2’s complement addition instead as followings? Let’s use 8-bit representation What does this mean? A – B = A + (–B), where A and B are positive Is –B equal to the 1’s complement or the 2’s complement of B? TRU-COMP1380 Number Systems22 1’s complement addition 2’s complement addition

Can we use 8-bit 1’s complement addition for 1 2 – 10 2 = –1 2 ? <- 8-bit 1’s complement of <- Is this correct? (Is this 1’s complement of 1?) Let’s use 8-bit 2’s complement addition for 1 2 – <- 2’s complement of <- Correct? (2’s complement of 1?) 1 2 – 10 2 = (–10 2 ) Subtraction can be converted to addition with negative numbers. Then, how to represent negative binary numbers, i.e., signed integers? TRU-COMP1380 Number Systems23

Representation of Negative Binaries Representation of signed integers 8 or 16 or 32 bits are usually used for integers. Let’s use 8 bits for examples. The left most bit (called most significant bit) is used as sign. When the MSB is 0, positive integers. When the MSB is 1, negative integers. The other 7 bits are used for integers. What signed integers can be described using the 8 bit representation? How to represent positive integer 5? How about -9? is really okay? (9) (-9) = (-18) It is wrong! We need a different representation for negative integers. TRU-COMP1380 Number Systems24

How about -9? is really okay? (9) (-9) = (-18) It is wrong! We need a different representation for negative integers. What is the 8-bit 1’s complement of 9? <- 8-bit 1’s complement of < bit 1’s complement of 9 = <- Is it zero? (1’s complement of 0?) What is the 2’s complement of 9? <- 8-bit 2’s complement of < bit 2’s complement of 9 = <- It looks more like zero. 2’s complement representation is used for negative integers. TRU-COMP1380 Number Systems25

1 2 – 10 2 = (–10 2 ) ??? <- 2’s complement of 10, i.e., <- 2’s complement of 1, i.e., -1 (= 1 – 2) – = (– ) ??? – 11 2 ??? 10 2 – ??? –10 2 – 1 2 ??? Is the two’s complement of the two’s complement of an integer the same integer? What is x when the 8-bit 2’s complement of x is TRU-COMP1380 Number Systems26

8-bit representation with 2’s complement (2 8 – 1) The maximum number is ? byte y = 125; y += 4; ?? The minimum number is ? byte x = -126; x -= 5; ?? What if we add the maximum number by 1 ??? What if we subtract the minimum number by 1 ??? TRU-COMP1380 Number Systems27 +1 overflow

16-bit representation with 2’s complement The maximum number is ? What if we add the maximum number by 1 ??? The minimum number is ? What if we subtract the minimum number by 1 ??? TRU-COMP1380 Number Systems28 +1 overflow +1 short y = 32767; y++; ??? short x = ; x--; ???

Note that computers use the 8-bit representation, the 16-bit representation, the 32-bit representation and the 64-bit representation with 2’complement for negative integers. In programming lanaguages byte,unsigned byte8-bit short,unsigned short16-bit int,unsigned int32-bit long,unsigned long64-bit When we use the 32-bit representation with 2’s complement, The maximum number is ? What if we add the maximum number by 1 ??? The minimum number is ? What if we subtract the minimum number by 1 ??? TRU-COMP1380 Number Systems29

How to convert a negative binary number to decimal? An example of 8-bit representation, = ??? TRU-COMP1380 Number Systems30

Multiplication of Binary Numbers How to multiply two decimal numbers? E.g., × 1 = ??? × 10 = ??? × 100 = ??? × 101 = × 10 × × × 1 What if we shift left by one bit? > What if we shift left by two bits? > Multiplication by a power of 2. TRU-COMP1380 Number Systems31

Division of Binary Numbers Binary division? 1111 <- quotient <- remainder Try / 110 Dividing negative binary numbers: division without sign, and then put the sign. TRU-COMP1380 Number Systems32

Binary division by a power of 2? / 10 = / 100 = / 1000 = 1001 What if we shift right by 1 bit? What if we shift right by 2 bits? What if we shift right by 3 bits? / 101 ??? Complicated implementation required TRU-COMP1380 Number Systems33

Fractions: Fixed-Point How can we represent fractions? Use “binary point” to separate positive from negative powers of two -- like “decimal point.” 2’s complement addition and subtraction still work. (Assuming binary points are aligned) TRU-COMP1380 Number Systems (40.625) (-1.25) (2’s complement) (39.375) 2 -1 = = = No new operations -- same as integer arithmetic.

How to convert decimal fractions to binary? = ??? * 2 = > 1(0.625 = x y z ) 0.25 * 2 = 0.5 -> 0(1.25 = x + y z => x = 1) 0.5 * 2 = 1.0 -> 1(0.25 = y z ) Therefore = ??? * 2 = 1.4 -> * 2 = 0.8 -> * 2 = 1.6 -> * 2 = 1.2 -> * 2 = 0.4 -> * 2 = 0.8 -> * 2 = 1.6 -> 1... Therefore TRU-COMP1380 Number Systems35 How to deal with big numbers and small numbers?

Very Large and Very Small: Floating-Point Large values: × requires 79 bits Small values: × requires > 110 bits How to handle those big/small numbers? Use equivalent of “scientific notation”: F × 2 E Need to represent F (fraction), E (exponent), and sign × = × × = × ( ) = × 2 6. Store 6 in the exponent and in mantissa. (Try the multiplication) IEEE 754 Floating-Point Standard (32-bits): TRU-COMP1380 Number Systems36 SExponentFraction (mantissa) 1b 8 bits 23 bits