Integer Representation Floating point Representation Other data types

Similar documents
Computer Organization: A Programmer's Perspective

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Floating Point Puzzles The course that gives CMU its Zip! Floating Point Jan 22, IEEE Floating Point. Fractional Binary Numbers.

CS429: Computer Organization and Architecture

Giving credit where credit is due

Giving credit where credit is due

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

System Programming CISC 360. Floating Point September 16, 2008

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

Floating Point January 24, 2008

Floating Point. CSE 238/2038/2138: Systems Programming. Instructor: Fatma CORUT ERGİN. Slides adapted from Bryant & O Hallaron s slides

Representing and Manipulating Floating Points. Jo, Heeseung

Floating point. Today. IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Next time.

Representing and Manipulating Floating Points

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

Foundations of Computer Systems

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

COMP2611: Computer Organization. Data Representation

Floating Point : Introduction to Computer Systems 4 th Lecture, May 25, Instructor: Brian Railing. Carnegie Mellon

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Representing and Manipulating Floating Points. Computer Systems Laboratory Sungkyunkwan University

The course that gives CMU its Zip! Floating Point Arithmetic Feb 17, 2000

Today: Floating Point. Floating Point. Fractional Binary Numbers. Fractional binary numbers. bi bi 1 b2 b1 b0 b 1 b 2 b 3 b j

Topics of this Slideset. CS429: Computer Organization and Architecture. Encoding Integers: Unsigned. C Puzzles. Integers

CS429: Computer Organization and Architecture

M1 Computers and Data

Representing and Manipulating Floating Points

Floating Point Numbers

Floating Point Numbers

15213 Recitation 2: Floating Point

Chapter 4: Data Representations

Representing and Manipulating Floating Points

Jin-Soo Kim Systems Software & Architecture Lab. Seoul National University. Integers. Spring 2019

Floating Point Arithmetic

MACHINE LEVEL REPRESENTATION OF DATA

Bits, Bytes, and Integers Part 2

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Data Representation 1

Inf2C - Computer Systems Lecture 2 Data Representation

Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

EE 109 Unit 19. IEEE 754 Floating Point Representation Floating Point Arithmetic

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Bits, Bytes, and Integers

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Number Systems. Both numbers are positive

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

Bits, Bytes and Integers

Integers and Floating Point

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

The type of all data used in a C++ program must be specified

Integers Sep 3, 2002

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

Scientific Computing. Error Analysis

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

CHW 261: Logic Design

Systems 1. Integers. Unsigned & Twoʼs complement. Addition, negation, multiplication

Under the Hood: Data Representation. Computer Science 104 Lecture 2

Introduction to Computers and Programming. Numeric Values

Integers. Dr. Steve Goddard Giving credit where credit is due

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

Integers. Today. Next time. ! Numeric Encodings! Programming Implications! Basic operations. ! Floats

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

Bits and Bytes January 13, 2005

CS367 Test 1 Review Guide

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

3.5 Floating Point: Overview

SIGNED AND UNSIGNED SYSTEMS

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

CS 101: Computer Programming and Utilization

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

Data Representation Floating Point

Floating Point Arithmetic

Topic Notes: Bits and Bytes and Numbers

Representing and Manipulating Integers. Jo, Heeseung

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Recap from Last Time. CSE 2021: Computer Organization. It s All about Numbers! 5/12/2011. Text Pictures Video clips Audio

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Data Representation Floating Point

1.1. Unit 1. Integer Representation

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Representing and Manipulating Integers Part I

Computer Organization & Systems Exam I Example Questions

Unit 3. Constants and Expressions

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

Question 4: a. We want to store a binary encoding of the 150 original Pokemon. How many bits do we need to use?

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

COMP2121: Microprocessors and Interfacing. Number Systems

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

Bits, Bytes, and Integers August 26, 2009

Topic Notes: Bits and Bytes and Numbers

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Chapter 2 Bits, Data Types, and Operations

Transcription:

Chapter 2 Bits, Data Types & Operations Integer Representation Floating point Representation Other data types

