Computer Science 111 Fundamentals of Programming I Number Systems.

Slides:



Advertisements
Similar presentations
The Binary Numbering Systems
Advertisements

Binary Systems1 DIGITAL LOGIC DESIGN by Dr. Fenghui Yao Tennessee State University Department of Computer Science Nashville, TN.
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3:
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3: IT Students.
EMB1006 The Binary System There is no 2 Jonathan-Lee Jones.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 2 Number Systems.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Round-Off and Truncation Errors
Digital Fundamentals Floyd Chapter 2 Tenth Edition
Assembly Language and Computer Architecture Using C++ and Java
Introduction to Programming with Java, for Beginners
Assembly Language and Computer Architecture Using C++ and Java
Computer Science 210 Computer Organization Floating Point Representation.
Floating Point Numbers.  Floating point numbers are real numbers.  In Java, this just means any numbers that aren’t integers (whole numbers)  For example…
Binary Numbers.
COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter.
Number Systems Computer Science 210 Computer Organization.
Chapter 3 Data Representation part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Connecting with Computer Science 2 Objectives Learn why numbering systems are important to understand Refresh your knowledge of powers of numbers Learn.
Binary Representation and Computer Arithmetic
The Binary Number System
Data Representation Number Systems.
Simple Data Type Representation and conversion of numbers
ES 244: Digital Logic Design Chapter 1 Chapter 1: Introduction Uchechukwu Ofoegbu Temple University.
Information Representation (Level ISA3) Floating point numbers.
CSU0014 Assembly Languages Homepage: Textbook: Kip R. Irvine, Assembly Language for Intel-Based Computers,
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
Assembly Language for x86 Processors 7th Edition
2-1 Chapter 2 - Data Representation Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Chapter Contents.
1 Digital Technology and Computer Fundamentals Chapter 1 Data Representation and Numbering Systems.
NUMBER REPRESENTATION CHAPTER 3 – part 3. ONE’S COMPLEMENT REPRESENTATION CHAPTER 3 – part 3.
Chapter 1 Data Storage(3) Yonsei University 1 st Semester, 2015 Sanghyun Park.
IT253: Computer Organization
Computer Architecture
Number Systems Spring Semester 2013Programming and Data Structure1.
Data Representation in Computer Systems
Calculating Two’s Complement. The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of.
Lecture 5. Topics Sec 1.4 Representing Information as Bit Patterns Representing Text Representing Text Representing Numeric Values Representing Numeric.
Chapter 2 Binary Values and Number Systems. 2 2 Natural Numbers Zero and any number obtained by repeatedly adding one to it. Examples: 100, 0, 45645,
©Brooks/Cole, 2003 Chapter 3 Number Representation.
CPU Internal memory I/O interface circuit System bus
Chapter 3 Number Representation. Convert a number from decimal to binary notation and vice versa. Understand the different representations of an integer.
Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.
CSC 221 Computer Organization and Assembly Language
Number Representation
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
Computer Math CPS120 Introduction to Computer Science Lecture 4.
COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.
Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Signed Integers The highest bit indicates the sign. 1 = negative, 0 = positive.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Digital Fundamentals Tenth Edition Floyd Chapter 2 © 2008 Pearson Education.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.7 Instructor: Lin Chen Sept 2013.
Chapter 1 Representing Data in a Computer. 1.1 Binary and Hexadecimal Numbers.
Software Design and Development Storing Data Computing Science.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
1 Integer Representations V1.0 (22/10/2005). 2 Integer Representations  Unsigned integer  Signed integer  Sign and magnitude  Complements  One’s.
Floating Point Numbers
Cosc 2150: Computer Organization
Programming and Data Structure
Number Representation
Number Systems and Binary Arithmetic
Data Representation Binary Numbers Binary Addition
Data Structures Mohammed Thajeel To the second year students
Fundamentals of Programming I Number Systems
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
OBJECTIVES After reading this chapter, the reader should be able to :
Presentation transcript:

Computer Science 111 Fundamentals of Programming I Number Systems

Numeric Types: int int is used for integers In many languages, the range of int is through In Python, an integer’s magnitude is limited only by the computer’s memory

