Section 2: Integer & Floating Point Numbers

Slides:



Advertisements
Similar presentations
James Tam Numerical Representations On The Computer: Negative And Rational Numbers How are negative and rational numbers represented on the computer? How.
Advertisements

1  2004 Morgan Kaufmann Publishers Chapter Three.
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
Number Representation (1) Fall 2005 Lecture 12: Number Representation Integers and Computer Arithmetic.
Binary numbers and arithmetic. ADDITION Addition (decimal)
Computer Organization & Programming Chapter2 Number Representation and Logic Operations.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
CENG 311 Machine Representation/Numbers
Computing Systems Basic arithmetic for computers.
Computer Architecture
+ CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 2.
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.
University of Washington Announcements Lab 1, how is it going? Accounts all set? Coursecast Readings! Before class! 1.
University of Washington Integers The Hardware/Software Interface CSE351 Winter 2013.
Ch3a- 2 EE/CS/CPE Computer Organization  Seattle Pacific University Crunching Numbers Topics we need to explore Representing numbers on a computer.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP
순천향대학교 정보기술공학부 이 상 정 1 3. Arithmetic for Computers.
University of Washington Roadmap car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Negative Integers Unsigned binary representation can not be used to represent negative numbers. With paper and pencil arithmetic, a number is made negative.
Lecture 4: Representing Negative Numbers CS 2011 Spring 2016, Dr. Rozier.
COSC2410: LAB 2 BINARY ARITHMETIC SIGNED NUMBERS FLOATING POINT REPRESENTATION BOOLEAN ALGEBRA 1.
Lecture 4: Digital Systems & Binary Numbers (4)
Negative Number Sign-Magnitude: left-most bit as the sign bit –16 bits –Example: 4-bit numbers is given by is given by ’s complement:
William Stallings Computer Organization and Architecture 8th Edition
Cosc 2150: Computer Organization
Backgrounder: Binary Math
Design of Digital Circuits Reading: Binary Numbers
Addition and Subtraction
Negative Numbers and Subtraction
Negative Integers Unsigned binary representation can not be used to represent negative numbers. With paper and pencil arithmetic, a number is made negative.
CHAPTER 9 COMPUTER ARITHMETIC - ALU
Negative Binary Numbers
Number Representation
Numbers in a Computer Unsigned integers Signed magnitude
University of Washington
CSCI206 - Computer Organization & Programming
Computer Architecture & Operations I
Lecture 2 Topics Binary Arithmetic (Unsigned binary operands)
Negative Binary Numbers
Integer Representations and Arithmetic
Addition and Substraction
Dr. Clincy Professor of CS
Dr. Clincy Professor of CS
CS213 Integers Topics Numeric Encodings Programming Implications
Lecture 2 Topics Binary Arithmetic (Unsigned binary operands)
Data Representation Data Types Complements Fixed Point Representation
Arithmetic Logical Unit
Integers II CSE 410 Winter 2017 Instructor: Teaching Assistants:
Subtraction The arithmetic we did so far was limited to unsigned (positive) integers. Today we’ll consider negative numbers and subtraction. The main problem.
Digital Logic & Design Lecture 02.
ECEG-3202 Computer Architecture and Organization
CS/COE0447 Computer Organization & Assembly Language
EEL 3705 / 3705L Digital Logic Design
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
October 17 Chapter 4 – Floating Point Read 5.1 through 5.3 1/16/2019
How are negative and rational numbers represented on the computer?
Dr. Clincy Professor of CS
Instructor: Alexander Stoytchev
Number Systems Rayat Shikshan Sanstha’s
Number Systems Rayat Shikshan Sanstha’s
ECE 171 Digital Circuits Chapter 2 Binary Arithmetic
Computer Organization COMP 210
ECE 331 – Digital System Design
CSC 220: Computer Organization Signed Number Representation
COMS 361 Computer Organization
Chapter3 Fixed Point Representation
Lecture 2: Bits, Bytes, Ints
Two’s Complement & Binary Arithmetic
Presentation transcript:

Section 2: Integer & Floating Point Numbers 1 Section 2: Integer & Floating Point Numbers Representation of integers: unsigned and signed Unsigned and signed integers in C Arithmetic and shifting Sign extension Background: fractional binary numbers IEEE floating-point standard Floating-point operations and rounding Floating-point in C Integers

Unsigned Integers Unsigned values are just what you expect b7b6b5b4b3b2b1b0 = b727 + b626 + b525 + … + b121 + b020 Useful formula: 1+2+4+8+...+2N-1 = 2N -1 You add/subtract them using the normal “carry/borrow” rules, just in binary An important use of unsigned values in C is pointers - there are no negative memory addresses 63 + 8 71 00111111 +00001000 01000111 Integers

