C Puzzles! Taken from old exams. Integers Sep 3, Encoding Integers Unsigned. Encoding Example (Cont.) The course that gives CMU its Zip!

Similar documents
C Puzzles The course that gives CMU its Zip! Integer Arithmetic Operations Jan. 25, Unsigned Addition. Visualizing Integer Addition

Page # CISC360. Integers Sep 11, Encoding Integers Unsigned. Encoding Example (Cont.) Topics. Twoʼs Complement. Sign Bit

Integers Sep 3, 2002

Page 1 CISC360. Encoding Integers Unsigned. Integers Sep 8, Numeric Ranges. Encoding Example (Cont.)

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

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

Bits, Bytes, and Integers. Bits, Bytes, and Integers. The Decimal System and Bases. Everything is bits. Converting from Decimal to Binary

Bits, Bytes, and Integer

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

CS429: Computer Organization and Architecture

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

Integer Arithmetic. CS 347 Lecture 03. January 20, 1998

Systems Programming and Computer Architecture ( )

Bits, Bytes, and Integers August 26, 2009

Computer Systems CEN591(502) Fall 2011

Today: Bits, Bytes, and Integers. Bits, Bytes, and Integers. For example, can count in binary. Everything is bits. Encoding Byte Values

CS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Bits, Bytes, and Integers. Data Representa+on: Base- 2 number representa+on. Binary Representa+ons. Encoding Byte Values. Example Data Representa+ons

Bits, Bytes, and Integers

CS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VIII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

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

Representa7on of integers: unsigned and signed Conversion, cas7ng Expanding, trunca7ng Addi7on, nega7on, mul7plica7on, shiaing

CS140 Lecture 08: Data Representation: Bits and Ints. John Magee 13 February 2017

Representing and Manipulating Integers. Jo, Heeseung

Csci 2021 Class Web Pages. Data Representa?on: Bits, Bytes, and Integers. Binary Representa?ons. Bits, Bytes, and Integers

Bits, Bytes and Integers Part 1

Bits, Bytes, and Integers Part 2

Lecture 5-6: Bits, Bytes, and Integers

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

Integer Encoding and Manipulation

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

BITS, BYTES, AND INTEGERS

Representing Integers. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Bits, Bytes, and Integers

Representing Integers

Representing and Manipulating Integers Part I

Arithmetic and Bitwise Operations on Binary Data

Foundations of Computer Systems

CS 270: Computer Organization. Integer Arithmetic Operations

Computer Organization: A Programmer's Perspective

Arithmetic and Bitwise Operations on Binary Data

Manipulating Integers

Bits, Bytes, and Integers

Ints and Floating Point

Integers II. CSE 351 Autumn Instructor: Justin Hsia

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Bits, Bytes and Integers

Integers II. CSE 351 Autumn 2018

Lemma 1 Let the components of, Suppose. Trees. A tree is a graph which is. (a) Connected and. (b) has no cycles (acyclic). (b)

Bits and Bytes: Data Presentation Mohamed Zahran (aka Z)

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

Bitwise Data Manipulation. Bitwise operations More on integers

Integer Representation

f x y z ds f P S (,, ) lim ( ) ds f P S S S.

POWER-OF-2 BOUNDARIES

Floating Point January 24, 2008

CS367 Test 1 Review Guide

Arithmetic Operators. Portability: Printing Numbers

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

Simple Data Types in C. Alan L. Cox

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

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

Giving credit where credit is due

COMP2611: Computer Organization. Data Representation

Giving credit where credit is due

Digital Image Processing Chapter 5: Image Restoration

The extra single-cycle adders

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

Programming in C++ 5. Integral data types

The final datapath. M u x. Add. 4 Add. Shift left 2. PCSrc. RegWrite. MemToR. MemWrite. Read data 1 I [25-21] Instruction. Read. register 1 Read.

Review. How to represent real numbers

CIS133J. Working with Numbers in Java