Why do Computers use Base 2? Base 10 Number Representation Natural representation for human transactions Hard to Implement Electronically ll Hard to store ENIAC (First electronic computer) used 10 vacuum tubes / digit Hard to transmit Need dhigh h precision i to encode 10 signal levels l on single wire Messy to implement digital logic functions Addition, multiplication, etc. Binary Representation 3.5 = 3 10 0 + 5 10-1 Specifically, the devices that make up a computer are switches that can be on or off, i.e. at high or low voltage. Easy to store with bistable elements Reliably transmitted on noisy and inaccurate wires Examples 3.5 = 1 2 Represent 3.5 10 as 11.1 1 + 1 2 0 + 1 2-1 + 2 Represent 15213 10 as 11101101101101 2 Represent 1.20 10 as 1.0011001100110011[0011] 2 Binary floating point number cannot exactly represent $1.20 2

Data representation in binary We need a way to represent information (data) in a form that is mutually comprehensible by human and machine. We have to develop schemes for representing all conceivable types of information integers, characters, floating point numbers, language, images, actions, etc. Binary digits (Bits): sequences of 0 s and 1 s that help humans keep track of the current flows stored/processed in the computer Using base 2 provide us with two symbols to work with: we can call them on & off, or (more usefully) 0 and 1. We group bits together to allow representation of more complex information For ease of use, Computer scientists usually represent quarters of bits in Hexadecimal eg. xa13f vs. 1010000100111111 Hex Binary x0 0000 x1 0001 x2 0010 x3 0011 x4 0100 x5 0101 x6 0110 x7 0111 x8 1000 x9 1001 xa 1010 xb 1011 xc 1100 xd 1101 xe 1110 xf 1111 3

Unsigned Binary Integers Y = abc = a.2 2 + b.2 1 + c.2 0 (where the digits a, b, c can each take on the values of 0 or 1 only) N = number of bits Range is: 0 i < 2 N 1 Umin = 0 Umax = 2 N 1 Problem: How do we represent negative numbers? 3-bits 5-bits 8-bits 0 000 00000 00000000 1 001 00001 00000001 2 010 00010 00000010 3 011 00011 00000011 4 100 00100 00000100 4

Signed Magnitude Leading bit is the sign bit -4 10100 Y = abc = (-1) a (b.2 1 + c.2 0 ) Range is: -2 N-1 + 1 < i < 2 N-1 1-3 10011-2 10010-1 10001-0 10000 Smin = -2 N-1 + 1 Smax = 2 N-1 1 +0 00000 Problems: How do we do addition/subtraction? We have two numbers for zero (+/-)! +1 00001 +2 00010 +3 00011 +4 00100 5

Two s Complement Transformation -16 10000 To transform a into a, invert all bits in a and add 1 to the result -3 11101 Range is: -2 N-1 <i <2 1-2 11110 Tmin = -2 N-1-1 11111 Tmax = 2 N-1 1 0 00000 Advantages: +1 00001 Operations need not check the sign +2 00010 Only one representation for zero +3 00011 Efficient use of all the bits +15 01111 6

Manipulating Binary numbers 1 Binary to Decimal conversion & vice versa A 4 bit binary number A = a 3 a 2 a 1 a 0 corresponds to: a 3 x 2 3 + a 2 x 2 2 + a 1 x 2 1 + a 0 x 2 0 = a 3 x 8 + a 2 x 4 + a 1 x 2 + a 0 x 1 (where a i = 0 or 1 only) A decimal number can be broken down by iteratively determining the highest power of two that fits in the number: e.g. (13) 10 => e.g. (63) 10 => e.g. (0.63) 10 => In the 2 s complement representation, leading zeros do not affect the value of a positive binary number, and leading ones do not affect the value of a negative number. So: 01101 = 00001101 = 13 and 11011 = 11111011 = 55 00001101 x0d 11111011 xfb 00001000 => 8 (as expected!) x08 7

