Integer Encoding and Manipulation

Similar documents
Lecture 5-6: Bits, Bytes, and Integers

Systems Programming and Computer Architecture ( )

Bits, Bytes, and Integers

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

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

Arithmetic and Bitwise Operations on Binary Data

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

CS429: Computer Organization and Architecture

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

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

Representing and Manipulating Integers. Jo, Heeseung

Integers Sep 3, 2002

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

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

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

Computer Systems CEN591(502) Fall 2011

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

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Arithmetic and Bitwise Operations on Binary Data

Bits, Bytes, and Integers Part 2

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.

BITS, BYTES, AND INTEGERS

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Foundations of Computer Systems

Bits, Bytes, and Integers August 26, 2009

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

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Bits, Bytes and Integers Part 1

Manipulating Integers

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

Integers II. CSE 351 Autumn 2018

Bits, Bytes, and Integers

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

Representing and Manipulating Integers Part I

CS 270: Computer Organization. Integer Arithmetic Operations

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

Bitwise Data Manipulation. Bitwise operations More on integers

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

Computer Organization: A Programmer's Perspective

Integer Representation

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

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

Representing Integers

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

Number Systems and Computer Arithmetic

CS/COE 0447 Example Problems for Exam 2 Spring 2011

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

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

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

Chapter 3 Arithmetic for Computers

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Binary Adders: Half Adders and Full Adders

Chapter 4. Operations on Data

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

Arithmetic Operations

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

Basic Arithmetic (adding and subtracting)

Microcomputers. Outline. Number Systems and Digital Logic Review

REMEMBER TO REGISTER FOR THE EXAM.

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

Computer Architecture Set Four. Arithmetic

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

Advanced Computer Architecture-CS501

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

EE 109 Unit 6 Binary Arithmetic

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

Module 2: Computer Arithmetic

COMP2611: Computer Organization. Data Representation

Bits and Bytes January 13, 2005

Chapter 3: Operators, Expressions and Type Conversion

Binary Values. CSE 410 Lecture 02

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

Chapter 3: part 3 Binary Subtraction

CS367 Test 1 Review Guide

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Arithmetic Operators. Portability: Printing Numbers

Simple Data Types in C. Alan L. Cox

Number Systems CHAPTER Positional Number Systems

Data Representation Type of Data Representation Integers Bits Unsigned 2 s Comp Excess 7 Excess 8

SIGNED AND UNSIGNED SYSTEMS

JAVA OPERATORS GENERAL

CSCI2467: Systems Programming Concepts

MIPS Integer ALU Requirements

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-2700: Digital Logic Design Winter Notes - Unit 4. hundreds.

Tailoring the 32-Bit ALU to MIPS

Integer Representation

Arithmetic Processing

Integer Representation Floating point Representation Other data types

Chapter 4 Arithmetic Functions

Arithmetic Logic Unit

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

CSC 2400: Computer Systems. Bit- Level vs. Logical Opera<ons. Bit- Level Operators. Common to C and Java &,, ~, ^, <<, >>

Data Types. Data Types. Integer Types. Signed Integers

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

COMP 303 Computer Architecture Lecture 6

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

Operators & Expressions

DIGITAL ARITHMETIC: OPERATIONS AND CIRCUITS

Transcription:

CSE 2421: Systems I Low-Level Programming and Computer Organization Integer Encoding and Manipulation Presentation D Study: Bryant Chapter 2.1 2.3 Gojko Babić 01-24-2018 Unsigned & Signed Integer Encoding Let binary number X have w-bit pattern: x w-1 x w-2 x w-3 x 3 x 2 x 1 x 0 x i = 0 or 1 Binary to Unsigned Binary to Two s Complement interpretation interpretation B2T(X) x w 1 2 w 1 w 2 w 1 B2U(X) x x i 2 i i 2 i i 0 For 2 s complement, most significant bit indicates sign 0 for nonnegative 1 for negative i 0 Sign Bit short int x = 15213; short int y = -15213; or short int x = 0x3B6D; short int y = 0xC493; Decimal Hex Binary x 15213 3B 6D 00111011 01101101 y -15213 C4 93 11000100 10010011 Presentation D 2 1

