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

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

More about Binary 9/6/2016

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

Binary Representations and Arithmetic

CS101 Lecture 04: Binary Arithmetic

Goals for this Week. CSC 2400: Computer Systems. Bits, Bytes and Data Types. Binary number system. Finite representations of binary integers

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Number System. Introduction. Decimal Numbers

Arithmetic and Bitwise Operations on Binary Data

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

Inf2C - Computer Systems Lecture 2 Data Representation

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

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

Fundamentals of Programming Session 2

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

World Inside a Computer is Binary

Signed umbers. Sign/Magnitude otation

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

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

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

COMP 122/L Lecture 2. Kyle Dewey

Representation of Non Negative Integers

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Part 2,Number Systems Questions

The Design of C: A Rational Reconstruction

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

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

The Design of C: A Rational Reconstruction"

CS 64 Week 1 Lecture 1. Kyle Dewey

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

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

CS 261 Fall Mike Lam, Professor Integer Encodings

COMP2611: Computer Organization. Data Representation

Binary Adders: Half Adders and Full Adders

Integer Representation

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

Integers. N = sum (b i * 2 i ) where b i = 0 or 1. This is called unsigned binary representation. i = 31. i = 0

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

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College January 27, 2015

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College September 6, 2018

Chapter 10 Binary Arithmetics

Bits, Words, and Integers

Chapter Three. Arithmetic

Chapter 3: Arithmetic for Computers

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Number systems and binary

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

Arithmetic and Bitwise Operations on Binary Data

Chapter 3: part 3 Binary Subtraction

Arithmetic Processing

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Chapter 2 Bits, Data Types, and Operations

Chapter 4: Data Representations

COMP Overview of Tutorial #2

EE 109 Unit 6 Binary Arithmetic

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

Numerical Representations On The Computer: Negative And Rational Numbers

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

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

Data III & Integers I

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

Topic Notes: Bits and Bytes and Numbers

Chapter 2 Bits, Data Types, and Operations

Binary Addition. Add the binary numbers and and show the equivalent decimal addition.

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

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS).

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Number Systems and Number Representation

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

SIGNED AND UNSIGNED SYSTEMS

More Programming Constructs -- Introduction

Chapter 4. Operations on Data

Advanced Computer Architecture-CS501

The Design of C: A Rational Reconstruction

COMP2121: Microprocessors and Interfacing. Number Systems

Topic Notes: Bits and Bytes and Numbers

Number Systems (2.1.1)

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Binary Arithmetic. Daniel Sanchez Computer Science & Artificial Intelligence Lab M.I.T.

Number Systems and Number Representation

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

Integer primitive data types

Data Representation 1

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

CS61B Lecture #14: Integers

Chapter 2 Bits, Data Types, and Operations

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

Number Representations

Computer Organization

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Principles of Computer Architecture. Chapter 3: Arithmetic

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

The Design of C: A Rational Reconstruction" Jennifer Rexford!

Floating Point Arithmetic

Math in MIPS. Subtracting a binary number from another binary number also bears an uncanny resemblance to the way it s done in decimal.

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Chapter 2 Bits, Data Types, and Operations

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Transcription:

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

WiCS! Swarthmore Women in Computer Science Slide 2

Today Binary Arithmetic Unsigned addition Subtraction Representation Signed magnitude Two s complement Signed overflow Bit operations Slide 3

Reading Quiz

Abstraction User / Programmer Wants low complexity Applications Specific functionality Software library Reusable functionality Operating system Manage resources Complex devices Compute & I/O Slide 5

Last Class: Binary Digits: (BITS) Most significant bit 7 6 5 4 3 2 1 0 10001111 Least significant bit Representation: 1 x 2 7 + 0 x 2 6.. + 1 x 2 3 + 1 x 2 2 + 1 x 2 1 + 1 x 2 0 10001111 = 143 one byte is the smallest addressable unit - contains 8 bits Slide 6