CSE 351 Midterm - Winter 2017

Reconstructing Generalized Staircase Polygons with Uniform Step Length

Integer Representation Floating point Representation Other data types

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

CS:APP Web Aside DATA:TNEG: Bit-Level Representation of Two s Complement Negation

CS429: Computer Organization and Architecture

Object Pose from a Single Image

CAPL Scripting Quickstart

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Systems Programming and Computer Architecture ( )

Pushing squares around

The single-cycle design from last time

Adaptive Influence Maximization in Microblog under the Competitive Independent Cascade Model

Basics of Digital Logic Design

Dr Paolo Guagliardo. Fall 2018

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

On Plane Constrained Bounded-Degree Spanners

CSE 351 Midterm - Winter 2015 Solutions

Prof. Kozyrakis. 1. (10 points) Consider the following fragment of Java code:

EE 109 Unit 6 Binary Arithmetic

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

USING MAPLE FOR VISUALIZATION OF TOPOLOGICAL SUBGROUPOIDS OF X Z X

CS 557 Lecture IX. Drexel University Dept. of Computer Science

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Transcription:

15-13 The corse that gies CMU its Zip! Topics class3.ppt Integers Sep 3,! Nmeric Encodings " Unsigned & Two s complement! Programming Implications " C promotion rles! Basic operations " Addition, negation, mltiplication! Programming Implications " Conseqences of oerflow " Using shifts to perform power-of- mltiply/diide 15-13 F C Pzzles! Taen from old exams! Assme machine with 3 bit word size, two s complement integers! For each of the following C expressions, either: " Arge that is tre for all argment ales " Gie example where not tre Initialization int x = foo(); int y = bar(); nsigned x = x; nsigned y = y; x <! ((x*) < ) x >= x & 7 == 7! (x<<3) < x > -1 x > y! -x < -y x * x >= x > && y >! x + y > x >=! -x <= x <=! -x >= 15-13, F Encoding Integers Unsigned Two s Complement w"1 # BU(X) = x i! i i= w! # BT(X) =!x w!1 " w!1 + x i " i i= Encoding Example (Cont.) x = 1513: 11111 11111 y = -1513: 111 1111 Weight1513-1513 short int x = 1513; short int y = -1513;! C short bytes long Sign Bit DecimalHexBinaryx Sign Bit! For s complement, most significant bit indicates sign " for nonnegatie " 1 for negatie 3 15-13, F 15-13, F

Nmeric Ranges Vales for Different Word Sizes Unsigned Vales! UMin =! UMax = w 1 111 1 Vales for W = 1 DecimalHexBinaryUMax Two s s Complement Vales! TMin = w 1 1! TMax = w 1 1 11 1 Other Vales! Mins 1 111 1 W13 Obserations! TMin = TMax + 1 " Asymmetric range! UMax = * TMax + 1 C Programming!!#inclde <limits.h> " K&R App. B11! Declares constants, e.g., "!ULONG_MAX "!LONG_MAX "!LONG_MIN! Vales platform-specific 5 15-13, F 15-13, F Unsigned & Signed Nmeric Vales X BU(X) BT(X) 1 1 1 1 11 3 3 1 11 5 5 11 111 1 7 7 11 9 7 11 1 111 11 5 11 1 111 13 3 111 1111 Eqialence! Same encodings for nonnegatie ales Uniqeness! Eery bit pattern represents niqe integer ale! Each representable integer has niqe bit encoding! Can Inert Mappings! UB(x) = BU -1 (x) " Bit pattern for nsigned integer 1! TB(x) = BT -1 (x) 15 1 " Bit pattern for two s comp 7 integer 15-13, F Casting Signed to Unsigned C Allows Conersions from Signed to Unsigned short int x = 1513; nsigned short int x = (nsigned short) x; short int y = -1513; nsigned short int y = (nsigned short) y; Reslting Vale! No change in bit representation! Nonnegatie ales nchanged " x = 1513! Negatie ales change into (large) positie ales " y = 533 15-13, F