2 s Complement Encoding Examples x = 15213: 00111011 01101101 y = -15213: 11000100 10010011 Weight 15213 15213 1 1 1 1 1 2 0 0 1 2 4 1 4 0 0 8 1 8 0 0 16 0 0 1 16 32 1 32 0 0 64 1 64 0 0 128 0 0 1 128 256 1 256 0 0 512 1 512 0 0 1024 0 0 1 1024 2048 1 2048 0 0 4096 1 4096 0 0 8192 1 8192 0 0 16384 0 0 1 16384 32768 0 0 1 32768 Sum 15213 15213 3 Unsigned Binary Decimal Conversion from binary unsigned integer to decimal integer 101101 2 = 45 10 since: 101101 2 = 2 5 + 2 3 + 2 2 + 2 0 = 45 10 Conversion from 927 10 integer to 16-bit binary integer: 927/2 = 463 + 1 LSB 463/2 = 231 + 1 231/2 = 115 + 1 115/2 = 57 +1 57/2 = 28 + 1 28/2 = 14 + 0 14/2 = 7 + 0 7/2 = 3 + 1 3/2 = 1 + 1 1/2 = 0 + 1 927 10 as short int = 0000001110011111 2 What about negative integers? g. babic Presentation D 4 2

Claim: Following holds for 2 s complement integers: ~x + 1 = x ~x is 1 s complement of x change 1 s to 0 s and 0 s to 1 s Proof: Negation = Complement + Increment ~x + x = 1111 111 = 1 1 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 ~x + x = 1 ~x + x +1 x = 1 +1 x ~x +1 = x Example: x = 15213 10 = 0011101101101101 2 x = ~x + 1 = 1100010010010010 + 1 = 1100010010010011 Works from positive 2 s complement integer to negative 2 s complement integer, as well as wise verse. Example: x=0 -x=? + x ~x 0 1 1 0 0 0 1 0 g. babic Presentation D 5 Numeric Ranges of W-bit Integers Unsigned Values UMin = 0 bit pattern: 000 000 UMax = 2 w 1 bit pattern: 111 111 Two s Complement Values TMin = 2 w 1 bit pattern: 100 000 TMax = 2 w 1 1 bit pattern: 011 111 Values for W = 16 Decimal Hex Binary UMax 65535 FF FF 11111111 11111111 TMax 32767 7F FF 01111111 11111111 TMin -32768 80 00 10000000 00000000-1 -1 FF FF 11111111 11111111 0 0 00 00 00000000 00000000 Presentation D 6 3

Values for Different Word Sizes W 8 16 32 64 UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615 TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807 TMin 128 32,768 2,147,483,648 9,223,372,036,854,775,808 Observations: TMin = TMax + 1 UMax = 2 * Tmax +1 Integer sizes are platform specific Our C language platform supports: (unsigned) char 1 byte (unsigned) short int 2 bytes (unsigned) int 4 bytes (unsigned) long long int 8 bytes Presentation D 7 4-bit 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 nonnegative values Uniqueness Every bit pattern represents unique integer value Each representable integer has unique bit encoding Presentation D 8 4

4-bit Signed Unsigned Mapping Bits Signed Unsigned 0000 0 0 0001 1 1 0010 0011 0100 2 3 4 = 2 3 4 0101 5 5 0110 6 6 0111 7 7 1000-8 8 1001-7 9 1010 1011-6 -5 +/ 16 10 11 1100-4 12 1101-3 13 1110-2 14 1111-1 15 9 Relation Between Signed & Unsigned Mappings between unsigned and two s complement numbers: keep bit representations, but reinterpret and can have unexpected effect of adding or subtracting 2 w ux w 1 0 + + + + + + x - + + + + + Largest negative weight becomes large positive weight Presentation D 10 5

Signed & Unsigned Conversion Visualized UMax UMax 1 TMax TMax + 1 TMax Unsigned Range 2 s Complement Range 0 1 2 0 TMin Presentation D 11 Unsigned vs. Signed in C By default, decimal constants are considered to be signed integers. Unsigned if have U as suffix: 0U, 4294967259U Explicit casting between signed & unsigned for same size integers doesn t change bit pattern: int tx, ty; unsigned int ux, uy; tx = (int) ux; uy = (unsigned) ty; Implicit casting also occurs via assignments and function calls: tx = ux; uy = ty; Explicit and implicit casting above gives the same results. Presentation D 12 6