Manipulating Binary numbers 10 Overflow If we add the two (2 s complement) 4 bit numbers representing 7 and 5 we get : 0111 => +7 0101 => +5 We get 4, not +12 as we would expect!! 1100 => 4 (in 4 bit 2 s comp.) We have overflowed the range of 4 bit 2s 2 s comp. ( 8 to +7), so the result is invalid. Note that if we add 16 to this result we get back 16 4 = 12 this is like stepping up to 5 bit 2 s complement representation In general, if the sum of two positive ii numbers produces a negative result, or vice versa, an overflow has occurred, and the result is invalid in that representation. 8

CS Reality #1 The first place where the underlying hardware abstraction can t be treated as a magic black box results in the need for data types You ve got to understand binary encodings. Can x = 20,000,000,000? Is x 2 0? Int s: 40000 * 40000 > 1,600,000,000 50000 * 50000 >?? Float s: Yes! Is (x + y) + z = x + (y + z)? Unsigned & Signed Int s: Yes! Float s: Is 4/5 = 4/5.0? (1e20 + 1e20) + 3.14 > 3.14 1e20 + ( 1e20 + 3.14) > 0? 3? 3.1? Maybe? (int) 4/5 = 0 what is 4/5.0? 9

C++ Integer Puzzles Assume machine with 32 bit word size, two s complement integers For each of the following C++ expressions, either: Argue that is true for all argument values Give example where not true x * x >= 0 ux >= 0 Initialization ux > -1 int x = foo(); int y = bar(); unsigned ux = x; unsigned uy = y; x < 0 (2*x) < 0 x > y -x < -y x > 0 && y > 0 x + y > 0 x >= 0 -x <= 0 x <= 0 -x >= 0 10

Problems Patt & Patel, Chapter 2: 2.4, 2.8, 2.10, 2.11, 2.17, 2.21 11

Representing Strings Strings in C/C++ Represented by array of characters Each character encoded in ASCII format Standard 7 bit encoding of character set Other encodings exist, but uncommon char S[6] = "15213"; x00a6 x00a7 x31 x35 UNICODE 16 bit superset of ASCII that x00a8 x32 includes characters for non English alphabets Character 0 has code 0x30 Digit i has code 0x30+i String should be null terminated Final character = 0 Line termination, and other special characters vary x00a9 x00aa x00ab Strings are really just arrays of numbers! How can we represent numbers using bits? Hey Hey, how do I know it s a string in the first place? x31 x33 x00 Hex #49 #53 #50 #49 #51 #00 Dec 12

Real numbers Most numbers are not integer! Range: The magnitude of the numbers we can represent is determined by how many bits we use: Precision: e.g. with 32 bits the largest number we can represent is about +/ 2 billion, far too small for many purposes. The exactness with which we can specify a number: e.g. a 32 bit number gives us 31 bits of precision, or roughly 9 figure precision in decimal representation Our decimal system handles non integer real numbers by adding yet another symbol the decimal point (.) to make a fixed point notation: e.g. 3,456.78 = 3.10 3 + 5.10 2 + 4.10 1 + 6.10 0 + 7.10 1 + 8.10 2 The floating point, or scientific, notation allows us to represent very large and very small numbers (integer or real), with as much or as little precision as needed. 13

Real numbers in a fixed space What if you only have 8 digits to represent a real number? Do you prefer... a low precision number with a large range? or a high precision number with a smaller range? Wouldn t it be nice if we could float the decimal point to allow for either case? 14

Scientific notation In binary, we only have 0 and 1, how can we represent the decimal point? In scientific notation, it s implicit. In other words: 425000 ( 10 0 ) = 425 10 3 = 42.5 10 4 = 4.25 10 5 We can represent any number with the decimal after the first digit by floating the decimal point to the left (or right) 0.0012500125 = 15