Why ? Numbers are stored in computer memory as patterns of voltage levels Just two voltage levels, ON and OFF, are significant on a digital computer ON and OFF represent the digits 1 and 0 of the binary number system All numbers are represented in binary in a digital computer

Computer Memory … Memory might have billions of cells that support the storage of trillions of binary digits or bits of information Each cell in this memory has room for 32 bits

Bits and Bytes A byte is 8 bits In some languages, int uses 4 bytes The magnitude and the sign (+/-) of the number are determined by the binary representation byte

Decimal and Binary Decimal numbers use the 10 decimal digits and a base of 10 Binary numbers use the binary digits 0 and 1 and a base of 2 The base is often provided as a subscript to indicate the type of system, as in and Thus, is very different from

Positional Notation Number systems are positional, so the magnitude of the number depends on the base and the position of the digits in the number Each position represents a power of the number’s base For example, in a 3-digit decimal number, the three positions represent the number of hundreds (10 2 ), tens (10 1 ), and ones (10 0 ) 342 = 3 * * * 10 0 = 3 * * * 1 = = 342

Positional Notation: Binary The base is now 2 and the only digits are 0 and 1 Each position represents a power of 2 For example, in a 4-digit binary number, the four positions represent the number of eights (2 3 ), fours (2 2 ), twos (2 1 ), and ones (1 0 ) 1101 = 1 * * * * 2 0 = 1 * * * * 1 = = 13

An Algorithm for Binary to Decimal Conversion # Input: A string of 1 or more binary digits # Output: The integer represented by the string binary = input("Enter a binary number: ") decimal = 0 exponent = len(binary) – 1 for digit in binary: decimal = decimal + int(digit) * 2 ** exponent exponent = exponent – 1 print("The integer value is", decimal) The len function returns the number of characters in a string The for loop visits each character in a string

Counting in Binary BinaryMagnitude Each power of 2 in binary is a 1 followed by the number of 0s equal to the exponent What is the magnitude of ?

Counting in Binary BinaryMagnitude Each number with only 1s equals one less than the power of 2 whose exponent is that number of 1s What is the magnitude of ?

Limits of Magnitude - Unsigned int s Unsigned integers are the non-negative integers The largest unsigned integer that can be represented using N bits is 2 N - 1 (all bits are 1s) Thus, the largest unsigned integer stored in 32 bits is

Limits of Magnitude - Signed int s Signed integers include negative and positive integers and 0 Part of the memory (one bit) must be reserved to represent the number’s sign somehow For each bit unavailable, you must subtract 1 from the exponent (2 N-1 ) of the number’s magnitude Thus, the largest positive signed integer stored in 32 bits is

Twos Complement Notation Positive numbers have 0 in the leftmost bit, negative numbers have 1 in the leftmost bit To compute a negative number’s magnitude, –Invert all the bits –Add 1 to the result –Use the conversion algorithm To represent a negative number, –Translate the magnitude to an unsigned binary number –Invert all the bits –Add 1 to the result

Convert Decimal to Binary Start with an integer, N, and an empty string, S Assume that N > 0 While N > 0: –Compute the remainder of dividing N by 2 (will be 0 or 1) –Prepend the remainder’s digit to S –Reset N to the quotient of N and 2

An Algorithm for Decimal to Binary Conversion # Input: An integer > 0 # Output: A string representing the integer in base 2 n = int(input("Enter an integer greater than 0: ")) binary = '' while n > 0: rem = n % 2 binary = str(rem) + binary n = n // 2 print(binary) Here we want the quotient and the remainder, not exact division!

Numeric Types: float Uses 8 bytes to represent floating-point numbers that range from through Default printing is up to 16 digits of precision But actual precision seems to extend even further - try format string with 60 places

Problems with Float Some numbers, such as 0.1, cannot be represented exactly, due to round-off errors This can cause errors when two floats are compared for equality

Example: Add 0.1 Ten Times sum = 0.0 for i in range(10): sum = sum print(sum) # Displays 1.0 print(sum < 1.0) # Displays True! Convert dollars and cents to pennies ( int s) in critical financial calculations!

Use int or float ? In general, arithmetic operations are faster with integers than with floats Integers (regular ones) require less memory Use float when it’s convenient to do so and you’re not worried about losing numeric information

Reading For Monday Finish Chapter 4