Fundamentals of Programming I Number Systems

Slides:



Advertisements
Similar presentations
2009 Spring Errors & Source of Errors SpringBIL108E Errors in Computing Several causes for malfunction in computer systems. –Hardware fails –Critical.
Advertisements

Princess Sumaya Univ. Computer Engineering Dept. Chapter 3:
Assembly Language and Computer Architecture Using C++ and Java
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.
Number Systems Computer Science 210 Computer Organization.
Binary Representation and Computer Arithmetic
Data Representation Number Systems.
Simple Data Type Representation and conversion of numbers
CSU0014 Assembly Languages Homepage: Textbook: Kip R. Irvine, Assembly Language for Intel-Based Computers,
Computer Science 111 Fundamentals of Programming I Number Systems.
IT253: Computer Organization
Number Systems Spring Semester 2013Programming and Data Structure1.
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.
Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Signed Integers The highest bit indicates the sign. 1 = negative, 0 = positive.
973cs111_add_posneg.ppt Integers Whole numbers Do NOT contain decimal points (as in money) 43,689 is an integer 43, is NOT an integer (it is floating.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
CHAPTER 5: Representing Numerical Data
Floating Point Numbers
Floating Point Representations
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)
Topic: Binary Encoding – Part 2
Data Representation COE 308 Computer Architecture
Cosc 2150: Computer Organization
Binary & Hex Review.
Computer Science 210 Computer Organization
Programming and Data Structure
Computer Science 210 Computer Organization
Data Representation ICS 233
Data Representation.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
A brief comparison of integer and double representation
Number Representation
3.1 Denary, Binary and Hexadecimal Number Systems
Introduction To Computer Science
Number Systems and Binary Arithmetic
Digital Logic & Design Dr. Waseem Ikram Lecture 02.
Data Representation Binary Numbers Binary Addition
Floating Point Math & Representation
Chapter 4 – Fundamental Data Types
Chapter 3 Data Storage.
Computer Science 210 Computer Organization
Numbers in a Computer Unsigned integers Signed magnitude
Number Systems & Binary
A Level Computing Component 2
William Stallings Computer Organization and Architecture 7th Edition
Chapter 6 Floating Point
Data Structures Mohammed Thajeel To the second year students
Binary Numbers Material on Data Representation can be found in Chapter 2 of Computer Architecture (Nicholas Carter) CSC 370 (Blum)
Computer Science 210 Computer Organization
Chapter 2 Bits, Data Types & Operations Integer Representation
Number Representation
Number Representations
Computer Science 210 Computer Organization
Binary / Hex Binary and Hex The number systems of Computer Science.
Digital Logic & Design Lecture 02.
Computer Science 210 Computer Organization
Number Representation
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Computer Organization
Decimal and binary representation systems
Binary & Hex Review.
OBJECTIVES After reading this chapter, the reader should be able to :
Number Representations
Data Representation COE 308 Computer Architecture
Chapter 1 Introduction.
Presentation transcript:

Fundamentals of Programming I Number Systems 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 -231 through 231 - 1 (-2,147,483,648 through 2,147,483,647) In Python, an integer’s magnitude is limited only by the computer’s memory

Why 231 - 1? 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 … 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 byte byte byte byte A byte is 8 bits 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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

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 304210 and 110111102 Thus, 110110 represents a very different integer value from 11012

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 (102), tens (101), and ones (100) 342 = 3 * 102 + 4 * 101 + 2 * 100 = 3 * 100 + 4 * 10 + 2 * 1 = 300 + 40 + 2 = 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 (23), fours (22), twos (21), and ones (10) 1101 = 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20 = 1 * 8 + 1 * 4 + 0 * 2 + 1 * 1 = 8 + 4 + 0 + 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 Binary Magnitude 1 10 2 11 3 100 4 101 5 110 6 111 7 1000 8 Each power of 2 in binary is a 1 followed by the number of 0s equal to the exponent What is the magnitude of 10000002? 21 22 23

Counting in Binary Binary Magnitude 1 10 2 11 3 100 4 101 5 110 6 111 7 1000 8 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 11112? 21 - 1 22 - 1 23 - 1

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

Limits of Magnitude - Signed ints 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 (2N-1) of the number’s magnitude Thus, the largest positive signed integer stored in 32 bits is 231 - 1

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

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 +10308.25 through -10308.25 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 total = 0.0 for i in range(10): total = total + 0.1 print(total) # Displays 1.0 print(total < 1.0) # Displays True! Convert dollars and cents to pennies (ints) 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 Friday Finish Chapter 4