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

Similar documents
Systems Programming and Computer Architecture ( )

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

Computer Systems CEN591(502) Fall 2011

Integers Sep 3, 2002

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

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

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

Bits, Bytes, and Integers

Representing and Manipulating Integers. Jo, Heeseung

Bits, Bytes, and Integers August 26, 2009

Bits, Bytes, and Integers

CS 270: Computer Organization. Integer Arithmetic Operations

Lecture 5-6: Bits, Bytes, and Integers

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

CS429: Computer Organization and Architecture

Manipulating Integers

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

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

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

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

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

Computer Organization: A Programmer's Perspective

Bits, Bytes, and Integers Part 2

Bits, Bytes and Integers Part 1

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

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

Integer Encoding and Manipulation

Foundations of Computer Systems

Bits, Bytes, and Integers

Arithmetic and Bitwise Operations on Binary Data

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

Integers II. CSE 351 Autumn 2018

Arithmetic and Bitwise Operations on Binary Data

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

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

Bits, Bytes, and Integer

Representing and Manipulating Integers Part I

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Representing Integers

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

Sign & Magnitude vs 2 s Complement ( Signed Integer Representa:ons)

Bits, Bytes and Integers

BITS, BYTES, AND INTEGERS

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

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

Integer Representation

Roadmap. The Interface CSE351 Winter Frac3onal Binary Numbers. Today s Topics. Floa3ng- Point Numbers. What is ?

Main Memory Organization

Outline. Integer representa+on and opera+ons Bit opera+ons Floa+ng point numbers. Reading Assignment:

Outline. Integer representa+on and opera+ons Bit opera+ons Floa+ng point numbers. Reading Assignment:

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

Bitwise Data Manipulation. Bitwise operations More on integers

Course Overview. Jo, Heeseung

Floa.ng Point : Introduc;on to Computer Systems 4 th Lecture, Sep. 10, Instructors: Randal E. Bryant and David R.

UNIT IV: DATA PATH DESIGN

University*of*Washington*

Lecture 2: Number Representa2on

Floats are not Reals. BIL 220 Introduc6on to Systems Programming Spring Instructors: Aykut & Erkut Erdem

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

Simple Data Types in C. Alan L. Cox

How a computer runs a program. Binary Representa8on and Opera8ons on Binary Data. Opera8ng System 9/17/13. You can view binary file contents

Data Representa+on: Floa+ng Point Numbers

CSE 351 Midterm - Winter 2017

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

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

CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

REMEMBER TO REGISTER FOR THE EXAM.

SIGNED AND UNSIGNED SYSTEMS

CS367 Test 1 Review Guide

CS61B Lecture #14: Integers

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

!"#$%$&'(!"#$!%&!'#$(%)*+,-./0123/,*-/*4/561-7.*89:-75:* %.0 *;72-1.7<*!=*>?,1?.9*$=!=*

Writing a Fraction Class

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

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

CS 107 Lecture 3: Integer Representations

Machine- Level Programming II: Arithme6c & Control

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

1/%*2.&34.&%56+74.&%*8"(%&,.9%*!"#$%&'()*)+,'-.&.' & /* '012*)314'-5'"16*1+713'

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

Machine-Level Programming II: Control and Arithmetic

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

CS241 Computer Organization Spring

Module 2: Computer Arithmetic

CSCI2467: Systems Programming Concepts

CSE 351 Midterm - Winter 2015 Solutions

Computer System and programming in C

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Integer Representation Floating point Representation Other data types

CS 261 Fall Mike Lam, Professor Integer Encodings

Machine-Level Programming II: Arithmetic & Control. Complete Memory Addressing Modes

System Programming and Computer Architecture (Fall 2009)

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

Floa=ng- Point Numbers

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Arithmetic Operators. Portability: Printing Numbers

Transcription:

Today s Topics Representa7on of integers: unsigned and signed Conversion, cas7ng Expanding, trunca7ng Addi7on, nega7on, mul7plica7on, shiaing CSE351 Inaugural Edi7on Spring 2010 1 Encoding Integers C short 2 bytes long Unsigned Two s Complement Sign Bit For 2 s complement, most significant bit indicates sign 0 for non nega9ve 1 for nega9ve short int x = 12345; short int y = 12345; Sign Bit x ~x + 1 CSE351 Inaugural Edi7on Spring 2010 2