Last Class: Unsigned Integers Suppose we had one byte Can represent 2 8 (256) values If unsigned (strictly non-negative): 0 255 What if we add one more? Slide 7

C types and their (typical!) sizes 1 byte (8 bits = 2 8 unique values): char, unsigned char 2 bytes (16 bits = 2 16 unique values): short, unsigned short 4 bytes (32 bits = 2 32 unique values): int, unsigned int, float 8 bytes (64 bits = 2 64 unique values): long long, unsigned long long, double 4 or 8 bytes: long, unsigned long unsigned long v1; short s1; long long ll; Slide 8

Last Class: Unsigned Integers Suppose we had one byte Can represent 2 8 (256) values If unsigned (strictly non-negative): 0 255 252 = 11111100 253 = 11111101 254 = 11111110 255 = 11111111 What if we add one more? Traditional number line: Addition 0 255 Larger Values Slide 9

Last Class: Unsigned Integers Suppose we had one byte Can represent 2 8 (256) values If unsigned (strictly non-negative): 0 255 252 = 11111100 253 = 11111101 254 = 11111110 255 = 11111111 What if we add one more? Car odometer rolls over. Any time we are dealing with a finite storage space we cannot represent an infinite number of values! Slide 10

Last Class: Unsigned Integers Suppose we had one byte Can represent 2 8 (256) values If unsigned (strictly non-negative): 0 255 255 (11111111) 0 Addition 252 = 11111100 253 = 11111101 254 = 11111110 192 64 255 = 11111111 What if we add one more? 128 (10000000) Modular arithmetic: Here, all values are modulo 256. Slide 11

Last Class: Unsigned Addition (4-bit) Addition works like grade school addition: 1 0110 6 1100 12 + 0100 + 4 + 1010 +10 1010 10 1 0110 6 ^carry out Four bits give us range: 0-15 Slide 12

Last Class: Unsigned Addition (4-bit) Addition works like grade school addition: 1 0110 6 1100 12 + 0100 + 4 + 1010 +10 1010 10 1 0110 6 ^carry out Four bits give us range: 0-15 Overflow! Slide 13

Suppose we want to support signed values (positive and negative) in 8 bits, where should we put -1 and -127 on the circle? Why? -127 (11111111) 0-1 (11111111) 0 A B -1-127 C: Put them somewhere else. Slide 14

Suppose we want to support signed values (positive and negative) in 8 bits, where should we put -1 and -127 on the circle? Why? -127 (11111111) 0-1 (11111111) 0 A: signed magnitude B: Two s complement -1-127 C: Put them somewhere else. Slide 15

Signed Magnitude One bit (usually left-most) signals: 0 for positive 1 for negative -127 0 1 For one byte: 1 = 00000001, -1 = 10000001 A Pros: Negation (negative value of a number) is very simple! -1 Slide 16

Signed Magnitude One bit (usually left-most) signals: 0 for positive 1 for negative -127 1 0 For one byte: 0 = 00000000 What about 10000000? Major con: Two ways to represent zero. -1 A -0 Slide 17

Two s Complement (signed) Borrow nice property from number line: 0-1 1 Only one instance of zero! Implies: -1 and 1 on either side of it. Slide 18

Two s Complement Borrow nice property from number line: 0-1 1 Only one instance of zero! Implies: -1 and 1 on either side of it. -1 1 0 B For an 8 bit range we can express 256 unique values: 128 non-negative values (0 to 127) 128 negative values (-1 to -128) -127 127-128 Slide 19

Two s Complement Only one value for zero With N bits, can represent the range: -2 N-1 to 2 N-1 1 Most significant bit still designates positive (0) /negative (1) Negating a value is slightly more complicated: 1 = 00000001, -1 = 11111111 From now on, unless we explicitly say otherwise, we ll assume all integers are stored using two s complement! This is the standard! Slide 20

Two s Compliment Each two s compliment number is now: [-2 n-1 *d n-1 ] + [2 n-2 *d n-2 ] + + [2 1 *d 1 ] + [2 0 *d 0 ] Note the negative sign on just the most significant bit. This is why first bit tells us whether the value is negative vs. positive. Slide 21

