Arithmetic and Bitwise Operations on Binary Data

Similar documents
Arithmetic and Bitwise Operations on Binary Data

Manipulating Integers

BITS, BYTES, AND INTEGERS

Systems Programming and Computer Architecture ( )

Lecture 5-6: Bits, Bytes, and Integers

Bits, Bytes, and Integers

Representing and Manipulating Integers. Jo, Heeseung

Bits, Bytes, and Integers Part 2

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

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

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

Integer Encoding and Manipulation

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

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

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

Bits, Bytes and Integers

CS 270: Computer Organization. Integer Arithmetic Operations

Bits, Bytes, and Integers August 26, 2009

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

Integers Sep 3, 2002

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

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

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

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

Bits, Bytes and Integers Part 1

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

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

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

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

CSCI2467: Systems Programming Concepts

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

Bits and Bytes January 13, 2005

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

Computer Systems CEN591(502) Fall 2011

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

Bits, Bytes, and Integers

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

CS429: Computer Organization and Architecture

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

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

Foundations of Computer Systems

Data III & Integers I

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

Data III & Integers I

Data III & Integers I

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Chapter 4. Operations on Data

Integers II. CSE 351 Autumn 2018

Floating Point Numbers

Floating Point Numbers

Data Representation Floating Point

COMP Overview of Tutorial #2

CHW 261: Logic Design

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Data Representation Floating Point

Binary Representations and Arithmetic

JAVA OPERATORS GENERAL

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

COMP2611: Computer Organization. Data Representation

Module 2: Computer Arithmetic

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

7/25/2016. Example: Addition of Unsigned Bit Patterns. ECE 120: Introduction to Computing. Adding Two Non-Negative Patterns Can Overflow

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

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

ECE260: Fundamentals of Computer Engineering

Arithmetic Processing

Computer Organization: A Programmer's Perspective

Hardware: Logical View

Bitwise Data Manipulation. Bitwise operations More on integers

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Bits, Words, and Integers

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

Number Systems and Computer Arithmetic

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

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

CS 261 Fall Mike Lam, Professor Integer Encodings

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

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

Integer Representation

COMP 122/L Lecture 2. Kyle Dewey

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

Lecture 8: Addition, Multiplication & Division

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

9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System

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

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

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Lecture 9. INC and DEC. INC/DEC Examples ADD. Arithmetic Operations Overflow Multiply and Divide

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

COMPUTER ARITHMETIC (Part 1)

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

Inf2C - Computer Systems Lecture 2 Data Representation

Chapter 5 : Computer Arithmetic

Arithmetic for Computers

Floating Point. CSE 238/2038/2138: Systems Programming. Instructor: Fatma CORUT ERGİN. Slides adapted from Bryant & O Hallaron s slides

Data Representation 1

Transcription:

Arithmetic and Bitwise Operations on Binary Data CSCI 2400: Computer Architecture ECE 3217: Computer Architecture and Organization Instructor: David Ferry Slides adapted from Bryant & O Hallaron s slides by Jason Fritts 1

Arithmetic and Bitwise Operations Operations Bitwise AND, OR, NOT, and XOR Logical AND, OR, NOT Shifts Complements Arithmetic Unsigned addition Signed addition Unsigned/signed multiplication Unsigned/signed division 2

Basic Processor Organization Register file (active data) We ll be a lot more specific later Arithmetic Logic Unit (ALU) Performs signed and unsigned arithmetic Performs logic operations Performs bitwise operations Many other structures CPU Register File ALU Memory 3

Boolean Algebra Developed by George Boole in 19th Century Algebraic representation of logic Encode True as 1 and False as 0 And Or A&B = 1 when both A=1 and B=1 A B = 1 when either A=1 or B=1 Not ~A = 1 when A=0 Exclusive-Or (Xor) A^B = 1 when either A=1 or B=1, but not both 4

General Boolean Algebras Operate on Bit Vectors Operations applied bitwise Bitwise-AND operator: Bitwise-NOR operator: Bitwise-XOR operator: Bitwise-NOT operator: ~ & ^ 01101001 & 01010101 01000001 01101001 01010101 01111101 01101001 ^ 01010101 00111100 ~ 01010101 01000001 01111101 00111100 10101010 All of the Properties of Boolean Algebra Apply 5

Quick Check Operate on Bit Vectors Operations applied bitwise Bitwise-AND operator: Bitwise-NOR operator: Bitwise-XOR operator: Bitwise-NOT operator: ~ & ^ 01100110 & 00101111 01000001 11110000 01010101 0 01101001 ^ 00001111 00111100 ~ 00101111 00100110 11110101 01100110 10101010 11010000 All of the Properties of Boolean Algebra Apply 6