Encoding Example (Cont.) x = 12345: 00110000 00111001 y = 12345: 11001111 11000111 CSE351 Inaugural Edi7on Spring 2010 3 ShiA Opera7ons LeA shia: x << y ShiE bit vector x lee by y posi9ons Throw away extra bits on lee Fill with 0s on right Mul9ply by 2**y Right shia: x >> y ShiE bit vector x right by y posi9ons Throw away extra bits on right Logical shie (for unsigned) Fill with 0s on lee Arithme9c shie (for signed) Replicate most significant bit on right Maintain sign of x Divide by 2**y Argument x << 3 Log. >> 2 Arith. >> 2 Argument x << 3 Log. >> 2 Arith. >> 2 01100010 00010000 00011000 00011000 10100010 00010000 00101000 11101000 Undefined behavior when y < 0 or y word_size CSE351 Inaugural Edi7on Spring 2010 4

Using ShiAs and Masks Extract 2 nd most significant byte of an integer First shie: x >> (2 * 8) Then mask: ( x >> 16 ) & 0xFF x x >> 16 ( x >> 16) & 0xFF 01100001 01100010 01100011 01100100 00000000 00000000 01100001 01100010 00010000 00000000 00000000 00000000 11111111 00011000 00000000 00000000 00000000 01100010 Extrac7ng the sign bit ( x >> 31 ) & 1 need the & 1 to clear out all other bits except LSB Condi7onals as Boolean expressions ( x is 0 or 1 ) if (x) a=y else a=z; which is the same as a = x? y: z; Can be re wri]en as: a = ( (x << 31) >> 31) & y + (!x << 31 ) >> 31 ) & z CSE351 Inaugural Edi7on Spring 2010 5 Numeric Ranges Unsigned Values UMin = 0 000 0 UMax = 2 w 1 111 1 Values for W = 16 Two s Complement Values TMin = 2 w 1 100 0 TMax = 2 w 1 1 011 1 Other Values Minus 1 111 1

Values for Different Word Sizes Observa7ons TMin = TMax + 1 Asymmetric range UMax = 2 * TMax + 1 C Programming #include <limits.h> Declares constants, e.g., ULONG_MAX LONG_MAX LONG_MIN Values planorm specific CSE351 Inaugural Edi7on Spring 2010 7 Unsigned & Signed Numeric Values X B2U(X) B2T(X) 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 7 1010 10 6 1011 11 5 1100 12 4 1101 13 3 1110 14 2 1111 15 1 Equivalence Same encodings for nonnega9ve values Uniqueness Every bit pa]ern represents unique integer value Each representable integer has unique bit encoding Can Invert Mappings U2B(x) = B2U 1 (x) Bit pa]ern for unsigned integer T2B(x) = B2T 1 (x) Bit pa]ern for two s comp integer CSE351 Inaugural Edi7on Spring 2010 8

T2U T2B B2U Two s Complement Unsigned Maintain Same Bit Pa]ern x ux X Mapping Between Signed & Unsigned U2T U2B B2T Two s Complement Unsigned Maintain Same Bit Pa]ern ux x X Keep bit representa7ons and reinterpret CSE351 Inaugural Edi7on Spring 2010 9 Mapping Signed Unsigned 0 1 2 3 4 5 6 7-8 -7-6 -5-4 -3-2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 U2T T2U CSE351 Inaugural Edi7on Spring 2010 10

Mapping Signed Unsigned 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0 1 2 3 4 5 6 7-8 -7-6 -5-4 -3-2 -1 = +16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 CSE351 Inaugural Edi7on Spring 2010 11 Rela7on between Signed & Unsigned Two s Complement x T2B T2U B2U X Unsigned ux w 1 ux + + + + + + x - + + + + + Maintain Same Bit Pa]ern 0 Large nega7ve weight becomes Large posi7ve weight CSE351 Inaugural Edi7on Spring 2010 12