Relation between Signed & Unsigned Relation Between Signed & Unsigned Weight-1513533 Two s Complement x TB TU X BU Unsigned x Maintain Same Bit Pattern - w 1 x + + + + + + x - + + + + + + w 1 w 1 = * w 1 = w x = " x x! # $ x + w x <! y = y + * 37 = y + 553 9 15-13, F 1 15-13, F Signed s. Unsigned in C Constants! By defalt are considered to be signed integers! Unsigned if hae U as sffix U, 99759U Casting! Explicit casting between signed & nsigned same as UT and TU int tx, ty; nsigned x, y; tx = (int) x; y = (nsigned) ty;! Implicit casting also occrs ia assignments and procedre calls tx = x; y = ty; 11 15-13, F Casting Srprises Expression Ealation! If mix nsigned and signed in single expression, signed ales implicitly cast to nsigned! Inclding comparison operations <, >, ==, <=, >=! Examples for W = 3 Constant 1 Constant Relation Ealation U == nsigned -1 < signed -1 U > nsigned 1737-173 > signed 1737U -173 < nsigned -1 - > signed (nsigned) -1 - > nsigned 1737 173U < nsigned 1737 (int) 173U > signed 1 15-13, F

Explanation of Casting Srprises Sign Extension s s Comp. " Unsigned! Ordering Inersion! Negatie " Big Positie UMax UMax 1 Tas:! Gien w-bit signed integer x! Conert it to w+-bit integer with same ale TMax TMax + 1 TMax Unsigned Range Rle:! Mae copies of sign bit:! X # = x w 1,, x w 1, x w 1, x w,, x s Comp. Range 1 copies of MSB X w TMin 13 15-13, F X # 1 15-13, F w Sign Extension Example short int x = 1513; int ix = (int) x; short int y = -1513; int iy = (int) y; Decimal Hex Binary x 1513 3B D 11111 11111 ix 1513 3B D 11111 11111 y -1513 C 93 111 1111 iy -1513 FF FF C 93 11111111 11111111 111 1111! Conerting from smaller to larger integer data type! C atomatically performs sign extension Jstification For Sign Extension Proe Correctness by Indction on! Indction Step: extending by single bit maintains ale X w - X # - + w+1! Key obseration: w 1 = w + w 1! Loo at weight of pper bits: X w 1 x w 1 15 15-13, F X # w x w 1 + w 1 x w 1 = w 1 x w 1 1 15-13, F

Why Shold I Use Unsigned? Don t Use Jst Becase Nmber Nonzero! C compilers on some machines generate less efficient code nsigned i; for (i = 1; i < cnt; i++) a[i] += a[i-1];! Easy to mae mistaes for (i = cnt-; i >= ; i--) a[i] += a[i+1]; Do Use When Performing Modlar Arithmetic! Mltiprecision arithmetic! Other esoteric stff Do Use When Need Extra Bit s s Worth of Range! Woring right p to limit of word size 17 15-13, F Negating with Complement & Increment Claim: Following Holds for s s Complement ~x + 1 == -x Complement! Obseration: ~x + x == 1111 11 == -1 Increment! ~x + x + (-x + 1) == -1 + (-x + 1)! ~x + 1 == -x Warning: Be catios treating int s as integers 1 15-13, F! OK here + x ~x -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Comp. & Incr. Examples x = 1513 lhexbinaryx Unsigned Addition Operands: w bits Tre Sm: w+1 bits + + DecimalHexBinary ~ Discard Carry: w bits UAdd w (, ) Standard Addition Fnction! Ignores carry otpt Implements Modlar Arithmetic s = UAdd w (, ) = + mod w UAdd w (,) = # + $ % +! w + < w + " w 19 15-13, F 15-13, F

