Fundamentals of Programming Session 2

Similar documents
The Design of C: A Rational Reconstruction

The Design of C: A Rational Reconstruction"

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

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

The Design of C: A Rational Reconstruction

Number System. Introduction. Decimal Numbers

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

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

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

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

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

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

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

Chapter 4. Operations on Data

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

Number representations

CHW 261: Logic Design

Octal and Hexadecimal Integers

BINARY SYSTEM. Binary system is used in digital systems because it is:

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

Fundamentals of Programming. Lecture 1: Introduction to C Programming

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

EE292: Fundamentals of ECE

EE 109 Unit 6 Binary Arithmetic

Introduction to Computer Science-103. Midterm

Chapter 2: Number Systems

Digital Logic Design Exercises. Assignment 1

Fundamentals of Programming Session 15

Digital Logic. The Binary System is a way of writing numbers using only the digits 0 and 1. This is the method used by the (digital) computer.

Fundamentals of Programming Session 12

CMPE223/CMSE222 Digital Logic Design. Positional representation

Fundamentals of Programming (C)

Level ISA3: Information Representation

Fundamentals of Programming Session 7

Module 1: Information Representation I -- Number Systems

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

Binary Representations and Arithmetic

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

CMPSCI 145 MIDTERM #1 Solution Key. SPRING 2017 March 3, 2017 Professor William T. Verts

Fundamentals of Programming Session 20

Arithmetic and Bitwise Operations on Binary Data

Chapter 2 Bits, Data Types, and Operations

ECOM 2325 Computer Organization and Assembly Language. Instructor: Ruba A.Salamah INTRODUCTION

Number Systems CHAPTER Positional Number Systems

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

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

Digital Systems and Binary Numbers

Numeral Systems. -Numeral System -Positional systems -Decimal -Binary -Octal. Subjects:

SIGNED AND UNSIGNED SYSTEMS

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

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

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

INFS 214: Introduction to Computing

Final Labs and Tutors

CS & IT Conversions. Magnitude 10,000 1,

Chapter 2 Bits, Data Types, and Operations

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

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

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

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 1: Basic Concepts. Chapter Overview. Welcome to Assembly Language

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

Digital Fundamentals. CHAPTER 2 Number Systems, Operations, and Codes

Arithmetic and Bitwise Operations on Binary Data

Fundamentals of Programming Session 8

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

Learning the Binary System

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

T02 Tutorial Slides for Week 2

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Microcomputers. Outline. Number Systems and Digital Logic Review

Moodle WILLINGDON COLLEGE SANGLI. ELECTRONICS (B. Sc.-I) Introduction to Number System

Chapter 1. Digital Systems and Binary Numbers

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

Fundamentals of Programming Session 4

Chapter 5: Computer Arithmetic

More about Binary 9/6/2016

Chapter 2 Bits, Data Types, and Operations

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

Lecture 2: Number Systems

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

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

Chapter 3: Number Systems and Codes. Textbook: Petruzella, Frank D., Programmable Logic Controllers. McGraw Hill Companies Inc.

9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation

Chapter 5: Computer Arithmetic. In this chapter you will learn about:

Beyond Base 10: Non-decimal Based Number Systems

THE LOGIC OF COMPOUND STATEMENTS

Number systems and binary

Learning Objectives. Binary over Decimal. In this chapter you will learn about:

Number Systems. The Computer Works in Binary, or how I learned to think like a computer. The computer s natural number system is binary not decimal.

Applied Computer Programming

MC1601 Computer Organization

Signed Binary Numbers

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

ECE/Comp Sci 352 Digital Systems Fundamentals. Charles R. Kime Section 2 Fall Logic and Computer Design Fundamentals

Digital Electronics A Practical Approach with VHDL William Kleitz Ninth Edition

LAB A Translating Data to Binary

Chapter 10 Binary Arithmetics

Part 2,Number Systems Questions

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

Transcription:

Fundamentals of Programming Session 2 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 Sharif University of Technology

Outlines Programming Language Binary numbers Addition Subtraction Division Multiplication One s and Two s Complement 2

Programming Language A programming language is an artificial language designed to express computations that can be performed by a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine, to express algorithms precisely, or as a mode of human communication. Many programming languages have some form of written specification of their syntax (form) and semantics (meaning). Some languages are defined by a specification document. For example, the C programming language is specified by an ISO Standard. Other languages, such as Perl, have a dominant implementation that is used as a reference. 3

