COMP 122/L Lecture 2. Kyle Dewey

Similar documents
CS 64 Week 1 Lecture 1. Kyle Dewey

CS 64 Week 0 Lecture 1. Kyle Dewey

Chapter 4. Operations on Data

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

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

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

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

Chapter 3: part 3 Binary Subtraction

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

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

COMP2611: Computer Organization. Data Representation

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

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

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

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

Number System. Introduction. Decimal Numbers

Bits, Words, and Integers

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

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

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

EE 109 Unit 6 Binary Arithmetic

Arithmetic for Computers

Chapter 3: Arithmetic for Computers

Number Systems CHAPTER Positional Number Systems

Arithmetic and Bitwise Operations on Binary Data

Introduction to Computers and Programming. Numeric Values

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

Lecture 8: Addition, Multiplication & Division

4. Number Representations

Chapter 10 - Computer Arithmetic

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

Chapter 10 Binary Arithmetics

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

Advanced Computer Architecture-CS501

CS 261 Fall Mike Lam, Professor Integer Encodings

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

BITWISE OPERATORS. There are a number of ways to manipulate binary values. Just as you can with

Lecture 12 Integers. Computer and Network Security 19th of December Computer Science and Engineering Department

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

CS33 Project Gear Up. Data

Inf2C - Computer Systems Lecture 2 Data Representation

Binary Adders: Half Adders and Full Adders

World Inside a Computer is Binary

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

Chapter 3: Operators, Expressions and Type Conversion

Arithmetic Operations

INF2270 Spring Philipp Häfliger. Lecture 4: Signed Binaries and Arithmetic


CO212 Lecture 10: Arithmetic & Logical Unit

Logic, Words, and Integers

Octal and Hexadecimal Integers

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

CHAPTER 5: Representing Numerical Data

Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

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

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

Arithmetic and Bitwise Operations on Binary Data

CS & IT Conversions. Magnitude 10,000 1,

carry in carry 1101 carry carry

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

CS/COE0447: Computer Organization

Computer Architecture and Organization

CS/COE0447: Computer Organization

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

Binary Arithmetic Intro to Assembly Language CS 64: Computer Organization and Design Logic Lecture #3

Computer Organization

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

SIGNED AND UNSIGNED SYSTEMS

Chapter 5 : Computer Arithmetic

Number Systems and Their Representations

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Module 2: Computer Arithmetic

Representation of Non Negative Integers

Chapter 1 Review of Number Systems

Number Systems and Computer Arithmetic

CS101 Lecture 04: Binary Arithmetic

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

1 Computer arithmetic with unsigned integers

Arithmetic Processing

Binary Values. CSE 410 Lecture 02

Lecture 2: Number Systems

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

Bitwise Instructions

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

EEM336 Microprocessors I. Arithmetic and Logic Instructions

Chapter Three. Arithmetic

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Slide Set 1. for ENEL 339 Fall 2014 Lecture Section 02. Steve Norman, PhD, PEng

Computer Arithmetic Floating Point

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

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

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

Part 2,Number Systems Questions

Chapter 4 Arithmetic Functions

Numerical computing. How computers store real numbers and the problems that result

Numeric Encodings Prof. James L. Frankel Harvard University

Number Systems. Both numbers are positive

Lecture 6: Signed Numbers & Arithmetic Circuits. BCD (Binary Coded Decimal) Points Addressed in this Lecture

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

Transcription:

COMP 122/L Lecture 2 Kyle Dewey

Outline Operations on binary values AND, OR, XOR, NOT Bit shifting (left, two forms of right) Addition Subtraction Twos complement

Bitwise Operations

Bitwise AND Similar to logical AND (&&), except it works on a bit-by-bit manner Denoted by a single ampersand: & (1001 & 0101)= 0001

Bitwise OR Similar to logical OR ( ), except it works on a bit-by-bit manner Denoted by a single pipe character: (1001 0101)= 1101

Bitwise XOR Exclusive OR, denoted by a carat: ^ Similar to bitwise OR, except that if both inputs are 1 then the result is 0 (1001 ^ 0101)= 1100

Bitwise NOT Similar to logical NOT (!), except it works on a bit-by-bit manner Denoted by a tilde character: ~ ~1001 = 0110

Shift Left Move all the bits N positions to the left, subbing in N 0s on the right

Shift Left Move all the bits N positions to the left, subbing in N 0s on the right 1001

Shift Left Move all the bits N positions to the left, subbing in N 0s on the right 1001 << 2 = 100100

Shift Left Useful as a restricted form of multiplication Question: how? 1001 << 2 = 100100

Shift Left as Multiplication Equivalent decimal operation: 234

Shift Left as Multiplication Equivalent decimal operation: 234 << 1 = 2340

Shift Left as Multiplication Equivalent decimal operation: 234 << 1 = 2340 234 << 2 = 23400