Bit-Level Operations in C Operations &,, ~, ^ Available in C Apply to any integral data type long, int, short, char, unsigned View arguments as bit vectors Arguments applied bit-wise Examples (char data type): in hexadecimal in binary ~0x41 0xBE // ~010000012 101111102 ~0x00 0xFF // ~000000002 111111112 0x69 & 0x55 0x41 // 011010012 & 010101012 010000012 0x69 0x55 0x7D // 011010012 010101012 011111012 7

Contrast: Logic Operations in C Contrast to Logical Operators &&,,! View 0 as False Anything nonzero as True Always return 0 or 1 Early termination Examples (char data type):!0x41 0x00!0x00 0x01!!0x41 0x01 0x69 && 0x55 0x01 0x69 0x55 0x01 p && *p // avoids null pointer access 8

Bitwise Operations: Applications Bit fields One byte can fit up to eight options in a single field Example: char flags = 0x1 0x4 0x8 = 000011012 Test for a flag: if ( flags & 0x4 ){ //bit 3 is set } else { //bit 3 was not set } 9

Shift Operations Left Shift: x << y Shift bit-vector x left y places 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 Replicate most significant bit on right Undefined Behavior Shift amount < 0 or word size Argument x 01100010 << 3 00010000 Log. >> 2 00011000 Arith. >> 2 00011000 Argument x 10100010 << 3 00010000 Log. >> 2 00101000 Arith. >> 2 11101000 10

Quick Check Left Shift: x << y Shift bit-vector x left y places 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 Replicate most significant bit on right Undefined Behavior Shift amount < 0 or word size Argument x 00110011 << 3 00010000 Log. >> 4 00011000 Arith. >> 3 00011000 Argument x 1111111 << 3 00010000 Log. >> 4 00101000 Arith. >> 3 11101000 11

Bitwise-NOT: One s Complement Bitwise-NOT operation: ~ Bitwise-NOT of x is ~x Flip all bits of x to compute ~x flip each 1 to 0 flip each 0 to 1 Complement Given x == 10011101 x: 1 0 0 1 1 1 0 1 Flip bits (one s complement): ~x: 0 1 1 0 0 0 1 0 12

Signed Integer Negation: Two s Complement Negate a number by taking 2 s Complement Flip bits (one s complement) and add 1 ~x + 1 == -x Negation (Two s Complement): Given x == 10011101 x: 1 0 0 1 1 1 0 1 Flip bits (one s complement): Add 1: ~x: 0 1 1 0 0 0 1 0 + 1 -x: 0 1 1 0 0 0 1 1 13

Complement & Increment Examples x = 15213 Decimal Hex Binary x 15213 3B 6D 00111011 01101101 ~x -15214 C4 92 11000100 10010010 ~x+1-15213 C4 93 11000100 10010011 x = 0 Decimal Hex Binary 0 0 00 00 00000000 00000000 ~0-1 FF FF 11111111 11111111 ~0+1 0 00 00 00000000 00000000 14

Arithmetic and Bitwise Operations Operations Bitwise AND, OR, NOT, and XOR Logical AND, OR, NOT Shifts Complements Arithmetic Unsigned addition Signed addition Unsigned/signed multiplication Unsigned/signed division 15

Unsigned Addition Operands: w bits True Sum: w+1 bits Discard Carry: w bits u + v u + v Addition Operation Carry output dropped at end of addition Valid ONLY if true sum is within w-bit range 16

Unsigned Addition Operands: w bits True Sum: w+1 bits Discard Carry: w bits u + v u + v Addition Operation Carry output dropped at end of addition Valid ONLY if true sum is within w-bit range Example #1: 1 1 0 1 1 0 0 0 1 0 98 10 + 0 0 1 0 0 1 0 1 0 74 10 1 0 1 0 1 1 0 0 172 10 Valid in 8-bit unsigned range 17

Unsigned Addition Example #2: 1 + 1 1 1 1 1 0 1 1 0 1 1 1 0 110 10 1 1 0 0 1 0 1 0 202 10 0 0 1 1 1 0 0 0 56 10 Not Valid in 8-bit unsigned range (312 is > 255) 18

Unsigned Addition Example #2: 1 + 1 Example #3: 1 1 1 1 0 1 1 0 1 1 1 0 110 10 1 1 0 0 1 0 1 0 202 10 0 0 1 1 1 0 0 0 56 10 Not Valid in 8-bit unsigned range (312 is > 255) + 1 1 1 1 1 1 1 0 0 1 0 0 1 1 1 1 1 1 0 1 0 1 0 0 0 0 1 0 0 0 1 1 1 0 1 1 0 0 0 1 0 10082 10 0 1 0 0 1 0 1 0 59978 10 1 0 1 0 1 1 0 0 4524 10 Not Valid in 16-bit unsigned range (70060 is > 65535) 19

Visualizing True Sum (Mathematical) Addition Integer Addition 4-bit integers u, v Compute true sum Values increase linearly with u and v Forms planar surface 32 28 True Sum 24 20 16 12 8 4 0 0 2 u 4 6 8 10 12 14 0 2 4 6 8 10 v 12 14 20

Visualizing Unsigned Addition Wraps Around If true sum 2 w At most once Overflow True Sum 2 w+1 Overflow 16 14 12 10 2 w 0 Modular Sum 8 6 4 2 0 0 u 2 4 6 8 10 12 14 0 2 4 6 8 10 12 v 14 21

Two s Complement Addition Operands: w bits True Sum: w+1 bits Discard Carry: w bits u + v u + v Signed/Unsigned adds 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 Will give s == t 22

Signed Addition Example #1: + 0 1 1 0 1 1 0 0 0 1 0 98 10 0 1 0 0 1 0 1 0 74 10 1 0 1 0 1 1 0 0-84 10 Note: Same bytes as for Ex #1 and Ex #2 in unsigned integer addition, but now interpreted as 8-bit signed integers Not Valid in 8-bit signed range (172 > 127) Example #2: + 1 1 1 1 1 1 0 1 1 0 1 1 1 0 110 10 1 1 0 0 1 0 1 0-54 10 0 0 1 1 1 0 0 0 56 10 Valid in 8-bit signed range (-128 < 56 < 127) 23

Visualizing Signed Addition Negative Overflow Values 4-bit two s comp. Range from -8 to +7 Wraps Around If sum 2 w 1 Becomes negative At most once If sum < 2 w 1 Becomes positive At most once 8 6 4 2 0-2 -4-6 -8-8 -6-4 -2 u 0 2 4 6-8 -6-2 -4 6 4 2 0 v Positive Overflow 24

Multiplication Goal: Computing Product of w-bit numbers x, y Either signed or unsigned But, exact results can be bigger than w bits Unsigned: up to 2w bits Result range: 0 x * y (2 w 1) 2 = 2 2w 2 w+1 + 1 Two s complement min (negative): Up to 2w-1 bits Result range: x * y ( 2 w 1 )*(2 w 1 1) = 2 2w 2 + 2 w 1 Two s complement max (positive): Up to 2w bits, but only for (SMin w ) 2 Result range: x * y ( 2 w 1 ) 2 = 2 2w 2 So, maintaining exact results would need to keep expanding word size with each product computed is done in software, if needed e.g., by arbitrary precision arithmetic packages 25

Unsigned Multiplication in C Operands: w bits * u v True Product: 2*w bits u v Discard w bits: w bits Standard Multiplication Function Ignores high order w bits Implements Modular Arithmetic machine(u v) = true(u v) mod 2 w 26

Signed Multiplication in C Operands: w bits True Product: 2*w bits u v * u v Discard w bits: w bits Standard Multiplication Function Ignores high order w bits Some of which are different for signed vs. unsigned multiplication Lower bits are the same 27

True Binary Multiplication Multiply positive integers using the same place-value algorithm you learned in grade school X 123 10 234 10 492 3690 + 24600 28782 10 28

True Binary Multiplication Multiply positive integers using the same place-value algorithm you learned in grade school X 123 10 234 10 492 3690 + 24600 X 0 1 1 1 1 0 1 1 123 10 1 1 1 0 1 0 1 0 234 10 28782 10 29

True Binary Multiplication Multiply positive integers using the same place-value algorithm you learned in grade school X 123 10 234 10 492 3690 + 24600 X 0 1 1 1 1 0 1 1 123 10 1 1 1 0 1 0 1 0 234 10 0 0 0 0 0 0 0 0 28782 10 30

True Binary Multiplication Multiply positive integers using the same place-value algorithm you learned in grade school X 123 10 234 10 492 3690 + 24600 X 0 1 1 1 1 0 1 1 123 10 1 1 1 0 1 0 1 0 234 10 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 28782 10 31

True Binary Multiplication Multiply positive integers using the same place-value algorithm you learned in grade school X 123 10 234 10 492 3690 + 24600 X 0 1 1 1 1 0 1 1 123 10 1 1 1 0 1 0 1 0 234 10 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 28782 10 32

True Binary Multiplication Multiply positive integers using the same place-value algorithm you learned in grade school X 123 10 234 10 492 3690 + 24600 X 0 1 1 1 1 0 1 1 123 10 1 1 1 0 1 0 1 0 234 10 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 28782 10 33

True Binary Multiplication Multiply positive integers using the same place-value algorithm you learned in grade school X 123 10 234 10 492 3690 + 24600 28782 10 + X 0 1 1 1 1 0 1 1 123 10 1 1 1 0 1 0 1 0 234 10 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 0 1 1 0 1 1 1 0 28782 10 34

Power-of-2 Multiply with Shift Consider: 6 10 * 2 10 = 12 10 X 0 1 1 0 6 10 0 0 1 0 2 10 35

Power-of-2 Multiply with Shift Consider: 6 10 * 2 10 = 12 10 + 0 1 1 0 6 10 X 0 0 1 0 2 10 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 12 10 36

Power-of-2 Multiply with Shift Consider: 6 10 * 2 10 = 12 10 + 0 1 1 0 6 10 X 0 0 1 0 2 10 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 12 10 Multiplying by two always shifts the input bit pattern by one to the left. That is: (6 10 * 2 10 ) == (0110 2 << 1) More generally- multiplying by 2 k always shifts the input by k to the left: (x 10 * 2 k ) == (x 2 << k) 37

Power-of-2 Multiply with Shift Operation u << k gives u * 2 k Both signed and unsigned Operands: w bits True Product: w+k bits Discard k bits: w bits u 2 k Examples u << 3 == u * 8 (u << 5) (u << 3) == u * 24 Most machines shift and add faster than multiply Compiler generates this code automatically * u 2 k 0 0 1 0 0 0 0 0 0 k 0 0 0 38

Unsigned Power-of-2 Divide with Shift Quotient of Unsigned by Power of 2 u >> k gives u / 2 k Uses logical shift k u Operands: / 2 k Division: Result: u / 2 k u / 2 k 0 0 1 0 0 0 0 0 0 0 0 0. Binary Point 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 39

Signed Power-of-2 Divide with Shift Quotient of Signed by Power of 2 x >> k gives x / 2 k Uses arithmetic shift Rounds wrong direction 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 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 40

Incorrect Power-of-2 Divide Consider: -25 / 2 We expect that -25 / 2 = -12, however: 1. -25 10 = 11100111 2 2. (-25 / 2 ) becomes (11100111 2 >> 1) 3. (11100111 2 >> 1) = 11110011 2 4. 11110011 2 = -13 41

Correct Power-of-2 Divide with Biasing Quotient of Negative 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 Dividend s low bits are zero Case 1: No rounding Dividend: Divisor: u +2 k 1 / 2 k u / 2 k 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 Biasing has no effect 42

Biasing without changing result Consider: -20 / 4 (answer should be -5) Without bias: 1. -20 10 = 11101100 2 2. (-20 / 4 ) becomes (11101100 2 >> 2) 3. (11101100 2 >> 2) = 11111011 2 4. 11111011 2 = -5 With bias: 1. -20 10 + 3 10 = 11101111 2 2. (-23 / 4 ) becomes (11101111 2 >> 2) 3. (11101111 2 >> 2) = 11111011 2 4. 11111011 2 = -5 43

Correct Power-of-2 Divide (Cont.) Case 2: Rounding Dividend: k 1 x +2 k 1 0 0 0 1 1 1 1 Nonzero low bits Incremented by 1 Binary Point Divisor: 2 k / x / 2 k 0 0 1 0 0 0 01 1 1 1. Biasing adds 1 to final result Incremented by 1 44

Biasing that does change the result Consider: -21 / 4 (answer should be -5) Without bias: 1. -21 10 = 11101011 2 2. (-21 / 4 ) becomes (11101011 2 >> 2) 3. (11101011 2 >> 2) = 11111010 2 4. 11111010 2 = -6 (incorrect!) With bias: 1. -21 10 + 3 10 = 11101110 2 2. (-18 / 4 ) becomes (11101110 2 >> 2) 3. (11101110 2 >> 2) = 11111011 2 4. 11111011 2 = -5 45

Biasing that does change the result Consider: -21 / 4 (answer should be -5) Without bias: 1. -21 10 = 11101011 2 2. (-21 / 4 ) becomes (11101011 2 >> 2) 3. (11101011 2 >> 2) = 11111010 2 4. 11111010 2 = -6 (incorrect!) With bias: Recall- lowest order bit has value 1! 1. -21 10 + 3 10 = 11101110 2 2. (-18 / 4 ) becomes (11101110 2 >> 2) 3. (11101110 2 >> 2) = 11111011 2 4. 11111011 2 = -5 46

Arithmetic: Basic Rules Unsigned ints, 2 s complement ints are isomorphic rings: isomorphism = casting Left shift Unsigned/signed: multiplication by 2 k Always logical shift Right shift Unsigned: logical shift, div (division + round to zero) by 2 k Signed: arithmetic shift Positive numbers: div (division + round to zero) by 2 k Negative numbers: div (division + round away from zero) by 2 k Use biasing to fix 47