Casting Surprises Expression evaluation: If there is a mix of unsigned and signed in single expression, signed values implicitly cast to unsigned; includes arithmetic and relational expressions Examples for W = 32, i.e. 32-bit integers Constant 1 Constant 2 Relation Evaluation 0 0U == unsigned -1 0 < signed -1 0U > unsigned 2147483647-2147483647-1 > signed 2147483647U -2147483647-1 < unsigned -1-2 > signed (unsigned) -1-2 > unsigned 2147483647 2147483648U < unsigned 2147483647 (int) 2147483648U > signed TMin=-2,147,483,648, TMax=2,147,483,647, UMax=4,294,967,295 Presentation D 13 Expanding & Truncating Integers Expanding refers to converting shorter-bit integer into longer-bit integer, e.g. short int into int Unsigned integers: zeros added Signed integers: sign extension Interesting case: when a shorter signed variable is assigned to a longer unsigned variable E.g.: unsigned int x; short int y; x = y; y is first sign extended to 32-bits, then that value is assigned to x Truncating refers to converting longer-bit integer into shorter-bit integer, e.g., unsigned into unsigned short Unsigned & signed integers: bits are truncated and results are reinterpreted For unsigned integers could be thought as mod operation Presentation D 14 7

Task: Given w-bit signed integer X convert it to w+k-bit integer X 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 X W 1 Sign Extension X w X k w Presentation D 15 Sign Extension Examples C automatically performs sign extension when converting from smaller to larger integer data type short int x = 15213; int ix = (int) x; //explicit casting not needed short int y = -15213; int iy = (int) y; //explicit casting not needed Decimal Hex Binary x 15213 3B 6D 00111011 01101101 ix 15213 00 00 3B 6D 00000000 00000000 00111011 01101101 y -15213 C4 93 11000100 10010011 iy -15213 FF FF C4 93 11111111 11111111 11000100 10010011 g. babic Presentation D 16 8

Mixing Integer Types: Example 1 int main() /*Expanding.c*/ %X an integer argument is { printed in hex; %hx a short int argument is short int si1=0x85ff; printed in hex; unsigned short su1=0x85ff; Output: int i1; su1= 85FF i1= 000085FF unsigned int u1; si1= 85FF u1= FFFF85FF i1=su1; u1=si1; printf("su1= %hx i1= %.8X", su1, i1); printf("\nsi1= %hx u1= %.8X", si1, u1); } g. babic Presentation D 17 Mixing Integer Types: Example 2 int main() { int i1 = 70000, i2; short si1 = -70, si2, si3; si2=i1/si1; si3= (short)i1/si1; i2= (short)(i1/si1); printf("i1%%d= %d i1%%hd= %hd si1= %hd \nsi2= %hd si3= %hd i2 =%d", i1, i1, si1, si2, si3, i2); } Output: i1%d= 70000 i1%hd= 4464 si1= -70 si2= -1000 si3= -63 i2= -1000 g. babic Presentation D 18 9

In the previous example little endian byte ordering assumed. Big Endian Least significant byte has highest address Little Endian Byte Ordering Least significant byte has lowest address Example: Variable X has 4-byte content 0x01234567 at address 0x1000; thus X includes consecutive bytes at addresses 0x1000, 0x1001, 0x1002 and 0x1003 Big Endian Little Endian 0x1003 0x1002 0x1001 0x1000 01 67 23 45 45 23 67 01 0x1003 0x1002 0x1001 0x1000 67 01 45 23 23 45 01 67 Presentation D 19 Bitwise Operations in C Bitwise operators perform the logical operations and (&), or ( ), xor (^), and 1 s complement (~). Those operations may be applied to any integer data type: signed/unsigned, long, int, short, char and they view arguments as bit vectors Function applied bit-wise All of the properties of Boolean algebra apply 01101001 01101001 & 01010101001 01010101 01101001 ^ 01010101 00111100 ~ 01010101 10101010 01000001 01111101 00111100 10101010 Presentation D 20 10

Contrast with Logical Operators in C Logical operators and (&&), or (II) and not (!) view 0 as false, anything nonzero as true Always return 0 or 1 with early termination Examples!0x41 0x00!0x00 0x01 0x69 && 0x55 0x01 0x69 0x55 0x01 Examples with bit level operators &,, ~; no ^ counterpart ~0x41 0xBE ~01000001 10111110 ~0x00 0xFF ~00000000 11111111 0x69 & 0x55 0x41 01101001 & 01010101 01000001 0x69 0x55 0x7D 01101001 01010101 01111101 Presentation D 21 Shift operations may be applied to any integer data type Left shift: x << y Shift bit-vector x left y positions Throw away extra bits on left Fill with 0 s on right Right shift: x >> y Shift bit-vector x right y positions Throw away extra bits on right Logical shift Fill with 0 s on left Arithmetic shift Shift Operations in C Replicate most significant bit on left Argument X << 3 Logical >>2 Arithmetic>>2 Argument X << 3 Logical >>2 Arithmetic>>2 Undefined behavior if shift amount < 0 or word size 01100010 00010000 00011000 00011000 10100010 00010000 00101000 11101000 Presentation D 22 11