Conversion Visualized 2 s Comp. Unsigned Ordering Inversion Nega9ve Big Posi9ve UMax UMax 1 TMax TMax + 1 TMax Unsigned Range 2 s Complement Range 0 1 2 0 TMin CSE351 Inaugural Edi7on Spring 2010 13 Signed vs. Unsigned in C Constants By default are considered to be signed integers Unsigned if have U as suffix 0U, 4294967259U Cas7ng int tx, ty; unsigned ux, uy; Explicit cas9ng between signed & unsigned same as U2T and T2U tx = (int) ux; uy = (unsigned) ty; Implicit cas9ng also occurs via assignments and procedure calls tx = ux; uy = ty; CSE351 Inaugural Edi7on Spring 2010 14

Cas7ng Surprises Expression Evalua7on If mix unsigned and signed in single expression, signed values implicitly cast to unsigned Including comparison opera9ons <, >, ==, <=, >= Examples for W = 32: TMIN = 2,147,483,648 TMAX = 2,147,483,647 Constant 1 Constant 2 Rela7on Evalua7on 0 0 0U == unsigned 1-1 0 0 < signed 1-1 0U > unsigned 2147483647 2147483647 2147483647 1-2147483648 > signed 2147483647U 2147483647 1-2147483648 < unsigned 1-1 2-2 > signed (unsigned) 1-1 2-2 > unsigned 2147483647 2147483648U < unsigned 2147483647 (int) 2147483648U > signed CSE351 Inaugural Edi7on Spring 2010 15 Code Security Example (revisited) /* Kernel memory region holding user-accessible data */ #define KSIZE 1024 char kbuf[ksize]; /* Copy at most maxlen bytes from kernel region to user buffer */ int copy_from_kernel(void *user_dest, int maxlen) { /* Byte count len is minimum of buffer size and maxlen */ int len = KSIZE < maxlen? KSIZE : maxlen; memcpy(user_dest, kbuf, len); return len; } #define MSIZE 528 void getstuff() { char mybuf[msize]; copy_from_kernel(mybuf, MSIZE); printf( %s\n, mybuf); } CSE351 Inaugural Edi7on Spring 2010 16

Malicious Usage /* Declaration of library function memcpy */ void *memcpy(void *dest, void *src, size_t n); /* Kernel memory region holding user-accessible data */ #define KSIZE 1024 char kbuf[ksize]; /* Copy at most maxlen bytes from kernel region to user buffer */ int copy_from_kernel(void *user_dest, int maxlen) { /* Byte count len is minimum of buffer size and maxlen */ int len = KSIZE < maxlen? KSIZE : maxlen; memcpy(user_dest, kbuf, len); return len; } #define MSIZE 528 void getstuff() { char mybuf[msize]; copy_from_kernel(mybuf, -MSIZE);... } CSE351 Inaugural Edi7on Spring 2010 17 Summary Cas7ng Signed Unsigned: Basic Rules Bit palern is maintained But reinterpreted Can have unexpected effects: adding or subtrac7ng 2 w Expression containing signed and unsigned int int is cast to unsigned!! CSE351 Inaugural Edi7on Spring 2010 18

Sign Extension Task: Given w bit signed integer x Convert it to w+k bit integer with same value Rule: Make k copies of sign bit: X = x w 1,, x w 1, x w 1, x w 2,, x 0 k copies of MSB X w X k w CSE351 Inaugural Edi7on Spring 2010 19 Sign Extension Example short int x = 12345; int ix = (int) x; short int y = -12345; int iy = (int) y; Decimal Hex Binary x 12345 30 39 00110000 01101101 ix 12345 00 00 30 39 00000000 00000000 00110000 01101101 y -12345 CF C7 11001111 11000111 iy -12345 FF FF CF C7 11111111 11111111 11001111 11000111 Conver7ng from smaller to larger integer data type C automa7cally performs sign extension CSE351 Inaugural Edi7on Spring 2010 20

