Extended Introduction to Computer Science CS1001.py. Lecture 5 Part A: Integer Representation (in Binary and other bases)

Similar documents
Extended Introduction to Computer Science CS1001.py. Lecture 4: Functions & Side Effects ; Integer Representations: Unary, Binary, and Other Bases

Instructors: Daniel Deutch, Amir Rubinstein, Teaching Assistants: Amir Gilad, Michal Kleinbort

COMP combinational logic 1 Jan. 18, 2016

Extended Introduction to Computer Science CS1001.py Lecture 7: Basic Algorithms: Sorting, Merge; Time Complexity and the O( ) notation

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

Binary Representations and Arithmetic

Extended Introduction to Computer Science CS1001.py Lecture 10, part A: Interim Summary; Testing; Coding Style

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

Extended Introduction to Computer Science CS1001.py

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

Extended Introduction to Computer Science CS1001.py. Lecture 5: Integer Exponentiation Efficiency of Computations; Search

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

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

Lecture 5 Part B (+6): Integer Exponentiation

Extended Introduction to Computer Science CS1001.py Lecture 5: Integer Exponentiation; Search: Sequential vs. Binary Search

Numbers and Representations

Extended Introduction to Computer Science CS1001.py Lecture 15: The Dictionary Problem Hash Functions and Hash Tables

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

Extended Introduction to Computer Science CS1001.py Lecture 11: Recursion and Recursive Functions, cont.

Number System. Introduction. Decimal Numbers

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

T02 Tutorial Slides for Week 2

Computer Science 1001.py. Lecture 19a: Generators continued; Characters and Text Representation: Ascii and Unicode

Integer Multiplication and Division

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

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

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Integer Representation

