Numbers in Computers.

Slides:



Advertisements
Similar presentations
Fixed Point Numbers The binary integer arithmetic you are used to is known by the more general term of Fixed Point arithmetic. Fixed Point means that we.
Advertisements

2-1 Chapter 2 - Data Representation Computer Architecture and Organization by M. Murdocca and V. Heuring © 2007 M. Murdocca and V. Heuring Computer Architecture.
Chapter 2: Data Representation
Binary Arithmetic Binary addition Binary subtraction
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3:
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3: IT Students.
1 IEEE Floating Point Revision Guide for Phase Test Week 5.
Chapter 3 Arithmetic for Computers. Multiplication More complicated than addition accomplished via shifting and addition More time and more area Let's.
Assembly Language and Computer Architecture Using C++ and Java
Number Systems Standard positional representation of numbers:
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
1  2004 Morgan Kaufmann Publishers Chapter Three.
Signed Numbers.
Assembly Language and Computer Architecture Using C++ and Java
CSE 378 Floating-point1 How to represent real numbers In decimal scientific notation –sign –fraction –base (i.e., 10) to some power Most of the time, usual.
1 Module 2: Floating-Point Representation. 2 Floating Point Numbers ■ Significant x base exponent ■ Example:
Floating Point Numbers
S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems.
Computer ArchitectureFall 2008 © August 27, CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)
Computer Science 210 Computer Organization Floating Point Representation.
Binary Number Systems.
School of Computer Science G51CSA 1 Computer Arithmetic.
Chapter 3 Data Representation part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Binary Representation and Computer Arithmetic
Dr. Bernard Chen Ph.D. University of Central Arkansas
Chapter3 Fixed Point Representation Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.
The Binary Number System
Data Representation Number Systems.
Simple Data Type Representation and conversion of numbers
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
Computer Arithmetic Nizamettin AYDIN
2-1 Chapter 2 - Data Representation Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Chapter Contents.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
IT253: Computer Organization
Computing Systems Basic arithmetic for computers.
ECE232: Hardware Organization and Design
Floating Point. Agenda  History  Basic Terms  General representation of floating point  Constructing a simple floating point representation  Floating.
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.
Number Representation. 10/12/2015CDA31002 Conversion between Representations Now we can represent a quantity in different number representations How can.
9.4 FLOATING-POINT REPRESENTATION
Fixed and Floating Point Numbers Lesson 3 Ioan Despi.
CSC 221 Computer Organization and Assembly Language
COMP201 Computer Systems Floating Point Numbers. Floating Point Numbers  Representations considered so far have a limited range dependent on the number.
Conversion to Larger Number of Bits Ex: Immediate Field (signed 16 bit) to 32 bit Positive numbers have implied 0’s to the left. So, put 16 bit number.
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3:
CSC 4250 Computer Architectures September 5, 2006 Appendix H. Computer Arithmetic.
Computer Arithmetic Floating Point. We need a way to represent –numbers with fractions, e.g., –very small numbers, e.g., –very large.
Floating Point Numbers Representation, Operations, and Accuracy CS223 Digital Design.
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
CDA 3100 Spring Special Thanks Thanks to Dr. Xiuwen Liu for letting me use his class slides and other materials as a base for this course.
10/7/2004Comp 120 Fall October 7 Read 5.1 through 5.3 Register! Questions? Chapter 4 – Floating Point.
Fixed-point and floating-point numbers Ellen Spertus MCS 111 October 4, 2001.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
CDA 3100 Fall Special Thanks Thanks to Dr. Xiuwen Liu for letting me use his class slides and other materials as a base for this course.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Floating Point Representations
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
Data Representation Binary Numbers Binary Addition
Number Representation
Number Representations
Data Representation Data Types Complements Fixed Point Representation
How to represent real numbers
How to represent real numbers
CDA 3100 Fall 2012.
Review In last lecture, done with unsigned and signed number representation. Introduced how to represent real numbers in float format.
Chapter3 Fixed Point Representation
Number Representations
Presentation transcript:

Numbers in Computers