If we interpret 11001 as a two s complement number, what is the value in decimal? Each two s compliment number is now: [-2 n-1 *d n-1 ] + [2 n-2 *d n-2 ] + + [2 1 *d 1 ] + [2 0 *d 0 ] A. -2 B. -7 C. -9 D. -25 Slide 22

If we interpret 11001 as a two s complement number, what is the value in decimal? Each two s compliment number is now: [-2 n-1 *d n-1 ] + [2 n-2 *d n-2 ] + + [2 1 *d 1 ] + [2 0 *d 0 ] A. -2 B. -7-16 + 8 + 1 = -7 C. -9 D. -25 Slide 23

If we interpret What is the decimal value of 1100? as unsigned, 4-bit value: 12 (%u) as signed (two s comp), 4-bit value: -4 (%d) as an 8-bit value: 12. (i.e., 0000 1100) Slide 24

Two s Complement Negation To negate a value x, we want to find y such that x + y = 0. For N bits, y = 2 N - x -1 0 1 B -127 127-128 Slide 25

Negation Example (8 bits) For N bits, y = 2 N - x Negate the value (2) 00000010 2 8-2 = 256-2 = 254 254 255 0-2 -1 0 1 Our wheel only goes to 127! Put -2 where 254 would be if wheel was unsigned. 254 in binary is 11111110-127 B -128 signed 127 unsigned Given 11111110, it s 254 if interpreted as unsigned and -2 interpreted as signed. 128 Slide 26

Negation Shortcut A much easier, faster way to negate: Flip the bits (0 s become 1 s, 1 s become 0 s) Add 1 Negate 00101110 (46) Formally: 2 8-46 = 256-46 = 210 210 in binary is 11010010 46: 00101110 Flip the bits: 11010001 Add 1 + 1-46: 11010010 Slide 27

Addition & Subtraction Addition is the same as for unsigned One exception: different rules for overflow Can use the same hardware for both Subtraction is the same operation as addition Just need to negate the second operand 6-7 = 6 + (-7) = 6 + (~7 + 1) ~7 is shorthand for flip the bits of 7 Slide 28

Subtraction Hardware Negate and add 1 to second operand: Can use the same circuit for add and subtract: 6-7 == 6 + ~7 + 1 input 1 -------------------------------> input 2 --> possible bit flipper --> ADD CIRCUIT ---> result possible +1 input--------> Slide 29

By switching to two s complement, have we solved this value rolling over (overflow) problem? A. Yes, it s gone. -1 0 1 B. Nope, it s still there. B C. It s even worse now. This is an issue we need to be aware of when adding and subtracting! -127-128 127 Slide 30

Slide 31 Overflow, Revisited Danger Zone 255 0-1 0 1 192 Unsigned 64 Signed 128-127 -128 127 Danger Zone