cast int( x float( x str( x hex( int string int oct( int string int bin( int string int chr( int int ord( ch

CS 64 Week 0 Lecture 1. Kyle Dewey

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

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

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

Final Labs and Tutors

Lecture 16: Binary Search Trees

Lab Using the Windows Calculator with Network Addresses

CS & IT Conversions. Magnitude 10,000 1,

CHAPTER 2 (b) : AND CODES

Lecture 1: What is a computer?

TOPICS. Other Number Systems. Other Number Systems 9/9/2017. Octal Hexadecimal Number conversion

CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduction to C, Part I

Data Representation Type of Data Representation Integers Bits Unsigned 2 s Comp Excess 7 Excess 8

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

Lecture 1: First Acquaintance with Python

Switching Circuits and Logic Design Prof. Indranil Sengupta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Computer Arithmetic. In this article we look at the way in which numbers are represented in binary form and manipulated in a computer.

data within a computer system are stored in one of 2 physical states (hence the use of binary digits)

Computer Science 1001.py. Lecture 19, part B: Characters and Text Representation: Ascii and Unicode

Slide Set 1. for ENEL 353 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

Chapter 1 Review of Number Systems

Computer Science 1001.py. Lecture 11: Recursion and Recursive Functions

CS 64 Week 1 Lecture 1. Kyle Dewey

Bits, Words, and Integers

in this web service Cambridge University Press

Module 1: Information Representation I -- Number Systems

Python Programming: An Introduction to Computer Science

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

Memory Addressing, Binary, and Hexadecimal Review

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

Fundamentals of Programming Session 2

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

Recap from Last Time. CSE 2021: Computer Organization. It s All about Numbers! 5/12/2011. Text Pictures Video clips Audio

What Is It? Instruction Register Address Register Data Register

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Chapter 2 Number System

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

CITS 1401 Problem Solving & Programming

Microcomputers. Outline. Number Systems and Digital Logic Review

CS 261 Fall Mike Lam, Professor Integer Encodings

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

CSCI 2212: Intermediate Programming / C Chapter 15

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

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

Chapter 3: Operators, Expressions and Type Conversion

Topic Notes: Bits and Bytes and Numbers

Boolean Arithmetic. From Nand to Tetris Building a Modern Computer from First Principles. Chapter 2

Inf2C - Computer Systems Lecture 2 Data Representation

Number Systems MA1S1. Tristan McLoughlin. November 27, 2013

COMP 122/L Lecture 2. Kyle Dewey

Variables and literals

! Addition! Multiplication! Bigger Example - RSA cryptography

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

EE292: Fundamentals of ECE

Beginning C Programming for Engineers

Lecture 17A: Finite and Infinite Iterators

Where we are going (today)

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

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

Python Numbers. Learning Outcomes 9/19/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17

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

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

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

CS 101: Computer Programming and Utilization

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

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

Agenda EE 224: INTRODUCTION TO DIGITAL CIRCUITS & COMPUTER DESIGN. Lecture 1: Introduction. Go over the syllabus 3/31/2010

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Lecture Notes on Ints

Transcription:

Extended Introduction to Computer Science CS1001.py Lecture 5 Part A: Integer Representation (in Binary and other bases) Instructors: Daniel Deutch, Amir Rubinstein Teaching Assistants: Michal Kleinbort, Amir Gilad School of Computer Science Tel-Aviv University, Winter Semester, 2017-8 http://tau-cs1001-py.wikidot.com

Reminder: What we did in Lectures 1-4 (Highlights) Python basics: types and variables conditionals (if-elif-else) loops (while, for and range) collections (str, list, tuple, dictionary, set) functions Python s memory model 2 / 25

And Now for Something Completely Different Until now we learned mostly Python. Some of you probably feel like this: Or, desirably, like this: 3 / 25

Before we move on... Reminder: what to do after each lecture/recitation: From now until the end of the course, we will learn numerous topics, and present to you the beauty, and challanges, of Computer Science. We will use Python extensively, and learn new tricks along the way. 4 / 25

Lecture 5 Part A: Plan Integer representation: you will understand how natural numbers are written in binary and other bases. Natural numbers: Unary vs. binary representation. Natural numbers: Representation in different bases (binary, decimal, octal, hexadecimal, 31, etc.). Negative integers (for reference only) Arithmetic operations on integers 5 / 25

1. Integer representation: Natural numbers God created the natural numbers; all the rest is the work of man (Leopold Kronecker, 1823 1891). 6 / 25

Binary Representation: From Bits to Numbers A brief explanation, using cards on whiteboard, of the binary representation of numbers, following the count the dots binary numbers activity from the Computer Science Unplugged site (Hebrew translation in. In particular, we will demonstrate how to convert binary representation to decimal representation, and vice versa. This, and more general base conversions, will be further discussed in recitations and in HW2 (no cards though :-). 7 / 25

Deep Inside the Computer Circuitry Most of the logic and memory hardware inside the computer are electronic devices containing a huge number of transistors (the transistor was invented in 1947, Bell Labs, NJ). At any given point in time, the voltage in each of these tiny devices (1 nanometer = 10 9 meter) is either +5v or 0v. Transistors operate as switches, and are combined to complex and functional circuitry. An extremely useful abstraction is to ignore the underlying electric details, and view the voltage levels as bits (binary digits): 0v is viewed as 0, +5v is viewed as 1. 8 / 25

From Bits to Numbers and Beyond - Hierarchy of Abstraction Levels An extremely useful abstraction is to ignore the underlying electric details, and view the voltage levels as bits (binary digits): 0v is viewed as 0, +5v is viewed as 1. The next conceptual step is arranging these bits so they can represent natural numbers. Then, we will strive to arrange natural numbers so they can represent other types of numbers - negative integers, real numbers, complex numbers, and furthermore characters, text, pictures, audio, video, etc. etc. (figure taken from http://www.learner.org) We will begin at the beginning: From bits to numbers. 9 / 25

Unary vs. Binary Representation of Numbers Consider the natural number nineteen. In unary, it is represented as 1111111111111111111. In binary, it is represented as 10011. The two representations refer to the same entity (the natural number nineteen, written in decimal as 19). However, the lengths of the two representations are substantially different. The unary representation is exponentially longer than the binary representation. To see this, consider the natural number 2 n. In unary it is represented by 2 n ones. In binary, it is represented by a single one, followed by n zeroes. 10 / 25

Representation of Natural Numbers in Base > 1 A natural number N can be represented in a base b (b > 1, an integer), as a polynomial, whose coefficients are natural numbers smaller than b. The coefficients of the polynomial are the digits of N in its b-ary representation. N = a k b k + a k 1 b k 1 +... + a 1 b + a 0 where for each i, 0 a i b. N in base b = (a k a k 1...a 1 a 0 ) Claim: The natural number N represented as a polynomial of degree k (has k + 1 digits) in base b satisfies b k N b k+1. Can you prove this? 11 / 25

Representation of Natural Numbers in Base > 1 Claim: The natural number N represented as a polynomial of degree k (has k + 1 digits) in base b satisfies b k N b k+1. Can you prove this? Concllusion (proof in the recitations): The number of digits, d, required for representing the natural number N in base b satisfies d = log b N + 1. 12 / 25

Other Bases > 1 Beside the commonly used decimal (base 10) and binary (base 2) representations, other representations are also in use. In particular the ternary (base 3), octal (base 8) and hexadecimal (hex, 0x, base 16) are well known. The lengths of representations in different bases differ. However, the lengths in bases b 2 and c 2 are related linearly. For example, a number represented with d digits (in decimal) will take at most d log 2 10 bits (in binary). Can you prove this? (maybe in your HW) As alluded to earlier, natural numbers in the computer are represented in binary, for a variety of reasons having to do with the physical world, electric circuits, capacities, currents, etc.. 13 / 25

Different Bases Representations in Python Python has built in functions for converting a number from decimal (base 10) to binary, octal (base 8), and hexadecimal (base 16). The returned values are strings, whose prefixes 0b,0o,0x indicate the bases 2, 8, 16, respectively. >>> bin(1000) 0b1111101000 >>> oct(1000) 0o1750 >>> hex(1000) 0x3e8 #hexadedimal digits are 0,1,2,...,9,a,b,c,d,e,f >>> type(bin(1000)) <class str > 14 / 25

Hexadecimal Representations in Python In hex, the letters a,b,...,f indicate the digits 10, 11,..., 15, respectively. >>> hex(10) 0xa >>> hex(15) 0xf >>> hex(62) 0x3e # 62 = 3*16 + 14 Recitation ( tirgul ): Conversion to target bases 2, 8, 16. 15 / 25

Converting to Decimal in Python Python has a built-in function, int, for converting a number from base b representation to decimal representation. The input is a string, which is a representation of a number in base b (for the power of two bases, 0b, 0o, 0x prefixes are optional), and the base, b itself. >>> int("0110",2) 6 >>> int("0b0110",2) 6 >>> int("f",16) 15 >>> int("fff",16) 4095 >>> int("fff",17) 4605 >>> int("ben",24) 6695 >>> int("ben",23) # "a" is 10,...,"m" is 22, so no "n" in base 23 Traceback (most recent call last): File "<pyshell#16>", line 1, in <module> int("ben",23) ValueError: invalid literal for int() with base 23: ben 16 / 25

Negative Integers An interesting issue is how negative integers can be represented. Suppose we use n bits. We would like to divide the range of 2 n values represented by those bits such that about half would be used for positive and about half for negative integers. For example, 32 bits can be used to represent any integer, k, in the range 2 31 k 2 31 1. We would also like elementary operations such as increment/decrement, addition, subtraction, and negation to be efficient. 17 / 25

Negative Integers - the sign bit option We could simply assign the leftmost bit to denote the sign: 0 mean +, 1 means. Disadvantages? 0 has 2 representations (+0 and -0) arithmetical operations (e.g. addition) involving negative numbers require slightly different algorithms (and computer circuitry). So the sign bit method is not in use. The method most often used is Two s Complement 18 / 25

Two s Complement (for reference only) For the sake of completeness, we ll explain how negative integers are represented in the two s complement representation. Suppose we have a k bits, non negative integer, M. To represent M, we compute 2 k M, and drop the leading (leftmost) bit. For the sake of simplicity, suppose we deal with an 8 bits number: 100000000 00000001 M = 1 --------- 11111111 1 100000000 00001110 M = 14 --------- 11110010 14 It turns out that in Two s complement non negative integers have 0 as their leading (leftmost) bit, while negative integers have 1 as their leading (leftmost) bit. A desirable property of two s complement is that we add two numbers as usual, regardless of whether they are positive or negative. 19 / 25

Large Natural Numbers and Computer Words Modern computers are arranged by words, groups of fixed number of bits that correspond to size of registers, units of memory, etc. Word size is uniform across a computer, and depends both on the hardware (processors, memory, etc.) and on the operating system. Typical word sizes are 8, 16 (Intel original 8086), 32, or 64 bits (most probably used by your PC or imac). In many programming languages, integers are represented by either a single computer word, or by two computer words. In Java, for example, these are termed int and long, correspondingly. This means that the range of integers is limited. Things are quite different in Python, as we will soon see. 20 / 25

Large Natural Numbers A 32 bits word can represent any integer, k, in the range 2 31 k 2 31 1. To handle larger numbers, these 32 bits words should be manipulated correspondingly (represent signed sums of successive powers of 2 32 ). This is either done explicitly by the user/programmer, or provided directly by the programming language. Python takes care of large integers internally, even if they are way over the word size. >>> 3**97-2**121+17 19088056320749371083854654541807988572109959828 21 / 25

Complexity Issues Still, when manipulating large integers, one should think of the computational resources involved: Space: There is no difficulty to represent 2 210 17, or, in Python 2**(2**10)-17 (as the result takes four lines on my Python Shell window, I skip it). But don t try 2 2100 17 (why?)! Time: How many basic operations (or, clock cycles) are needed? Time will grow as n, the number of bits in the numbers we operate on, grows. How time grows as a function of n is important since this will make the difference between fast and slow tasks, and even between feasible and infeasible tasks. We will define these notions precisely in the future, but you should start paying attention today. 22 / 25

Binary Addition Suppose we have two n-bit positive integers in binary, A and B. Computing A+B is done bitwise, with possibly a carry bit in each position. Q1: What is the maximal length of the output? Q2: How many bit operations needed? 23 / 25

Binary Multiplication Suppose we have two n-bit positive integers in binary, A and B. Computing A*B can also be done bitwise, just like decimal multiplication from highschool: Q1: what is the maximal length of the output? Q2: How many bit operations needed? 24 / 25

Binary operations Subtraction and division are also performed in a similar way to decimal, but we will not show it. This site: http://courses.cs.vt.edu/ csonline/numbersystems/lessons/index.html contains some really good explanations and demos. 25 / 25