Review Last lecture, we saw How to convert a number in decimal into a number in binary Keep on dividing it by 2 until the quotient is 0. Then write down the remainders, last remainder first. How to represent a negative number in binary. Three steps: Get the binary representation of the positive number Invert every bit Add 1.

Review How many different numbers that can be represented by 4 bits? Always 16 (24), because there are this number of different combinations with 4 bits, regardless of the type of the number these 4 bits are representing. Obviously, this also applies to other number of bits. With n bits, we can represent 2n different numbers. If the number is unsigned integer, it is from 0 to 2n-1.

Review – Digging a little deeper Why can a binary number be obtained by keeping on dividing by 2, and why should the last remainder be the first bit? Note that Any integer can be represented by the summation of the powers of 2: d = dn-12n-1 + dn-22n-2 +…+ d222 + d121 + d020 For example, 19 = 16 + 2 + 1 = 1 * 24 + 0 * 23 + 0 * 22 + 1 * 21 + 1 * 20. The binary representation is the binary coefficients. So 19ten in binary is 10011two.

Review – Digging a little deeper In fact, any integer can be represented by the summation of the powers of some base, where the base is either 10, 2 or 16 in this course. For example, 19 = 1 * 101 + 9 * 100. How do you get the 1 and 9? You divide 19 by 10 repeatedly until the quotient is 0, same as binary! In fact, the dividing process is just an efficient way to get the coefficients. How do you determine whether the last bit is 0 or 1? You can do it by checking whether the number is even or odd. Once this is determined, you go ahead to determine the next bit, by checking (d - d020)/2 is even or odd, and so on, until you don’t have to check any more (when the number is 0).

Review Converting from binary to decimal. This conversion is also based on the formula: d = dn-12n-1 + dn-22n-2 +…+ d222 + d121 + d020 while remembering that the digits in the binary representation are the coefficients. For example, given 101011two, in decimal, it is 25 + 23 + 21 + 20 = 43.

Review What is the range of numbers represented by 2’s complement with 4 bits? The answer is [-8,7]. This is because all numbers with leading bit being 1 is a negative number. So we have 8 of them. Then 0 is all 0. Then seven positive numbers. If numbers are represented in n bits, the non-negative numbers are from 0000…00 to 0111…11, the negative numbers are from 1000…00 to 1111…11.

Two’s Complement Representation 4/27/2017 Two’s Complement Representation Type (C) Number of bits Range (decimal) char 8 -128 to 127 short 16 -32768 to 32767 int 32 -2,147,483,648 to 2,147,483,647 long long 64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 n+1 bits (in general) n+1 -2n to 2n - 1 CDA3100 4/27/2017 CDA3100

Why use 2’s complement?* If you think about it, x in 2’s complement with n bits is just 2n-x. Consider 01101 +(– 00011) = 01101 – 00011 = 01010 (13-3=10 in decimal). 01101 – 00011 = 01101 + 100000 – 00011 – 100000 = 01101 + (100000 – 00011) – 100000 = 01101 + 11101 – 100000 = 101010 – 100000 = 01010 11101 is the 2’s complement of 00011. Means that computer (the adder) does not have to be specifically redesigned for dealing with negative numbers, make life easier for the computer The reason is, assume you are subtracting a with b , where 2^{n}>a>b>0. Note that a-b=a+2^{n+1}-b-2^{n+1}. But 2^{n+1}-b is the 2’s complement of b. Also note that 2^{n}>a-b>0. So if represented in binary forms, a+2^{n+1}-b will be having a 1 bit in bit n+1 and some thing in bit 0 to bit n-1 equal to a-b. Bit n will be 0. So you take what is in bit 0 to bit n and it must be a-b.

Addition in binary 39ten + 57ten = ? How to do it in binary?

Addition in Binary First, convert the numbers to binary forms. We are using 8 bits. 39ten -> 001001112 57ten -> 001110012 Second, add them. 00100111 00111001 01100000

Addition in binary The addition is bit by bit. We will run in at most 4 cases, where the leading bit of the result is the carry: 0+0+0=00 1+0+0=01 1+1+0=10 1+1+1=11