Signed Integers Let's do the natural thing for the positives They correspond to the unsigned integers of the same value Example (8 bits): 0x00 = 0, 0x01 = 1, …, 0x7F = 127 But, we need to let about half of them be negative Use the high order bit to indicate negative: call it the “sign bit” Call this a “sign-and-magnitude” representation Examples (8 bits): 0x00 = 000000002 is non-negative, because the sign bit is 0 0x7F = 011111112 is non-negative 0x85 = 100001012 is negative 0x80 = 100000002 is negative… Integers

Sign-and-Magnitude Negatives How should we represent -1 in binary? Sign-and-magnitude: 100000012 Use the MSB for + or -, and the other bits to give magnitude 0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 – 0 – 1 – 2 – 3 – 4 – 5 – 6 – 7 Integers

Sign-and-Magnitude Negatives How should we represent -1 in binary? Sign-and-magnitude: 100000012 Use the MSB for + or -, and the other bits to give magnitude (Unfortunate side effect: there are two representations of 0!) 0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 – 0 – 1 – 2 – 3 – 4 – 5 – 6 – 7 Integers

Sign-and-Magnitude Negatives How should we represent -1 in binary? Sign-and-magnitude: 100000012 Use the MSB for + or -, and the other bits to give magnitude (Unfortunate side effect: there are two representations of 0!) Another problem: math is cumbersome Example: 4 - 3 != 4 + (-3) 0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 – 0 – 1 – 2 – 3 – 4 – 5 – 6 – 7 4 + 3 = 7 √ 4 – 3 = 1 √ 4 + -3 = -7? -4 + 3 = -7? 0100 +1011 1111 Integers

Two’s Complement Negatives How should we represent -1 in binary? Rather than a sign bit, let MSB have same value, but negative weight W-bit word: Bits 0, 1, …, W-2 add 20, 21, …, 2W-2 to value of integer when set, but bit W-1 adds -2W-1 when set e.g. unsigned 10102: 1*23 + 0*22 + 1*21 + 0*20 = 1010 2’s comp. 10102: -1*23 + 0*22 + 1*21 + 0*20 = -610 So -1 represented as 11112; all negative integers still have MSB = 1 Advantages of two’s complement: only one zero, simple arithmetic To get negative representation of any integer, take bitwise complement and then add one! ~x + 1 = -x 0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 + 1 + 2 + 3 + 4 + 5 + 6 + 7 – 8 – 7 – 6 – 5 – 4 – 3 – 2 – 1 Integers

Two’s Complement Arithmetic The same addition procedure works for both unsigned and two’s complement integers Simplifies hardware: only one adder needed Algorithm: simple addition, discard the highest carry bit Called “modular” addition: result is sum modulo 2W Examples: Integers

Two’s Complement Why does it work? Put another way: given the bit representation of a positive integer, we want the negative bit representation to always sum to 0 (ignoring the carry-out bit) when added to the positive representation This turns out to be the bitwise complement plus one What should the 8-bit representation of -1 be? 00000001 +???????? (we want whichever bit string gives the right result) 00000000 00000010 00000011 +???????? +???????? 00000000 00000000 Integers

Two’s Complement Why does it work? Put another way: given the bit representation of a positive integer, we want the negative bit representation to always sum to 0 (ignoring the carry-out bit) when added to the positive representation This turns out to be the bitwise complement plus one What should the 8-bit representation of -1 be? 00000001 +11111111 (we want whichever bit string gives the right result) 100000000 00000010 00000011 +???????? +???????? 00000000 00000000 Integers

Two’s Complement Why does it work? Put another way: given the bit representation of a positive integer, we want the negative bit representation to always sum to 0 (ignoring the carry-out bit) when added to the positive representation This turns out to be the bitwise complement plus one What should the 8-bit representation of -1 be? 00000001 +11111111 (we want whichever bit string gives the right result) 100000000 00000010 00000011 +11111110 +11111101 100000000 100000000 Integers

Unsigned & Signed Numeric Values X Signed Unsigned 0000 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 –8 8 –7 9 –6 10 –5 11 –4 12 –3 13 –2 14 –1 15 1000 1001 1010 1011 1100 1101 1110 1111 Both signed and unsigned integers have limits If you compute a number that is too big, you wrap: 6 + 4 = ? 15U + 2U = ? If you compute a number that is too small, you wrap: -7 - 3 = ? 0U - 2U = ? The CPU may be capable of “throwing an exception” for overflow on signed values But it won't for unsigned C and Java just cruise along silently when overflow occurs... Integers

Visualizations Same W bits interpreted as signed vs. unsigned: Two’s complement (signed) addition: x and y are W bits wide 2w–1 2w 2w Two’s complement Two’s complement +2w–1 –2w–1 2w–1 +2w–1 Unsigned Unsigned –2w–1 x + y +2w Positive overflow +2w –1 +2w –1 Normal –2w –1 –2w –1 Negative overflow –2w Integers

Values To Remember Unsigned Values Two’s Complement Values UMin = 0 000…0 UMax = 2w – 1 111…1 Two’s Complement Values TMin = –2w–1 100…0 TMax = 2w–1 – 1 011…1 Negative 1 111…1 0xFFFFFFFF (32 bits) Values for W = 16 Integers