Real numbers in binary We mimic the decimal floating point notation to create a hybrid binary floating point number: We first use a binary point to separate whole numbers from fractional numbers to make a fixed point notation: e.g. 25.75 = 4 + 3 + 1 + 1 + 2 10 1.2 1.10 1.10 1.2 1.2 = 11001.110 2 (2 1 = 0.5 and 2 2 = 0.25, etc.) We then float the binary point: 00011001.110 => 1.1001110 x 2 4 mantissa = 1.1001110, exponent = 4 Now we have to express this without the extra symbols ( x, 2,. ) by convention, we divide the available bits into three fields: sign, mantissa, exponent 16

32 bits: IEEE 754 fp numbers 32 bit float Sign: 1 bit 1 8 bits 23 bits s biased exp. fraction N = (-1) s x 1.fraction x 2 (biased exp. 127) Mantissa: 23 bits We normalize the mantissa by dropping the leading 1 and recording only its fractional part (why?) Exponent: 8 bits In order to handle both + and exponents, we add 127 to the actual exponent to create a biased exponent (this provides a lexographic order!) 2 127 => biased exponent = 0000 0000 (= 0) 2 0 => biased exponent = 0111 1111 (= 127) 2 +127 => biased exponent = 1111 1110 (= 254) 17

IEEE 754 fp numbers example Example: 25.75 => 00011001.110 => 1.1001110 x 2 4 sign bit = 0 (+) normalized mantissa (fraction) = 100 1110 0000 0000 0000 0000 biased exponent = 4 + 127 = 131 => 1000 0011 so 25.75 => 0 1000 0011 100 1110 0000 0000 0000 0000 => x41ce0000 Special values represented by convention: Infinity (+ and ): exponent = 255 (1111 1111) and mantissa = 0 NaN (not a number): exponent = 255 and mantissa 0 Zero (0): exponent = 0 and mantissa = 0 note: special case => fraction is de normalized, i.e no hidden 1 18

IEEE 754 fp numbers 64 bit double Double precision (64 bit) floating point 64 bits: 1 11 bits 52 bits s biased exp. fraction N = (-1) s x 1.fraction x 2 (biased exp. 1023) Range & Precision: i 32 bit: mantissa of 23 bits + 1 => approx. 7 digits decimal 2 +/-127 => approx. 10 +/-38 64 bit: mantissa of 52 bits + 1 => approx. 15 digits decimal 2 +/-1023 => approx. 10 +/-306 19

Floating point in C/C++ C Guarantees Two Levels float single precision double double precision Conversions Casting between int,, float,, and double changes numeric values Double or float to int Truncates fractional part Like rounding toward zero Not defined when out of range Generally saturates to TMin or TMax int to double Exact conversion, as long as int has 53 bit word size int to float Will round according to rounding mode 20

Floating point puzzles in C++ int x = ; float f = ; double d = ; Assume neither d nor f is NAN x == (int)(float) x x == (int)(double) x f == (float)(double) f d == (float) d f == -(-f); 2/3 == 2/3.0 d < 0.00 ((d*2) < 0.0) 0) d > f -f > -d d * d >= 0.0 (d+f)-d d == f 21

Another floating point puzzle #include <iostream> using namespace std; int main() { float i; for (i=0.0;i<25.0;i+=0.1) { cout << i << endl; } } [...] 7 7.1 7.2 7.3 7.4 7.5 7.6 7.77 7.79999 7.89999 7.99999 8.09999 8.2 8.3 [...] 22

Floating point number line 32 bits can represent 2 32 unique values How are those values distributed differently between integers and floats? What are the implications? -1 0 +1-10 0 +1 0-2b 0 +2b 23

Ariane 5 Payload rocket 7 billion (Euro) in design alone Danger! Computed horizontal velocity as floating point number Converted to 16 bit integer Worked OK for Ariane 4 Overflowed for Ariane 5 Result same software Exploded 37 seconds after liftoff Cargo worth $500 million 24

Other Data Types Other numeric data types e.g. BCD Bit vectors & masks sometimes we want to deal with the individual bits themselves Logic values True (non zero) or False (0) Instructions Misc Output as data from a compiler Grapics, sounds, 25

Practice Problems Patt & Patel 2.39, 2.40, 2.41, 2.42, 2.56 26