Subtraction in Binary 57ten – 39ten = ?

Subtraction in Binary 00111001 00100111 00010010

Subtraction in binary Do this digit by digit. No problem if 0 - 0 = 0, 1 - 0 = 1 1 – 1 = 0. When encounter 0 - 1, set the result to be 1 first, then borrow 1 from the next more significant bit, just as in decimal. Borrow means setting the borrowed bit to be 0 and the bits from the bit following the borrowed bit to the bit before this bit to be 1. If no bit to borrow from, pretend bit n is 1.

Subtraction with 2’s Complement How about 39ten + (-57ten)?

Subtraction with 2’s Complement First, what is (-57ten) in binary in 8 bits? 00111001 (57ten in binary) 11000110 (invert) 11000111 (add 1) Second, add them. 00100111 11000111 11101110

Converting 2’s complement to decimal What is 11101110ten in decimal if it represents a two’s complement number? 11101110 (original) 11101101 (after minus 1. Binary subtraction is just the inverse process of addition. Borrow if you need.) 00010010 (after inversion)

Two’s Complement Representation 4/27/2017 Two’s Complement Representation Sign extension We often need to convert a number in n bits to a number represented with more than n bits From char to int for example This can be done by taking the most significant bit from the shorter one and replicating it to fill the new bits of the longer one Existing bits are simply copied CDA3100 4/27/2017 CDA3100

Sign Extension Example 4/27/2017 Sign Extension Example 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 -3ten -3ten -3ten -3ten - How about unsigned numbers? CDA3100 4/27/2017 CDA3100

Sign Extension Example: Unsigned 4/27/2017 Sign Extension Example: Unsigned 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 252ten 252ten 252ten 252ten CDA3100 4/27/2017 CDA3100

Unsigned and Signed Numbers 4/27/2017 Unsigned and Signed Numbers Note that bit patterns themselves do not have inherent meaning We also need to know the type of the bit patterns For example, which of the following binary numbers is larger? CDA3100 4/27/2017 CDA3100

Unsigned and Signed Numbers 4/27/2017 Unsigned and Signed Numbers Note that bit patterns themselves do not have inherent meaning We also need to know the type of the bit patterns For example, which one is larger? Unsigned numbers? Signed numbers? CDA3100 4/27/2017 CDA3100

Numbers with Fractions So, done with negative numbers. Done with signed and unsigned integers. How about numbers with fractions? How to represent, say, 5.75ten in binary forms?

Numbers with Fractions In general, to represent a real number in binary, you first find the binary representation of the integer part, then find the binary representation of the fraction part, then put a dot in between.

Numbers with fractions The integer part is 5ten which is 101two. How did you get it?

Numbers with Fractions The fraction is 0.75. Note that it is 2-1 + 2-2 = 0.5 + 0.25, so 5.75ten = 101.11two

How to get the fraction In general, what you do is kind of the reverse of getting the binary representation for the integer: divide the fraction first by 0.5 (2-1), take the quotient as the first bit of the binary fraction, then divide the remainder by 0.25 (2-2), take the quotient as the second bit of the binary fraction, then divide the remainder by 0.125 (2-3),

How to get the fraction Take 0.1 as an example. 0.1/0.5=0*0.5+0.1 –> bit 1 is 0. 0.1/0.25 = 0*0.25+0.1 –> bit 2 is 0. 0.1/0.125 = 0*0.125+0.1 –> bit 3 is 0. 0.1/0.0625 = 1*0.0625+0.0375 –> bit 4 is 1. 0.0375/0.03125 = 1*0.03125+0.0625 –> bit 5 is 1. And so on, until the you have used all the bits that hardware permits

Floating Point Numbers 4/27/2017 Floating Point Numbers Recall scientific notation for decimal numbers A number is represented by a significand (or mantissa) and an integer exponent F * 10E Where F is the significand, and E the exponent Example: 3.1415926 * 102 Normalized if F is a single digit number CDA3100 4/27/2017 CDA3100