Shifts on Signed & Unsigned Integers C performs right shifts on signed integers as arithmetic shifts C performs right shifts on unsigned integers as logical shifts int main() { /*Program Shift.Short.c */ int i1=0x81234567, i2, i3; unsigned u1=0x81234567, u2, u3; i2 = i1 >> 8; u2 = u1 >> 8; i3 = i1 << 8; u3 = u1 << 8; printf("i1= 0x%x i2= 0x%.8X i3= 0x%x", i1, i2, i3); printf("\nu1= 0x%x u2= 0x%.8x u3= 0x%x", u1, u2, u3); } Output: i1= 0x81234567 i2= 0xFF812345 i3= 0x23456700 u1= 0x81234567 u2= 0x00812345 u3= 0x23456700 g. babic Presentation D 23 Compounded Assignments In addition to arithmetic compounded assignments, such as += and %=, C provides more derivatives by combining computation and assignment together. &= bitwise AND assignment = bitwise OR assignment ^= bitwise exclusive OR assignment <<= left shift assignment >>= right shift assignment Examples: i&= 3 i= i& 3 i <<= 3 i = i << 3 Presentation D 24 12

1. a = 00010011 b = 01010001 a + b = 001100100 2. a = 11010111 b = 11010001 a + b = 110101000 3. a = 01010011 b = 01010001 a + b = 010100100 4. a = 01110011 b = 11010001 a + b = 101000100 5. a = 10110011 b = 10010001 a + b = 101000100 Adding Binary Numbers 1-bit Adder Truth Table Input Output a i b i Carry In i Sum i CarryOut i 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1 1 0 1 1 0 0 1 0 1 0 1 0 1 1 1 0 0 1 1 1 1 1 1 Let us analyze each case assuming addition of unsigned or signed integers g. babic Presentation D 25 Analysis of Examples on Previous Slide 1. a = 00010011 As signed a=+19, b=+81, a + b = +100 correct b = 01010001 As unsigned a=19, b=81, a + b = 100 correct a + b = 001100100 2. a = 11010111 As signed a=-41, b=-47, a + b = -88 correct b = 11010001 As unsigned a=215, b=209, a + b = 168 incorrect a + b = 110101000 Final carryout == 1 unsigned overflow 3. a = 01010011 As signed a=+83, b=+81, a+b = -92 incorrect b = 01010001 As unsigned a=83, b=81, a+b = 164 correct a + b = 010100100 Adding 2 positive gives negative signed overflow 4. a = 01110011 As signed a=+115, b=-47, a+b=+68 correct b = 11010001 As unsigned a=115, b=209, a+b=68 incorrect a + b = 101000100 Final carry out == 1 unsigned overflow 5. a = 10110011 As signed a=-77, b=-111, a+b = +68 incorrect b = 10010001 As unsigned a=179, b=145, a+b=68 incorrect a + b = 101000100 Adding 2 negative gives positive signed overflow g. babic 26 Final carry out == 1 unsigned overflow 13

UAdd w : Unsigned Addition If operands have w bits, true sum has w+1 bits But standard C addition function ignores (w+1)st bit (final carry output), i.e. (w+1)st bit is truncated. If final carry out is 1, we are left with wrong result overflow UAdd w (u, v) = (u + v) mod 2 w u + v u + v UAdd w (u, v) UAdd w (u,v) u v u v 2 w u v 2 w u v 2 w Overflow case Presentation D 27 TAdd w : Two s Complement Addition Operands: w bits + u v True Sum: w+1 bits u + v Discard carryout: w bits TAdd w (u, v) TAdd w (u,v) u v 2 w 1 u v u v 2 w 1 u v TMin w Negative overflow TMin w u v TMax w TMax w u v Positive overflow Interesting property: 2 s complement addition function is same if operands have same or different signs. Compare with decimal addition! Presentation D 28 14

