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

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

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

More about Binary 9/6/2016

CS 31: Intro to Systems C Programming. Kevin Webb Swarthmore College September 13, 2018

Binary Representations and Arithmetic

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

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

Arithmetic and Bitwise Operations on Binary Data

Computer Organization

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

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

Information Science 1

Information Science 1

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Number System. Introduction. Decimal Numbers

ISA 563 : Fundamentals of Systems Programming

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

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

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Signed umbers. Sign/Magnitude otation

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Fundamental of Programming (C)

The Design of C: A Rational Reconstruction

The Design of C: A Rational Reconstruction"

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

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

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

A Java program contains at least one class definition.

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Topic Notes: Bits and Bytes and Numbers

LING 388: Computers and Language. Lecture 5

CS101 Lecture 04: Binary Arithmetic

UNIT- 3 Introduction to C++

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

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

Computer System and programming in C

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

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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

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

Data Representation 1

CSC201, SECTION 002, Fall 2000: Homework Assignment #2

As stated earlier, the declaration

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

Variables and literals

CS 241 Data Organization Binary

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Work relative to other classes

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

The type of all data used in a C (or C++) program must be specified

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

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

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

Fundamentals of Programming Session 2

Topic Notes: Bits and Bytes and Numbers

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

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

CS 261 Fall Mike Lam, Professor Integer Encodings

Course Outline Introduction to C-Programming

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CS Prof J.P.Morrison

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Data Types. Data Types. Integer Types. Signed Integers

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

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

More Programming Constructs -- Introduction

Bits, Words, and Integers

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Inf2C - Computer Systems Lecture 2 Data Representation

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

Expressions and Casting

ECE260: Fundamentals of Computer Engineering

Conditionals and Loops

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

CS 101: Computer Programming and Utilization

COMP2121: Microprocessors and Interfacing. Number Systems

LECTURE 3 C++ Basics Part 2

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

SIGNED AND UNSIGNED SYSTEMS

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

N.B. These pastpapers may rely on the knowledge gained from the previous chapters.

Chapter 3: Operators, Expressions and Type Conversion

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

The Compilation Process

Full file at

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

Beginning C Programming for Engineers

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Chapter 3: Arithmetic for Computers

C and Programming Basics

COMP2611: Computer Organization. Data Representation

The Design of C: A Rational Reconstruction

C - Basics, Bitwise Operator. Zhaoguo Wang

PYTHON- AN INNOVATION

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

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

Transcription:

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

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 192 255 (11111111) 0 Addition 64 128 (10000000)

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.

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

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!

What About Subtraction? Suppose we want to subtract 5. Adding 16 makes us do a complete turn Adding 16-5 = 11 will make us land five spaces before the original point 15 (1111) 0 This is how we will subtract 5 Does this look familiar? 12 4 8 (1000)

Two s Complement The Encoding comes from Definition of the 2 s complement of a number: 2 s complement of an N bit number, x, is its complement with respect to 2 N Can use this to find the bit encoding, y, for the negation of x: For N bits, y = 2 N x X -X 2 4 - X 4 bit examples: 0000 0000 10000 0000 = 0000 (only 4 bits) 0001 1111 10000 0001 = 1111 0010 1110 10000 0010 = 1110 0011 1101 10000 0011 = 1101

What About Subtraction? Suppose we want to subtract 5. Adding 16 makes us do a complete turn Adding 16-5 = 11 will make us land five spaces before the original point 15 (1111) 0 This is how we will subtract 5 This is two s complement! 12 4 8 (1000)

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 15 (1111) 0 12 4 8 (1000)

Negation Example (8 bits) For N bits, y = 2 N - x Negate 00000010 (2) 2 8-2 = 256-2 = 254 254 in binary is 11111110 255 (11111111) 0 192 64 128 (1000)

Negation Example (8 bits) For N bits, y = 2 N - x Negate 00000010 (2) 2 8-2 = 256-2 = 254 254 in binary is 11111110 255 (11111111) 0 192 64 Given 11111110, it s 254 if interpreted as unsigned and -2 interpreted as signed. 128 (1000)