Visalizing Integer Addition Visalizing Unsigned Addition Integer Addition! -bit integers,! Compte tre sm Add (, )! Vales increase linearly with and! Forms planar srface 3 1 1 Add (, ) Integer Addition 1 1 1 1 1 1 Wraps Arond! If tre sm " w! At most once Tre Sm w+1 w Oerflow Modlar Sm 1 1 1 1 Oerflow UAdd (, ) 1 1 1 1 1 1 1 15-13, F 15-13, F Mathematical Properties Modlar Addition Forms an Abelian Grop! Closed nder addition!!$!uadd w (, )!! $! w 1! Commtatie UAdd w (, )!!=!! UAdd w (, )! Associatie UAdd w (t, UAdd w (, ))!!=!! UAdd w (UAdd w (t, ), )! is additie identity UAdd w (, )!!=!!! Eery element has additie inerse " Let UComp w ( )!!=! w UAdd w (, UComp w ( ))!!=!! Two s Complement Addition Operands: w bits Tre Sm: w+1 bits TAdd and UAdd hae Identical Bit-Leel Behaior! Signed s. nsigned addition in C: int s, t,, ; s = (int) ((nsigned) + (nsigned) ); t = +! Will gie s == t + + Discard Carry: w bits TAdd w (, ) 3 15-13, F 15-13, F

Characterizing TAdd Fnctionality! Tre sm reqires w+1 bits! Drop off MSB! Treat remaining bits as s comp. integer TAdd(, ) > < < > NegOer PosOer 111 1 1 1 1 1 TAdd w (,) = Tre Sm w 1 w 1 w 1 5 15-13, F w # + + w!1 % $ + % & +! w!1 PosOer NegOer TAdd Reslt 11 1 1 + < TMin (NegOer) w TMin w " + " TMax w TMax w < + (PosOer) Visalizing s Comp. Addition Vales! -bit two s comp.! Range from - to +7 Wraps Arond! If sm % w 1 " Becomes negatie " At most once! If sm < w 1 " Becomes positie " At most once NegOer - - - - - - - - TAdd (, ) 15-13, F - - - - PosOer Detecting s Comp. Oerflow Tas Claim! Gien s = TAdd w (, )! Determine if s = Add w (, )! Example int s,, ; s = + ;! Oerflow iff either:, <, s % (NegOer), %, s < (PosOer) w 1 w 1 of = (< == <) && (<!= s<); PosOer NegOer Mathematical Properties of TAdd Isomorphic Algebra to UAdd! TAdd w (, ) = UT(UAdd w (TU( ), TU())) " Since both hae identical bit patterns Two s s Complement Under TAdd Forms a Grop! Closed, Commtatie, Associatie, is additie identity! Eery element has additie inerse Let TComp w ( )!!=! UT(UComp w (TU( )) TAdd w (, TComp w ( ))!!=!! TComp w () = #! " TMin w $ % TMin w = TMin w 7 15-13, F 15-13, F

Mltiplication Compting Exact Prodct of w-bit nmbers x, y! Either signed or nsigned Ranges! Unsigned:! x * y! ( w 1) = w w+1 + 1 " Up to w bits! Two s complement min: x * y " ( w 1 )*( w 1 1) = w + w 1 " Up to w 1 bits! Two s complement max: x * y! ( w 1 ) = w " Up to w bits, bt only for (TMin w ) Maintaining Exact Reslts! Wold need to eep expanding word size with each prodct compted! Done in software by arbitrary precision arithmetic pacages Unsigned Mltiplication in C Operands: w bits Tre Prodct: *w bits Discard w bits: w bits UMlt w (, ) Standard Mltiplication Fnction! Ignores high order w bits Implements Modlar Arithmetic UMlt w (, ) = mod w * 9 15-13, F 3 15-13, F Unsigned s. Signed Mltiplication Unsigned Mltiplication nsigned x = (nsigned) x; nsigned y = (nsigned) y; nsigned p = x * y! Trncates prodct to w-bit nmber p = UMlt w (x, y)! Modlar arithmetic: p = x & y mod w Two s s Complement Mltiplication int x, y; int p = x * y;! Compte exact prodct of two w-bit nmbers x, y! Trncate reslt to w-bit nmber p = TMlt w (x, y) Unsigned s. Signed Mltiplication Unsigned Mltiplication nsigned x = (nsigned) x; nsigned y = (nsigned) y; nsigned p = x * y Two s s Complement Mltiplication int x, y; Relation int p = x * y;! Signed mltiplication gies same bit-leel reslt as nsigned! p == (nsigned) p 31 15-13, F 3 15-13, F