Programming Language Evolution of Programming Languages: First Generation: Machine languages Strings of numbers giving machine specific instructions Example: 1300042774 1400593419 1200274027 Computer only understands machine language instructions. Second Generation: Assembly languages English-like abbreviations representing elementary computer operations (translated via assemblers) 4

Programming Language Example: LOAD BASEPAY ADD OVERPAY STORE GROSSPAY Third Generation : High-level languages Codes similar to everyday English Use mathematical notations (translated via compilers) Example: grosspay = basepay + overpay 5

6 Programming Language

Common Software Operating System Assemblers Compilers Interpreters 7

Binary numbers 8 Binary numbers Why binary? Computers are built using digital circuits Inputs and outputs can have only two values True (high voltage) or false (low voltage) Converting base 10 to base 2 Octal and hexadecimal Integers Unsigned integers Integer addition Signed integers C bit operators And, or, not, and xor Shift-left and shift-right

Base 10 and Base 2 Base 10 Each digit represents a power of 10 4173 = 4*10 3 + 1*10 2 + 7*10 1 + 3*10 0 Base 2 Each bit represents a power of 2 10110 = 1*2 4 + 0*2 3 + 1*2 2 + 1*2 1 + 0*2 0 = 22 Question: What is the binary representation of number 12? Response: 1100 9

Base 8 Octal (base 8) Digits 0, 1,, 7 Thus the 12 bit binary 110 010 101 001 number converted to Oct is: 6251 000 = 0 001 = 1 010 = 2 011 = 3 100 = 4 101 = 5 110 = 6 111 = 7 10

Base 8 Question: What is the octal representation of number 118 (in base 10)? Response: 166 Question: What is the octal representation of number 1011000101 (in base 2)? Response: 1305 Question: What is the binary representation of number 1472 (in base 8)? Response: 1100111010 11

Base 16 Hexadecimal (base 16) Digits 0, 1,, 9, A, B, C, D, E, F Thus the 16-bit binary number 1011 0010 1010 1001 converted to Hex is: B2A9 0000 = 0 1000 = 8 0001 = 1 1001 = 9 0010 = 2 1010 = A 0011 = 3 1011 = B 0100 = 4 1100 = C 0101 = 5 1101 = D 0110 = 6 1110 = E 0111 = 7 1111 = F 12

Base 16 Question: What is the hexadecimal representation of number 375 (in base 10)? Response: 177 Question: What is the hexadecimal representation of number 1011000101(in base 2)? Response: 2C5 Question: What is the binary representation of number 6A4D2 (in base 16)? Response: 01101010010011010010 13

Integers Fixed number of bits in memory Short: usually 16 bits Int: 16 or 32 bits Long: 32 bits Unsigned integer No sign bit Always positive or 0 14 Example of unsigned int 00000001 1 00001111 15 00010000 16 00100001 33 11111111 255

Decimal Addition Add 3758 to 4657: 1 1 1 3 7 5 8 + 4 6 5 7 1) Add 8 + 7 = 15 Write down 5, carry 1 2) Add 5 + 5 + 1 = 11 Write down 1, carry 1 3) Add 7 + 6 + 1 = 14 Write down 4, carry 1 8 4 1 5 4) Add 3 + 4 + 1 = 8 Write down 8 15

Binary Addition Rules: 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 (just like in decimal) 1 + 1 = 2 10 = 10 2 = 0 with 1 to carry 16 1 + 1 + 1 = 3 10 = 11 2 = 1 with 1 to carry

Binary Addition 17 Example 1: Add binary 110111 to 11100 1 1 1 1 0 1 1 1 + 0 1 1 1 0 0 1 1 1 0 1 0 0 1 1 Col 1) Add 1 + 0 = 1 Write 1 Col 2) Add 1 + 0 = 1 Write 1 Col 3) Add 1 + 1 = 2 (10 in binary) Write 0, carry 1 Col 4) Add 1+ 0 + 1 = 2 Write 0, carry 1 Col 5) Add 1 + 1 + 1 = 3 (11 in binary) Write 1, carry 1 Col 6) Add 1 + 1 + 0 = 2 Write 0, carry 1 Col 7) Bring down the carried 1 Write 1