Summary: Expanding, Trunca7ng: Basic Rules Expanding (e.g., short int to int) Unsigned: zeros added Signed: sign extension Both yield expected result Trunca7ng (e.g., unsigned to unsigned short) Unsigned/signed: bits are truncated Result reinterpreted Unsigned: mod opera9on Signed: similar to mod for small numbers only why? CSE351 Inaugural Edi7on Spring 2010 21 Nega7on: Complement & Increment Claim: Following Holds for 2 s Complement ~x + 1 == -x Complement Observa9on: ~x + x == 1111 111 == -1 + x ~x 1 0 0 1 1 1 0 1 0 1 1 0 0 0 1 0 Complete Proof? 1 1 1 1 1 1 1 1 1 CSE351 Inaugural Edi7on Spring 2010 22

Complement & Increment Examples x = 12345 x = 0 CSE351 Inaugural Edi7on Spring 2010 23 Unsigned Addi7on Operands: w bits True Sum: w+1 bits Discard Carry: w bits u + v u + v UAdd w (u, v) Standard Addi7on Func7on Ignores carry output Implements Modular Arithme7c s = UAdd w (u, v) = u + v mod 2 w CSE351 Inaugural Edi7on Spring 2010 24

Visualizing (Mathema7cal) Integer Addi7on Integer Addi7on 4 bit integers u, v Compute true sum Add 4 (u, v) Values increase linearly with u and v Forms planar surface Add 4 (u, v) u v CSE351 Inaugural Edi7on Spring 2010 25 Visualizing Unsigned Addi7on Wraps Around If true sum 2 w At most once UAdd 4 (u, v) Overflow True Sum 2 w+1 Overflow 2 w 0 Modular Sum u v CSE351 Inaugural Edi7on Spring 2010 26

Visualizing 2 s Complement Addi7on Values 4 bit two s comp. Range from 8 to +7 Wraps around (at most once) If sum 2 w 1 Becomes nega9ve If sum < 2 w 1 Becomes posi9ve NegOver TAdd 4 (u, v) v u PosOver CSE351 Inaugural Edi7on Spring 2010 27 Characterizing TAdd Func7onality True sum requires w+1 bits Drop off MSB Treat remaining bits as 2 s complement integer TAdd(u, v) > 0 v < 0 Nega9ve Overflow < 0 u > 0 Posi9ve Overflow 2 w (NegOver) 2 w (PosOver) CSE351 Inaugural Edi7on Spring 2010 28

Mul7plica7on Compu7ng Exact Product of w bit numbers x, y Either signed or unsigned Ranges Unsigned: 0 x * y (2 w 1) 2 = 2 2w 2 w+1 + 1 Up to 2w bits Two s complement min: x * y ( 2 w 1 )*(2 w 1 1) = 2 2w 2 + 2 w 1 Up to 2w 1 bits Two s complement max: x * y ( 2 w 1 ) 2 = 2 2w 2 Up to 2w bits, but only for (TMin w ) 2 Maintaining Exact Results Would need to keep expanding word size with each product computed Done in soeware by arbitrary precision arithme9c packages CSE351 Inaugural Edi7on Spring 2010 29 Unsigned Mul7plica7on in C Operands: w bits True Product: 2*w bits Discard w bits: w bits u v u * v UMult w (u, v) Standard Mul7plica7on Func7on Ignores high order w bits Implements Modular Arithme7c UMult w (u, v) = u v mod 2 w CSE351 Inaugural Edi7on Spring 2010 30

Signed Mul7plica7on in C Operands: w bits True Product: 2*w bits Discard w bits: w bits u v u * v TMult w (u, v) Standard Mul7plica7on Func7on Ignores high order w bits Some of which are different for signed vs. unsigned mul9plica9on Lower bits are the same CSE351 Inaugural Edi7on Spring 2010 31 Power of 2 Mul7ply with ShiA Opera7on u << k gives u * 2 k Both signed and unsigned Operands: w bits True Product: w+k bits u 2 k * u 2 k Discard k bits: w bits UMult w (u, 2 k ) TMult w (u, 2 k ) Examples u << 3 == u * 8 u << 5 - u << 3 == u * 24 Most machines shie and add faster than mul9ply Compiler generates this code automa9cally k 0 0 1 0 0 0 0 0 0 0 0 0 CSE351 Inaugural Edi7on Spring 2010 32