Floating Points in Binary 4/27/2017 Floating Points in Binary Normalized binary scientific notation For a fixed number of bits, we need to decide How many bits for the significand (or fraction) How many bits for the exponent There is a trade-off between precision and range More bits for significand increases precision while more bits for exponent increases the range CDA3100 4/27/2017 CDA3100

IEEE 754 Floating Point Standard 4/27/2017 IEEE 754 Floating Point Standard Single precision Represented by 32 bits Since the leading 1 bit in the significand in normalized binary numbers is always 1, it is not represented explicitly CDA3100 4/27/2017 CDA3100

4/27/2017 Exponent If we represent exponents using two’s complement, then it would not be intuitive as small numbers appear to be larger 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 CDA3100 4/27/2017 CDA3100

4/27/2017 Biased Notation The most negative exponent will be represented as 00…00 and the most positive as 111…11 That is, we need to subtract the bias from the corresponding unassigned value The value of an IEEE 754 single precision is 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 s exponent fraction 1 bit 8 bits 23 bits CDA3100 4/27/2017 CDA3100

Example 101.11two= 22 + 20 + 2-1 + 2-2 = 5.75ten 4/27/2017 Example 101.11two= 22 + 20 + 2-1 + 2-2 = 5.75ten The normalized binary number will be 1.011122 = 1.01112(129-127) So the exponent is 129ten = 10000001 As a hexadecimal number, the representation is 0x40B80000 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 CDA3100 4/27/2017 CDA3100

IEEE 754 Double Precision It uses 64 bits (two 32-bit words) 4/27/2017 IEEE 754 Double Precision It uses 64 bits (two 32-bit words) 1 bit for the sign 11 bits for the exponent 52 bits for the fraction 1023 as the bias 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 s Exponent fraction 1 bit 11 bits 20 bits Fraction (continued) 32 bits CDA3100 4/27/2017 CDA3100

Example (Double Precision) 4/27/2017 Example (Double Precision) 101.11two= 22 + 20 + 2-1 + 2-2 = 5.75 The normalized binary number will be 1.011122 = 1.01112(1025-1023) So the exponent is 1025ten = 10000000001two As a hexadecimal number, the representation is 0x4017 0000 0000 0000 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 CDA3100 4/27/2017 CDA3100

floating-point number 4/27/2017 Special Cases Single precision Double precision Object represented Exponent Fraction nonzero denormalized number 1-254 anything 1-2046 floating-point number 255 2047  infinity NaN (Not a number) Denormalized If the exponent is all 0s, but the fraction is non-zero (else it would be interpreted as zero), then the value is a denormalized number, which does not have an assumed leading 1 before the binary point. Thus, this represents a number (-1)s × 0.f × 2-126, where s is the sign bit and f is the fraction. For double precision, denormalized numbers are of the form (-1)s × 0.f × 2-1022. From this you can interpret zero as a special type of denormalized number. CDA3100 4/27/2017 CDA3100

Floating Point Numbers How many different numbers can the single precision format represent? What is the largest number it can represent?

Ranges for IEEE 754 Single Precision 4/27/2017 Ranges for IEEE 754 Single Precision Largest positive number Smallest positive number (floating point) 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 CDA3100 4/27/2017 CDA3100

Ranges for IEEE 754 Single Precision 4/27/2017 Ranges for IEEE 754 Single Precision Largest positive number Smallest positive number (floating point) Overflow and underflow Overflow if the exponent is too large to be represented Underflow if the exponent is too small to be represented CDA3100 4/27/2017 CDA3100

Ranges for IEEE 754 Double Precision 4/27/2017 Ranges for IEEE 754 Double Precision Largest positive number Smallest positive number (Floating-point number) CDA3100 4/27/2017 CDA3100

Comments on Overflow and Underflow 4/27/2017 Comments on Overflow and Underflow Overflow (and underflow also for floating numbers) happens when a number is outside the range of a particular representation For example, by using 8-bit two’s complement representation, we can only represent a number between -128 and 127 If a number is smaller than -128, it will cause overflow If a number is larger than 127, it will cause overflow also Note that arithmetic operations can result in overflow CDA3100 4/27/2017 CDA3100