Binary Addition: Summary TAdd w and UAdd w have identical bit-level behavior Signed vs. unsigned addition in C: int s, t, u, v; s = (int) ((unsigned) u + (unsigned) v); t = u + v /* s == t */ How to detect unsigned addition overflow? If the final carryout is 1. Why? If the sum is smaller than any of operands. Why? How to detect 2 s complement addition overflow? If adding 2 negative operands results in positive sum or if adding 2 positive operands results in negative sum. Why? If carryin in the most significant bit is different from final carryout. Why? g. babic Presentation D 29 Logical Operations and Gates Logical operation Complement A 0 1 ~A 1 0 AND A B A and B 0 0 0 0 1 0 1 0 0 OR A B A or B 0 0 0 0 1 1 1 0 1 XOR A B A xor B 0 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 0 Gates Gates are simplest digital logic circuits, and they implement basic logical operations (functions). Gates are designed using few resistors and transistors. Gates are used to build more complex circuits that implement more complex logic functions. g. babic Presentation D 30 15

1-bit Adder 32-bit adder can be built out of 32 1-bit adders 1-bit Adder Truth Table CarryIn CarryIn i = Carryout i-1 Input Output a i b i CarryIn i Sum i CarryOut i a a i b i i th 1-bit Adder Sum i 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1 1 0 1 1 0 0 1 0 1 0 1 0 1 Carryout CarryOut i 1 1 0 0 1 1 1 1 1 1 g. babic Presentation D 31 Designing 1-bit Adder A part of 1-bit adder design that calculates carryout C a r r y I n a b C a r r y O u t g. babic Presentation D 32 16

2 s Complement Overflow fl1 ~/Cse2421> add2char #include<stdio.h> a=83, a=0x53 b=81, b=0x51 void main() c=-92, c=0xffffffa4 { a+b=164, a+b=0x000000a4 char a=83, b=81, c; printf ("a=%hd, a=0x%hx",a, a); printf (" b=%hd, b=0x%hx",b, b); c=a+b; printf ("\n c=%d, c=0x%x",c, c); printf ("\n a+b=%d, a+b=0x%.8x",a+b, a+b); } g. babic Presentation D 33 32-bit Ripple Carry Adder 0 a0 b0 Cin + Cout sum0 a1 b1 Cin + Cout sum1 a2 b2 Cin + Cout sum2 a31 b31 Cin + Cout sum31 Carry out g. babic Presentation D 34 17

32-bit Subtractor 0 1 A B = A + ( B) = A + ~B + 1 a0 b0 Cin + Cout Result0 a1 b1 Cin + Cout Result1 a2 b2 Cin + Cout Result2 a31 b31 Cin + Cout Result31 CarryOut g. babic Presentation D 35 Which of Statements Below Are Always True? Initialization: int x = foo(); int y = bar(); unsigned ux=x; unsigned uy=y; x < 0 implies x*2 < 0? No if overflow into positive! ux >= 0? Yes. x&7 == 7 implies (x << 30) < 0? Yes. ux > -1? Never. Umax == -1! x > y implies -x < -y? No if y == Tmin! x*x >= 0? No if overflow into negative! x > 0 && y > 0 implies x + y > 0? No if overflow into negative! x >= 0 implies -x <= 0? Yes. x <= 0 implies -x >= 0? No if x == Tmin. (x -x) >> 31 == -1? No if x == 0 ux >> 3 == ux / 8? Yes. x >> 3 == x / 8? No if x negative! int x = -19; y = x >> 3; // y = -3 int z = x / 8; // z = -2 x & (x-1)!= 0? No. x == 0 or x == 1! or x==tmin 36 Presentation D 18

Unsigned Multiplication in C Computing exact product of w-bit unsigned numbers x, y Range of results: 0 x * y (2 w 1) 2 = 2 2w 2 w+1 + 1 up to 2w bits needed But standard C unsigned multiplication function ignores high order w bits and gives as a result of multiplication lower w bits. C implements modular arithmetic: UMult w (u, v) = u * v mod 2 w u Operands: w bits * v True product: 2*w bits u * v Discard w bits: w bits UMult w (u, v) Presentation D 37 Computing exact product of w-bit 2 s complement numbers x, y Range of results: min: x *y ( 2 w 1 )*(2 w 1 1) = 2 2w 2 + 2 w 1 up to 2w 2 bits max: x *y ( 2 w 1 ) 2 Signed Multiplication in C = 2 2w 2 up to 2w 1 bits As for unsigned, standard C signed multiplication function ignores high order w bits. u Operands: w bits * v True product: 2*w bits u * v Discard w bits: w bits TMult w (u, v) Although some of ignored bits are different for signed vs. unsigned multiplication, lower w bits are the same. But, how do we figure out if lower w bits are the complete product or we have lost some significant bit values in higher w bits? Presentation D 38 19