Multiplication Shifting left N positions multiplies by (base) N Multiplying by 2 or 4 is often necessary (shift left 1 or 2 positions, respectively) Often a whooole lot faster than telling the processor to multiply Compilers try hard to do this 234 << 2 = 23400

Shift Right Move all the bits N positions to the right, subbing in either N 0s or N 1s on the left Two different forms

Shift Right Move all the bits N positions to the right, subbing in either N 0s or N (whatever the leftmost bit is)s on the left Two different forms 1001 >> 2 = either 0010 or 1110

Shift Right Trick Question: If shifting left multiplies, what does shift right do?

Shift Right Trick Question: If shifting left multiplies, what does shift right do? Answer: divides in a similar way, but truncates result

Shift Right Trick Question: If shifting left multiplies, what does shift right do? Answer: divides in a similar way, but truncates result 234

Shift Right Trick Question: If shifting left multiplies, what does shift right do? Answer: divides in a similar way, but truncates result 234 >> 1 = 23

Two Forms of Shift Right Subbing in 0s makes sense What about subbing in the leftmost bit? And why is this called arithmetic shift right? 1100 (arithmetic)>> 1 = 1110

Answer...Sort of Arithmetic form is intended for numbers in twos complement, whereas the nonarithmetic form is intended for unsigned numbers

Twos Complement

Problem Binary representation so far makes it easy to represent positive numbers and zero Question: What about representing negative numbers?

Twos Complement Way to represent positive integers, negative integers, and zero If 1 is in the most significant bit (generally leftmost bit in this class), then it is negative

Decimal to Twos Complement Example: -5 decimal to binary (twos complement)

Decimal to Twos Complement Example: -5 decimal to binary (twos complement) First, convert the magnitude to an unsigned representation

Decimal to Twos Complement Example: -5 decimal to binary (twos complement) First, convert the magnitude to an unsigned representation 5 (decimal) = 0101 (binary)

Decimal to Twos Complement Then, take the bits, and negate them

Decimal to Twos Complement Then, take the bits, and negate them 0101

Decimal to Twos Complement Then, take the bits, and negate them ~0101 = 1010

Decimal to Twos Complement Finally, add one:

Decimal to Twos Complement Finally, add one: 1010

Decimal to Twos Complement Finally, add one: 1010 + 1 = 1011

Twos Complement to Decimal Same operation: negate the bits, and add one

Twos Complement to Decimal Same operation: negate the bits, and add one 1011

Twos Complement to Decimal Same operation: negate the bits, and add one ~1011 = 0100

Twos Complement to Decimal Same operation: negate the bits, and add one 0100

Twos Complement to Decimal Same operation: negate the bits, and add one 0100 + 1 = 0101

Twos Complement to Decimal Same operation: negate the bits, and add one 0100 + 1 = We started with 1011 - negative 0101 = -5

Intuition Modular arithmetic, with the convention that a leading 1 bit means negative 000 Denotes +1 111 110 001 010 101 011 100 -This is the intuition from Wikipedia, which makes a whole lot more sense

Intuition Modular arithmetic, with the convention that a leading 1 bit means negative (zero) 000 Denotes +1 (least -) 111 110 001 010 (least +) 101 011 (most +) 100 (most -) -This is the intuition from Wikipedia, which makes a whole lot more sense -There is still a lot of detail missing here - it s not necessary to understand in order to work with this. There is actually quite a bit of mathematics behind why this works

Intuition Modular arithmetic, with the convention that a leading 1 bit means negative 0 000 Denotes +1-1 111-2 110 001 1 010 2-3 101 011 3 100-4 -This is the intuition from Wikipedia, which makes a whole lot more sense -There is still a lot of detail missing here - it s not necessary to understand in order to work with this. There is actually quite a bit of mathematics behind why this works

Negation of 1 000 111 001 110 010 101 011 100 -Take our wheel from before

Negation of 1 000 111 001 110 010 101 011 100 -This is 1

Negation of 1 000 111 001 110 010 101 011 100 -Inverted bits

Negation of 1 000 111 001 110 010 101 011 100 -Add 1 -This is exactly what we expected - binary 111 represents decimal -1

Consequences What is the negation of 000? 000 111 001 110 010 101 011 100 -Negate all bits: 000 -> 111 -Add one: 000 -Technically, adding one resulted in 1000, but that got cut off

Consequences What is the negation of 100? 000 111 001 110 010 101 011 100 -Negate all bits: 100 -> 011 -Add one: 100 -Uh oh...this states that the negation of -4 is -4. -Underlying problem is that we don t have a representation for 4 with just three bits

-101 (-3) shifted right yields 110 (-2), NOT 111 (-1) as expected from typical integer division -Integer division rounds towards zero, whereas shift right rounds towards negative infinity -This means they work _identically_ for positive values, but not for negative values (also meaning they are always the same for _unsigned_ values) Arithmetic Shift Right Not exactly division by a power of two Consider -3 / 2 0 000-1 111 001 1-2 110 010 2-3 101 011 3 100-4