Power-of- Mltiply with Shift Operation! << gies *! Both signed and nsigned Operands: w bits Tre Prodct: w+ bits Examples Discard bits: w bits UMlt w (, )! << 3 == *! << 5 - << 3 == * 1! Most machines shift and add mch faster than mltiply " Compiler generates this code atomatically 33 15-13, F * TMlt w (, ) Unsigned Power-of- Diide with Shift Qotient of Unsigned by Power of! >> gies ) / *! Uses logical shift Operands: Diision: Reslt: / / 1 DiisionComptedHexBinaryx x > 3 15-13, F. ) / * Binary Point Signed Power-of- Diide with Shift Correct Power-of- Diide Qotient of Signed by Power of! x >> gies ) x / *! Uses arithmetic shift! Ronds wrong direction when < x Operands: / 1 Diision: Reslt: x / RondDown(x / ) DiisionComptedHexBinaryy. Binary Point y Qotient of Negatie Nmber by Power of! Want ' x / ( (Rond Toward )! Compte as ) (x+ -1)/ * " In C: (x + (1<<)-1) >> " Biases diidend toward Case 1: No ronding Diidend: Diisor: / ' / ( 1 + + 1 1 1 1 1 1 1 1 1 1 1 1 1. Binary Point 1 1 1 35 15-13, F Biasing has no effect 3 15-13, F

Correct Power-of- Diide (Cont.) Case : Ronding Diidend: Diisor: / x ' x / ( 1 + + 1 1 1 1 1 Incremented by 1 Biasing adds 1 to final reslt 1 1 1 1 1. Incremented by 1 Binary Point Properties of Unsigned Arithmetic Unsigned Mltiplication with Addition Forms Commtatie Ring! Addition is commtatie grop! Closed nder mltiplication!!$!umlt w (, )!!$! w 1! Mltiplication Commtatie UMlt w (, )!!=!! UMlt w (, )! Mltiplication is Associatie UMlt w (t, UMlt w (, ))!!=!! UMlt w (UMlt w (t, ), )! 1 is mltiplicatie identity UMlt w (, 1)!!=!!! Mltiplication distribtes oer addtion UMlt w (t, UAdd w (, ))!!=!! UAdd w (UMlt w (t, ), UMlt w (t, )) 37 15-13, F 3 15-13, F Properties of Two s Comp. Arithmetic Isomorphic Algebras! Unsigned mltiplication and addition " Trncating to w bits! Two s complement mltiplication and addition " Trncating to w bits Both Form Rings! Isomorphic to ring of integers mod w Comparison to Integer Arithmetic! Both are rings! Integers obey ordering properties, e.g., >! + > >, >! >! These properties are not obeyed by two s comp. arithmetic C Pzzle Answers! Assme machine with 3 bit word size, two s comp. integers! TMin maes a good conterexample in many cases # x <! ((x*) < ) False: TMin # x >= Tre: = UMin # x & 7 == 7! (x<<3) < Tre: x 1 = 1 # x > -1 False: # x > y! -x < -y False: -1, TMin # x * x >= False: 3 # x > && y >! x + y > False: TMax, TMax # x >=! -x <= Tre: TMax < # x <=! -x >= False: TMin TMax + 1 == TMin 39 1513 * 3 == -13 (1-bit words) 15-13, F 15-13, F