Compiled Mul7plica7on Code C compiler automa7cally generates shia/add code when mul7plying by constant C Func7on int mul12(int x) { return x*12; } Compiled Arithme7c Opera7ons leal (%eax,%eax,2), %eax sall $2, %eax Explana7on t <- x+x*2 return t << 2; CSE351 Inaugural Edi7on Spring 2010 33 Unsigned Power of 2 Divide with ShiA Quo7ent of Unsigned by Power of 2 u >> k gives u / 2 k Uses logical shie k u Operands: / 2 k 0 0 1 0 0 0 Division: Result: u / 2 k u / 2 k 0 0 0 0 0 0. Binary Point CSE351 Inaugural Edi7on Spring 2010 34

Compiled Unsigned Division Code C Func7on unsigned udiv8(unsigned x) { return x/8; } Compiled Arithme7c Opera7ons shrl $3, %eax Explana7on # Logical shift return x >> 3; Uses logical shia for unsigned For Java Users Logical shie wri]en as >>> CSE351 Inaugural Edi7on Spring 2010 35 Signed Power of 2 Divide with ShiA Quo7ent of Signed by Power of 2 x >> k gives x / 2 k Uses arithme9c shie Rounds wrong direc9on when u < 0 Operands: Division: Result: / x 2 k x / 2 k RoundDown(x / 2 k ) k 0 0 1 0 0 0 0. 0 Binary Point CSE351 Inaugural Edi7on Spring 2010 36

Correct Power of 2 Divide Quo7ent of Nega7ve Number by Power of 2 Want x / 2 k (Round Toward 0) Compute as (x+2 k -1)/ 2 k In C: (x + (1<<k)-1) >> k Biases dividend toward 0 Case 1: No rounding Dividend: u +2 k 1 Divisor: / 2 k u / 2 k Biasing has no effect k 1 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 0 0 0 01 1 1 1. Binary Point 1 1 1 CSE351 Inaugural Edi7on Spring 2010 37 Correct Power of 2 Divide (Cont.) Case 2: Rounding Dividend: k x 1 +2 k 1 0 0 0 1 1 1 1 Divisor: / 2 k x / 2 k Incremented by 1 0 0 1 0 0 0 01 1 1 1. Binary Point Biasing adds 1 to final result Incremented by 1 CSE351 Inaugural Edi7on Spring 2010 38

Compiled Signed Division Code C Func7on int idiv8(int x) { return x/8; } Compiled Arithme7c Opera7ons testl %eax, %eax js L4 L3: sarl $3, %eax ret L4: addl $7, %eax jmp L3 Explana7on if x < 0 x += 7; # Arithmetic shift return x >> 3; Uses arithme7c shia for int For Java Users Arith. shie wri]en as >> CSE351 Inaugural Edi7on Spring 2010 39 Arithme7c: Basic Rules Addi7on: Unsigned/signed: Normal addi9on followed by truncate, same opera9on on bit level Unsigned: addi9on mod 2 w Mathema9cal addi9on + possible subtrac9on of 2w Signed: modified addi9on mod 2 w (result in proper range) Mathema9cal addi9on + possible addi9on or subtrac9on of 2w Mul7plica7on: Unsigned/signed: Normal mul9plica9on followed by truncate, same opera9on on bit level Unsigned: mul9plica9on mod 2 w Signed: modified mul9plica9on mod 2 w (result in proper range) CSE351 Inaugural Edi7on Spring 2010 40

Arithme7c: Basic Rules LeA shia Unsigned/signed: mul9plica9on by 2 k Always logical shie Right shia Unsigned: logical shie, div (division + round to zero) by 2 k Signed: arithme9c shie Posi9ve numbers: div (division + round to zero) by 2 k Nega9ve numbers: div (division + round away from zero) by 2 k Use biasing to fix CSE351 Inaugural Edi7on Spring 2010 41