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

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

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

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

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

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

Integers Sep 3, 2002

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

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

Bits, Bytes, and Integer

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

CS429: Computer Organization and Architecture

Systems Programming and Computer Architecture ( )

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

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

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

Bits, Bytes, and Integers August 26, 2009

Bits, Bytes, and Integers Part 2

Computer Systems CEN591(502) Fall 2011

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

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

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

CS 270: Computer Organization. Integer Arithmetic Operations

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

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

Arithmetic and Bitwise Operations on Binary Data

Integer Encoding and Manipulation

Arithmetic and Bitwise Operations on Binary Data

BITS, BYTES, AND INTEGERS

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

Foundations of Computer Systems

Representing and Manipulating Integers. Jo, Heeseung

Lecture 5-6: Bits, Bytes, and Integers

Computer Organization: A Programmer's Perspective

System Programming CISC 360. Floating Point September 16, 2008

Manipulating Integers

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

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

Bits, Bytes and Integers Part 1

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

Bits, Bytes, and Integers

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

Representing and Manipulating Integers Part I

Representing Integers

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Computer Organization: A Programmer's Perspective

Integers II. CSE 351 Autumn 2018

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Bits, Bytes, and Integers

CSE 351 Midterm - Winter 2017

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

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

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

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

COMP2611: Computer Organization. Data Representation

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

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

Giving credit where credit is due

Number Systems. Readings: , Problem: Implement simple pocket calculator Need: Display, adders & subtractors, inputs

Giving credit where credit is due

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

Lecture 8: Addition, Multiplication & Division

CSE 351 Midterm - Winter 2015 Solutions

World Inside a Computer is Binary

Floating Point. The World is Not Just Integers. Programming languages support numbers with fraction

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

Systems Programming and Computer Architecture ( )

Bits, Bytes and Integers

CS367 Test 1 Review Guide

Chapter 4. Operations on Data

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

Digital Arithmetic. Digital Arithmetic: Operations and Circuits Dr. Farahmand

CS:APP Web Aside DATA:TMIN: Writing TMin in C

Numeric Encodings Prof. James L. Frankel Harvard University

EE 109 Unit 6 Binary Arithmetic

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

Chapter 3: part 3 Binary Subtraction

CHW 261: Logic Design

4/8/17. Admin. Assignment 5 BINARY. David Kauchak CS 52 Spring 2017

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

Integer Representation Floating point Representation Other data types

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

Review. How to represent real numbers

COMP 122/L Lecture 2. Kyle Dewey

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

Foundations of Computer Systems

CSE 351 Midterm - Winter 2015

Signed Multiplication Multiply the positives Negate result if signs of operand are different

Chapter 2 Data Representations

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,...

Module 2: Computer Arithmetic

Chapter 10 - Computer Arithmetic

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

Data Representations & Arithmetic Operations

Computer Organization & Systems Exam I Example Questions

Numbering systems. Dr Abu Arqoub

Adding Binary Integers. Part 4. Negative Binary. Integers. Adding Base 10 Numbers. Adding Binary Example = 10. Arithmetic Logic Unit

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

Transcription:

CISC3 Integers Sep, 9 Encoding Integers Unsigned w 1 BU(X) = x i i i= short int x = 1513; short int y = -1513; Twoʼs Complement w BT(X) = x w 1 w 1 + x i i i= Sign Bit Decimal Hex Binary x 1513 3B D 11111 11111 y -1513 C 93 111 1111 Powerpoint Lectre Notes for Compter Systems: A Programmer's Perspective, R. Bryant and D. O'Hallaron, Prentice Hall, 3 15-13 Fʼ CISC3 Fa9 Encoding Example (Cont.) x = 1513: 11111 11111 y = -1513: 111 1111 Weight 1513-1513 1 1 1 1 1 1 1 1 1 1 1 3 1 3 1 1 1 1 5 1 5 51 1 51 1 1 1 1 9 1 9 19 1 19 13 1 13-37 1-37 3 Sm 1513-1513 CISC3 Fa9 Nmeric Ranges Vales for W = 1 Decimal Hex Binary UMax 5535 FF FF 11111111 11111111 TMax 377 7F FF 1111111 11111111 TMin -37 1-1 -1 FF FF 11111111 11111111 CISC3 Fa9 Page 1

Vales for Different Word Sizes W 1 3 UMax 55 5,535,9,97,95 1,,7,73,79,551,15 TMax 17 3,77,17,3,7 9,3,37,3,5,775,7 TMin -1-3,7 -,17,3, -9,3,37,3,5,775, 5 CISC3 Fa9 Unsigned & Signed Nmeric Vales X BU(X) BT(X) 1 1 1 1 11 3 3 1 11 5 5 11 111 7 7 1 11 9 7 11 1 111 11 5 11 1 111 13 3 111 1 1111 15 1 CISC3 Fa9 Casting Signed to Unsigned Relation between Signed & Unsigned short int x = 1513; nsigned short int x = (nsigned short) x; short int y = -1513; nsigned short int y = (nsigned short) y; Twoʼs Complement TU Unsigned x TB BU x X Maintain Same Bit Pattern - w 1 x + + + + + + x - + + + + + + w 1 w 1 = * w 1 = w x = x x x + w x < 7 CISC3 Fa9 CISC3 Fa9 Page