Binary Addition You can always check your answer by converting the figures to decimal, doing the addition, and comparing the answers. 1 1 0 1 1 1 + 0 1 1 1 0 0 1 0 1 0 0 1 1 Verification 110111 2 55 10 +011100 2 + 28 10 83 10 64 32 16 8 4 2 1 1 0 1 0 0 1 1 = 64 + 16 + 2 +1 = 83 10 18

Decimal Subtraction Subtract 4657 from 8025: 19 7 9 1 8 10 2 15-4 6 5 7 3 3 6 8 1) Try to subtract 5 7 can t. Must borrow 10 from next column. Add the borrowed 10 to the original 5. Then subtract 15 7 = 8. 2) Try to subtract 1 5 can t. Must borrow 10 from next column. But next column is 0, so must go to column after next to borrow. Add the borrowed 10 to the original 0. Now you can borrow 10 from this column. Add the borrowed 10 to the original 1.. Then subtract 11 5 = 6 3) Subtract 9 6 = 3 4) Subtract 7 4 = 3

Decimal Subtraction 8 0 2 5-4 6 5 7 3 3 6 8 So when you cannot subtract, you borrow from the column to the left. The amount borrowed is 1 base unit, which in decimal is 10. The 10 is added to the original column value, so you will be able to subtract. 20

Binary Subtraction In binary, the base unit is 2 So when you cannot subtract, you borrow from the column to the left. The amount borrowed is 2. The 2 is added to the original column value, so you will be able to subtract. 21

Binary Subtraction Example 1: Subtract binary 11100 from 110011 2 1 0 0 2 2 1 1 0 0 1 1-1 1 1 0 0 22 1 0 1 1 1 Col 1) Subtract 1 0 = 1 Col 2) Subtract 1 0 = 1 Col 3) Try to subtract 0 1 can t. Must borrow 2 from next column. But next column is 0, so must go to column after next to borrow. Add the borrowed 2 to the 0 on the right. Now you can borrow from this column (leaving 1 remaining). Add the borrowed 2 to the original 0. Then subtract 2 1 = 1 Col 4) Subtract 1 1 = 0 Col 5) Try to subtract 0 1 can t. Must borrow from next column. Add the borrowed 2 to the remaining 0. Then subtract 2 1 = 1 Col 6) Remaining leading 0 can be ignored.

Binary Subtraction Subtract binary 11100 from 110011: 2 1 0 0 2 2 1 1 0 0 1 1-1 1 1 0 0 1 0 1 1 1 Verification 110011 2 51 10-11100 2-28 10 23 10 64 32 16 8 4 2 1 = 16 + 4 + 2 + 1 = 23 10 1 0 1 1 1 23

One s and Two s Complement Consider only numbers in a range E.g., five-digit car odometer: 0, 1,, 99999 E.g., eight-bit numbers 0, 1,, 255 Roll-over when you run out of space E.g., car odometer goes from 99999 to 0, 1, E.g., eight-bit number goes from 255 to 0, 1, 24

One s and Two s Complement One s complement: flip every bit E.g., b is 01000101 (i.e., 69 in base 10) One s complement is 10111010 That s simply 255-69 Subtracting from 11111111 is easy (no carry needed!) 1111 1111-0100 0101 b 1011 1010 one s complement 25 Two s complement Add 1 to the one s complement E.g., (255 69) + 1 1011 1011

One s and Two s Complement 26 Computing a b for unsigned integers Same as a + 256 b Same as a + (255 b) + 1 Same as a + onecomplement(b) + 1 Same as a + twocomplement(b) Example: 172 69 The original number 69: 0100 0101 One s complement of 69: 1011 1010 Two s complement of 69: 1011 1011 Add to the number 172: 1010 1100 The sum comes to: 0110 0111 Equals: 103 in base 10 1010 1100 + 1011 1011 1 0110 0111

One s and Two s Complement 27 Sign-magnitude representation Use one bit to store the sign Zero for positive number One for negative number Examples E.g., 0010 1100 44 E.g., 1010 1100-44 Hard to do arithmetic this way, so it is rarely used Complement representation One s complement Flip every bit E.g., 1101 0011-44 Two s complement Flip every bit, then add 1 E.g., 1101 0100-44