Negation Example (8 bits) For N bits, y = 2 N - x Negate 00000010 (2) 2 8-2 = 256-2 = 254 254 in binary is 11111110 255 (11111111) 0 Negate 00101110 (46) 2 8-46 = 256-46 = 210 210 in binary is 11010010 192 128 (1000) 64

Negation Shortcut A much easier, faster way to negate: Flip the bits (0 s become 1 s, 1 s become 0 s) Add 1 (bit addition) Negate 00101110 (46) Flip the bits: 11010001 Add 1: 11010010

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

Signed Addition & Subtraction Addition is the same as for unsigned Can use the same hardware for both Subtraction is the same operation as addition Just need to negate the second operand One exception

By using two s complement, do we still have 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

Signed Addition & Subtraction Addition is the same as for unsigned Can use the same hardware for both Subtraction is the same operation as addition Just need to negate the second operand One exception: different rules for overflow

Overflow, in More Details 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

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! The result will look incorrect -1 0 1 Signed addition (and subtraction): Signed 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-127 0111 No chance of overflow here - signs of operands are different! -128 127

Signed Overflow Overflow: happens exactly when sign bits of operands are the same, but 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.

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

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) 3-6 = 0011 + 1010 + 1 = 0 1101 A. 1 B. 2 C. 3 D. 4 E. 5 (-6)

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 = 4 9 + 6 = 1001 + 0110 + 0 = 0 1111 = 15 3 + 6 = 0011 + 0110 + 0 = 0 1001 = 9 Subtraction (carry-in = 1) 6-3 = 0110 + 1100 + 1 = 1 0011 = 3 (-3) 3-6 = 0011 + 1010 + 1 = 0 1101 = 13 A. 1 B. 2 C. 3 D. 4 E. 5 Pattern? (-6)

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?

Suppose I have an 8-bit signed value, 00010110 (22), and I want to add it to a signed four-bit value, 1011 (-5). How should we represent the four-bit value? A. 1101 (don t change it) B. 00001101 (pad the beginning with 0 s) C. 11111011 (pad the beginning with 1 s) D. Represent it some other way.

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.

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!

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

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

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

C programming Up Next

Hello World # hello world import math Python def main(): print hello world main() C // hello world #include <stdio.h> int main( ) { printf( hello world\n ); return 0; } #: single line comment //: single line comment import libname: include Python libraries Blocks: indentation print: statement to printout string #include<libname>: include C libraries Blocks: { } (indentation for readability) printf: function to print out format string statement: each on separate line statement: each ends with ; def main(): : the main function definition int main( ) : the main function definition (int specifies the return type of main)

White Space Python cares about how your program is formatted. Spacing has meaning. C compiler does NOT care. Spacing is ignored. This includes spaces, tabs, new lines, etc. Good practice (for your own sanity): Put each statement on a separate line. Keep indentation consistent within blocks.

These are the same program #include <stdio.h> int main() { int number = 7; if (number > 10) { do_this(); } else { do_that(); } } #include <stdio.h> int main() { int number = 7; if (number > 10) { do_this(); } else { do_that();}}

Curly Bracket Etiquette #include <stdio.h> int main() { int number = 7; if (number > 10) { do_this(); } else { do_that(); } } #include <stdio.h> int main() { int number = 7; if (number > 10) { do_this(); } else { do_that(); } } The most important thing is being consistent throughout your program

Types Everything is stored as bits. Type tells us how to interpret those bits. What type of data is it? integer, floating point, text, etc.

Types in C All variables have an explicit type! You (programmer) must declare variable types. Where: at the beginning of a block, before use. How: <variable type> <variable name>; Examples: int humidity; float temperature; humidity = 20; temperature = 32.5

We have to explicitly declare variable types ahead of time? Lame! Python figured out variable types for us, why doesn t C? A. C is old. B. Explicit type declaration is more efficient. C. Explicit type declaration is less error prone. D. Dynamic typing (what Python does) is imperfect. E. Some other reason (explain)

Numerical Type Comparison Integers (int) Example: int humidity; humidity = 20; Floating Point (float, double) Example: float temperature; temperature = 32.5; Only represents integers Small range, high precision Faster arithmetic (Maybe) less space required Represents fractional values Large range, less precision Slower arithmetic I need a variable to store a number, which type should I use? Use the one that fits your specific need best

more C programming Up Next