Relation Between Signed & Unsigned Weight -1513 533 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 5 51 1 1 1 1 1 9 19 13 1 13 1 13 37 1-37 1 37 Sm -1513 533 Signed vs. Unsigned in C y = y + * 37 = y + 553 9 CISC3 Fa9 1 CISC3 Fa9 Casting Srprises Explanation of Casting Srprises UMax UMax 1 U == nsigned -1 < signed -1 U > nsigned 1737-173 > signed 1737U -173 < nsigned -1 - > signed (nsigned) -1 - > nsigned 1737 173U < nsigned 11 1737 (int) 173U > signed CISC3 Fa9 ʼs Comp. Range TMax 1 TMax + 1 TMax TMin 1 CISC3 Fa9 Unsigned Range Page 3

Sign Extension Sign Extension Example short int x = 1513; int ix = (int) x; short int y = -1513; int iy = (int) y; k copies of MSB X w 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 Converting from smaller to larger integer data type C atomatically performs sign extension X 13 k w CISC3 Fa9 1 CISC3 Fa9 Jstification For Sign Extension Why Shold I Use Unsigned? X w - X - + w+1 15 CISC3 Fa9 1 CISC3 Fa9 Page

Negating with Complement & Increment + x ~x -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Comp. & Incr. Examples x = 1513 Decimal Hex Binary x 1513 3B D 11111 11111 ~x -151 C 9 111 111 ~x+1-1513 C 93 111 1111 y -1513 C 93 111 1111 Decimal Hex Binary ~ -1 FF FF 11111111 11111111 ~+1 17 CISC3 Fa9 1 CISC3 Fa9 Unsigned Addition Visalizing Integer Addition Operands: w bits Tre Sm: w+1 bits + v + v Add (, v) Integer Addition Discard Carry: w bits UAdd w (, v) 3 1 1 1 1 UAdd w (,v) = + v + v w + v < w + v w 1 1 1 1 v 19 CISC3 Fa9 CISC3 Fa9 Page 5

Visalizing Unsigned Addition Mathematical Properties Overflow UAdd (, v) Tre Sm w+1 Overflow 1 1 1 w Modlar Sm 1 1 1 1 1 1 1 v 1 CISC3 Fa9 CISC3 Fa9 Twoʼs Complement Addition Characterizing TAdd Operands: w bits Tre Sm: w+1 bits + v + v Tre Sm 111 1 w 1 PosOver 1 w 1 TAdd Reslt 11 1 Discard Carry: w bits TAdd w (, v) PosOver TAdd(, v) > v < < > NegOver 1 1 w 1 1 1 w NegOver + v + w 1 + v < TMin w (NegOver) TAdd w (,v) = + v TMin w + v TMax w + v w 1 TMax w < + v (PosOver) 3 CISC3 Fa9 CISC3 Fa9 Page

Visalizing ʼs Comp. Addition Detecting ʼs Comp. Overflow NegOver w 1 PosOver TAdd (, v) w 1 NegOver - - - - - - - - - - - - v PosOver 5 CISC3 Fa9 CISC3 Fa9 Mathematical Properties of TAdd Mltiplication TComp w () = TMin w TMin w = TMin w 7 CISC3 Fa9 CISC3 Fa9 Page 7

Unsigned Mltiplication in C Unsigned vs. Signed Mltiplication Operands: w bits Tre Prodct: *w bits v Discard w bits: w bits * v UMlt w (, v) 9 CISC3 Fa9 3 CISC3 Fa9 Unsigned vs. Signed Mltiplication Power-of- Mltiply with Shift Operands: w bits Tre Prodct: w+k bits k * k Discard k bits: w bits UMlt w (, k ) TMlt w (, k ) k 1 31 CISC3 Fa9 3 CISC3 Fa9 Page

Unsigned Power-of- Divide with Shift Signed Power-of- Divide with Shift Operands: Division: Reslt: / k / k / k k 1 Division Compted Hex Binary x 1513 1513 3B D 11111 11111 x >> 1 7.5 7 1D B 1111 11111 x >> 95.15 95 3 B 11 11111 x >> 59.5713 59 3B 11111 33 CISC3 Fa9. Binary Point Operands: Division: Reslt: / x k x / k RondDown(x / k ) k 1 3 CISC3 Fa9. Binary Point Division Compted Hex Binary y -1513-1513 C 93 111 1111 y >> 1-7.5-77 E 9 1111 111 y >> -95.15-951 FC 9 111111 111 y >> -59.5713 - FF C 11111111 111 Correct Power-of- Divide Correct Power-of- Divide (Cont.) Case : Ronding Dividend: x k 1 + k + 1 1 1 1 1 Dividend: Divisor: / k / k k 1 + k + 1 1 1 1 1 1 1 1 1 Binary Point 1 1 1 1. 1 1 1 Divisor: Incremented by 1 Binary Point / k 1 x / k 1 1 1 1. Biasing adds 1 to final reslt Incremented by 1 Biasing has no effect 35 CISC3 Fa9 3 CISC3 Fa9 Page 9

Properties of Unsigned Arithmetic Properties of Twoʼs Comp. Arithmetic 37 CISC3 Fa9 3 CISC3 Fa9 Work in Grop: C Pzzles Work in grop of two to solve these C pzzles Taken 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 vales Give example where not tre x < ((x*) < ) Initialization int x = foo(); int y = bar(); nsigned x = x; nsigned y = y; x >= x & 7 == 7 (x<<3) < x > -1 x > y -x < -y x * x >= x > && y > x + y > x >= -x <= 39 x <= CISC3 Fa9 -x >= C Pzzle Answers Assme machine with 3 bit word size, twoʼs comp. integers TMin makes 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 CISC3 Fa9 Page 1