Multiplication Examples in C Below shown are three examples of multiplication of two 3-bit unsigned and three examples of two 2 s complement numbers with identical bit patterns. Although the bit-level representations of the full products may differ, those of the truncated product are identical for signed and unsigned multiplication. Mode x y x*y Truncated x*y unsigned 5 10 = 101 2 3 10 = 011 2 15 10 = 001111 2 111 2 = 7 10 2 s comp -3 10 = 101 2 3 10 = 011 2-9 10 = 110111 2 111 2 = -1 10 unsigned 4 10 = 100 2 7 10 = 111 2 28 10 = 011100 2 100 2 = 4 10 2 s comp -4 10 = 100 2-1 10 = 111 2 4 10 = 000100 2 100 2 = -4 10 unsigned 3 10 = 011 2 3 10 = 011 2 9 10 = 001001 2 001 2 = 1 10 2 s comp 3 10 = 011 2 3 10 = 011 2 9 10 = 001001 2 001 2 = 1 10 g. babic Presentation D 39 Mixing Integer Types: Example 3 int main() { int i2; long i3,i4; short si1 = -1000; i2= si1*si1*si1*si1*si1; i3= si1*si1*si1*si1*si1; i4=si1; i4= i4*si1*si1*si1*si1; printf("i2%%d= %d", i2); printf("\ni2%%hd= %hd", i2); printf("\ni2%%ld= %ld", i2); printf("\ni3%%d= %d", i3); printf("\ni3%%hd= %hd", i3); printf("\ni3%%ld= %ld", i3); printf("\ni4%%d= %d", i4); printf("\ni4%%hd= %hd", i4); printf("\ni4%%ld= %ld", i4);} fl1 ~/Cse2421> MixInt1 i2%d= 1530494976 i2%hd= -32768 i2%ld= 1530494976 i3%d= 1530494976 i3%hd= -32768 i3%ld= 1530494976 i4%d= 1530494976 i4%hd= -32768 i4%ld= -1000000000000000 40 20

Power-of-2 Multiply Using Shifts Operation u << k (left shift) is equivalent to u *2 k for both signed and unsigned. Most machines shift and add much faster than multiply and if one of operands is constant, compilers generate code where an multiplication is replaced by shifts and adds. Examples: x = u * 8 could be compiled as: x = u << 3 x = u * 24 could be compiled as: x = (u << 5) (u<<3) //There is a better way? x= u * 12 could be compiled as: x = (u<<1 + u) << 2 g. babic Presentation D 41 Power-of-2 Divide Using Shifts Divide is also much slow than shift. Quotient of an unsigned integer by power of 2 is equivalent to logical right shift, i.e. u >> k gives u / 2 k. Division Computed Hex Binary x 15213 15213 3B 6D 00111011 01101101 x >> 1 7606.5 7606 1D B6 00011101 10110110 x >> 4 950.8125 950 03 B6 00000011 10110110 x >> 8 59.4257813 59 00 3B 00000000 00111011 Similarly, a quotient of signed integer by power of 2 would be equivalent to arithmetic right shift, i.e. u >> k would give u / 2 k. Arithmetic shift works correct for positive numbers (as logical shift does for unsigned), but for negative integers a result rounds in wrong direction. Division Computed Hex Binary y -15213-15213 C4 93 11000100 10010011 y >> 1-7606.5-7607 E2 49 11100010 01001001 y >> 4-950.8125-951 FC 49 11111100 01001001 y >> 8-59.4257813-60 FF C4 11111111 11000100 What is a correction for negative integers? g. babic Presentation D 42 21