Addition

Building Up Addition Question: how might we add the following, in decimal? 986 +123 ----?

Building Up Addition Question: how might we add the following, in decimal? 986 +123 ----? 6 +3 --?

Building Up Addition Question: how might we add the following, in decimal? 986 +123 ----? 8 +2 --? 6 +3 -- 9

Building Up Addition Question: how might we add the following, in decimal? 986 +123 ----? Carry: 1 8 +2 -- 0 6 +3 -- 9

Building Up Addition Question: how might we add the following, in decimal? 986 +123 ----? 1 9 +1 --? 8 +2 -- 0 6 +3 -- 9

Building Up Addition Question: how might we add the following, in decimal? 986 +123 ----? Carry: 1 1 8 6 9 +2 +3 +1 -- -- -- 0 9 1

Building Up Addition Question: how might we add the following, in decimal? 986 +123 ----? 1 +0 -- 1 1 9 +1 -- 1 8 +2 -- 0 6 +3 -- 9

Core Concepts We have a primitive notion of adding single digits, along with an idea of carrying digits We can build on this notion to add numbers together that are more than one digit long

Now in Binary Arguably simpler - fewer one-bit possibilities 0 0 1 1 +0 +1 +0 +1 -- -- -- --????

Now in Binary Arguably simpler - fewer one-bit possibilities 0 0 1 1 +0 +1 +0 +1 -- -- -- -- 0 1 1 0 Carry: 1

Chaining the Carry Also need to account for any input carry 0 0 0 0 0 0 1 1 +0 +1 +0 +1 -- -- -- -- 0 1 1 0 Carry: 1 1 1 1 1 0 0 1 1 +0 +1 +0 +1 -- -- -- -- 1 0 Carry: 1 0 Carry: 1 1 Carry: 1

Adding Multiple Bits How might we add the numbers below? 011 +001 ------

Adding Multiple Bits How might we add the numbers below? 0 011 +001 ------ -Need an initial carry-in of zero

Adding Multiple Bits How might we add the numbers below? 10 011 +001 ------ 0 -Need an initial carry-in of zero

Adding Multiple Bits How might we add the numbers below? 110 011 +001 ------ 00 -Need an initial carry-in of zero

Adding Multiple Bits How might we add the numbers below? 0110 011 +001 ------ 100 -Need an initial carry-in of zero

Adding Multiple Bits How might we add the numbers below? 0110 011 +001 ------ 100 Output Carry Bit Result Bits -Need an initial carry-in of zero

Another Example 111 +001 ------

Another Example 0 111 +001 ------

Another Example 10 111 +001 ------ 0

Another Example 110 111 +001 ------ 00

Another Example 1110 111 +001 ------ 000 Output Carry Bit Result Bits -Now we have an output carry bit of 1. What does this mean?

Output Carry Bit Significance For unsigned numbers, it indicates if the result did not fit all the way into the number of bits allotted May be an error condition for software

Signed Addition Question: what is the result of the following operation? 011 +011 ----?

Signed Addition Question: what is the result of the following operation? 011 +011 ---- 0110 -If these are treated as signed numbers in two s complement, then we need a leading 0 to indicate that this is a positive number -Truncated to three bits, the result is a negative number!

Overflow In this situation, overflow occurred: this means that both the operands had the same sign, and the result s sign differed 011 +011 ---- 110 Possibly a software error

Overflow vs. Carry These are different ideas Carry is relevant to unsigned values Overflow is relevant to signed values 111 011 111 001 +001 +011 +100 +001 ---- ---- ---- ---- 000 110 011 010 No Overflow; Overflow; Overflow; No Overflow; Carry No Carry Carry No Carry -As to when is it a problem, this all depends on exactly what it is you re doing

Subtraction

Subtraction Have been saying to invert bits and add one to second operand Could do it this way in hardware, but there is a trick 001-001 ----? Hint: these two questions are equivalent 001 +111 ----?

Subtraction Trick Assume we can cheaply invert bits, but we want to avoid adding twice (once to add 1 and once to add the other result) How can we do this easily?

Subtraction Trick Assume we can cheaply invert bits, but we want to avoid adding twice (once to add 1 and once to add the other result) How can we do this easily? Set the initial carry to 1 instead of 0

Subtraction Example 0101-0011 ----

Subtraction Example 0101-0011 Invert 0011 ----

Subtraction Example 0101-0011 ---- Invert 0011 1100

Subtraction Example 0101-0011 ---- Invert 0011 Equivalent to 1100

Subtraction Example 1 0101 Invert 0011 Equivalent to 0101-0011 1100 +1100 ---- ---- -An initial carry-in of 1 is equivalent to adding 1 and then adding the other operand

Subtraction Example 11011 0101 Invert 0011 Equivalent to 0101-0011 1100 +1100 ---- ---- 0010 -An initial carry-in of 1 is equivalent to adding 1 and then adding the other operand