If we add a positive number and a negative number, will we have overflow? (Assume they are the same # of bits) A. Always -1 0 1 B. Sometimes Signed C. Never -127 127-128 Danger Zone Slide 32

Signed Overflow Overflow: IFF the sign bits of operands are the same, but the sign bit of result is different. Not enough bits to store result! Signed addition (and subtraction): 2+-1=1 2+-2=0 2+-4=-2 2+7=-7-2+-7=7 0010 0010 0010 0010 1110 +1111 +1110 +1100 +0111 +1001 1 0001 1 0000 1110 1001 1 0111-1 -127 0 Signed No chance of overflow here - signs -128 of operands are different! Slide 33 1 127

Signed Overflow Slide 34 Overflow: IFF the sign bits of operands are the same, but the sign bit of result is different. Not enough bits to store result! Signed addition (and subtraction): 2+-1=1 2+-2=0 2+-4=-2 2+7=-7-2+-7=7 0010 0010 0010 0010 1110 +1111 +1110 +1100 +0111 +1001 1 0001 1 0000 1110 1001 1 0111 Overflow here! Operand signs are the same, and they don t match output sign!

Overflow Rules Signed: The sign bits of operands are the same, but the sign bit of result is different. Can we formalize unsigned overflow? Need to include subtraction too, skipped it before. Slide 35

Recall Subtraction Hardware Negate and add 1 to second operand: Can use the same circuit for add and subtract: 6-7 == 6 + ~7 + 1 input 1 -------------------------------> input 2 --> possible bit flipper --> ADD CIRCUIT ---> result possible +1 input--------> Let s call this +1 input: Carry in Slide 36

How many of these unsigned operations have overflowed? 4 bit unsigned values (range 0 to 15): carry-in carry-out Addition (carry-in = 0) 9 + 11 = 1001 + 1011 + 0 = 1 0100 9 + 6 = 1001 + 0110 + 0 = 0 1111 3 + 6 = 0011 + 0110 + 0 = 0 1001 Subtraction (carry-in = 1) 6-3 = 0110 + 1100 + 1 = 1 0011 3-6 = 0011 + 1010 + 1 = 0 1101 A. 1 B. 2 C. 3 D. 4 E. 5 (-3) (-6) Slide 37

How many of these unsigned operations have overflowed? Interpret these as 4-bit unsigned values (range 0 to 15): carry-in carry-out Addition (carry-in = 0) 9 + 11 = 1001 + 1011 + 0 = 1 0100 = 4 9 + 6 = 1001 + 0110 + 0 = 0 1111 = 15 3 + 6 = 0011 + 0110 + 0 = 0 1001 = 9 Subtraction (carry-in = 1) (-3) 6-3 = 0110 + 1100 + 1 = 1 0011 = 3 3-6 = 0011 + 1010 + 1 = 0 1101 = 13 (-6) A. 1 B. 2 C. 3 D. 4 E. 5 Pattern? Slide 38

Slide 39 Overflow Rule Summary Signed overflow: The sign bits of operands are the same, but the sign bit of result is different. Unsigned: overflow The carry-in bit is different from the carry-out. C in C out C in XOR C out 0 0 0 0 1 1 1 0 1 1 1 0 So far, all arithmetic on values that were the same size. What if they re different?

Sign Extension When combining signed values of different sizes, expand the smaller to equivalent larger size: char y=2, x=-13; short z = 10; z = z + y; z = z + x; 0000000000001010 0000000000000101 + 00000010 + 11110011 0000000000000010 1111111111110011 Fill in high-order bits with sign-bit value to get same numeric value in larger number of bytes. Slide 40

Let s verify that this works 4-bit signed value, sign extend to 8-bits, is it the same value? 0111 ---> 0000 0111 obviously still 7 1010 ----> 1111 1010 is this still -6? -128 + 64 + 32 + 16 + 8 + 0 + 2 + 0 = -6 yes! Slide 41

Operations on Bits For these, doesn t matter how the bits are interpreted (signed vs. unsigned) Bit-wise operators (AND, OR, NOT, XOR) Bit shifting Slide 42

Bit-wise Operators bit operands, bit result (interpret as you please) & (AND) (OR) ~(NOT) ^(XOR) A B A & B A B ~A A ^ B 0 0 0 0 1 0 0 1 0 1 1 1 1 0 0 1 0 1 1 1 1 1 0 0 01010101 01101010 10101010 ~10101111 00100001 & 10111011 ^ 01101001 01010000 01110101 00101010 11000011 Slide 43

More Operations on Bits Bit-shift operators: << left shift, >> right shift 01010101 << 2 is 01010100 2 high-order bits shifted out 2 low-order bits filled with 0 01101010 << 4 is 10100000 01010101 >> 2 is 00010101 01101010 >> 4 is 00000110 10101100 >> 2 is 00101011 (logical shift) or 11101011 (arithmetic shift) Arithmetic right shift: fills high-order bits w/sign bit C automatically decides which to use based on type: signed: arithmetic, unsigned: logical Slide 44

C programming Up Next!