Multiplication Multiplication is more complicated than addition and it can be accomplished by shifting and additions. But more time and more area required Example of unsigned multiplication mimicking decimal multiply: 5-bit multiplicand 11001 2 = 25 10 5-bit multiplier 10011 2 = 19 10 11001 11001 00000 00000 11001. 111011011 2 = 475 10 But, this algorithm is very impractical to implement because of a problem to perform addition of many operands at once. g. babic Presentation D 43 Unsigned Integers Multiplication The multiplication can be done with intermediate additions. We introduce Product with a double number of bits initialized by multiplier in lower half and 0 s in upper half. The same example: multiplicand 11001 Intermediate Product initially 00000 10011 Add multiplicand since Product bit 0 = 1 011001 10011 Shift Product right; Step 1 done 01100 11001 Add since Product bit 0 = 1 100101 11001 Shift Product right; Step 2 done 10010 11100 Shift Product right, no addition bit=0; Step 3 done 01001 01110 Shift Product right, no addition bit=0; Step 4 done 00100 10111 Add since Product bit 0 = 1 011101 10111 Shift Product right, Step 5 done 01110 11011 Finale result in Product = 01110 11011 2 = 475 10 g. babic Presentation D 44 22

Unsigned Integer Multiplication Multiplicand & Multiplier are w-bit unsigned integers. Initially, 2w-bit Product has Multiplier in its w lower bits and zeros in its w higher bits. == 1 Start Test Product bit 0 == 0 Add Multiplicand to the left half of Product and store the result to the left half of Product Shift Product 1-bit right; Shift in the final carry out from the last addition Shift Product 1-bit logical right w repetitions? No Yes g. babic Presentation D 45 Result in Product Register Multiplication of 2 s Complement Integers A simple algorithm that uses unsigned integer multiplication: If needed, convert to positive integer any negative operand and remember original signs Perform multiplication of unsigned integers Negate product if original signs disagree This algorithm is not simple to implement since: it has to account in advance about signs, if needed, convert operands from negative to positive integers, if needed, convert result back to negative integer at the end Booth s algorithm for 2 s complement multiplication doesn t require converting operands between negative and positive integers. g. babic Presentation F 46 23

Booth s Algorithm Start Subtract Multiplicand from the left half of Product and store the result to the left half of Product == 01 == 10 Test Product bit 0 and last time shifted out bit == 00 or 11 Add Multiplicand to the left half of Product and store the result to the left half of Product Shift Product 1-bit arithmetic right Multiplicand & Multiplier are w-bit signed integers. Initially, 2w-bit Product has the Multiplier in its w lower bits and zeros in its w higher bits. w repetitions? g. babic 47 Result in Product Register Yes No Booth s Algorithm: Example A 6-bit (signed) multiplicand 110101 = -11 10 ; Note -110101 = 001011 6-bit (signed) multiplier 011101 = +29 10 Product register 000000 011101 0 assumed for step1. 001011 10 subtract (i.e. add 001011) 001011 011101 shift 000101 101110 1 step 1. ends 110101 01 - add 111010 101110 shift 111101 010111 0 step 2. ends 001011 10 - subtract 001000 010111 shift 000100 001011 1 step 3. ends 11 - no arithmetic shift 000010 000101 1 step 4. ends 11 - no arithmetic shift 000001 000010 1 step 5. ends g. babic Presentation D 48 24

Booth s Algorithm: Example A (continued) shift 000001 000010 1 step 5. ends (repeated line) 110101 01 - add 110110 000010 shift 111011 000001 0 step 6. ends Product = 111011 000001 2 = - 000100 111111 2 = - (256+63) = - 319 10 Note, that Booth s algorithm has nice property that we do not have to worry in advance about signs of operands. It can be shown (by example, see Example B next)) that the algorithm produces a product with incorrect sign if the multiplicand is Tmin = -2 31 ; thus an update to the algorithm should be made for that case. g. babic Presentation D 49 Booth s Algorithm: Example B 6-bit (signed) multiplicand 100000 = -32 10 ; Note -100000 = 1000000 6-bit (signed) multiplier 111101 = -3 10 Product register 000000 111101 0 assumed for step1. 100000 10 subtract (i.e. add 100000) 100000 111101 shift 110000 011110 1 step 1. ends 100000 01 - add 010000 011110 shift 001000 001111 0 step 2. ends 100000 10 - subtract 101000 001111 shift 110100 000111 1 step 3. ends 11 - no arithmetic shift 111010 000011 1 step 4. ends 11 - no arithmetic shift 111101 000001 1 step 5. ends 11 - no arithmetic shift 111110 100000 1 step 6. ends Product = 111110100000 2 = -96 10 instead of +96 10 50 25