Set Theory in Computer Science. Binary Numbers. Base 10 Number. What is a Number? = Binary Number Example

Similar documents
Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

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

COMP2611: Computer Organization. Data Representation

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

Chapter 3: Arithmetic for Computers

Number Systems. Both numbers are positive

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

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

Numeric Encodings Prof. James L. Frankel Harvard University

IEEE Standard 754 Floating Point Numbers

unused unused unused unused unused unused

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

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Introduction to Computers and Programming. Numeric Values

FLOATING POINT NUMBERS

Chapter 2. Data Representation in Computer Systems

Signed umbers. Sign/Magnitude otation

Chapter 2 Data Representations

Module 2: Computer Arithmetic

Floating Point Arithmetic

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

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

Inf2C - Computer Systems Lecture 2 Data Representation

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Data Representations & Arithmetic Operations

C NUMERIC FORMATS. Overview. IEEE Single-Precision Floating-point Data Format. Figure C-0. Table C-0. Listing C-0.

CMPSCI 145 MIDTERM #2 SPRING 2017 April 7, 2017 Professor William T. Verts NAME PROBLEM SCORE POINTS GRAND TOTAL 100

CSCI 402: Computer Architectures. Arithmetic for Computers (3) Fengguang Song Department of Computer & Information Science IUPUI.

Numerical computing. How computers store real numbers and the problems that result

Chapter 4: Data Representations

8/30/2016. In Binary, We Have A Binary Point. ECE 120: Introduction to Computing. Fixed-Point Representations Support Fractions

MACHINE LEVEL REPRESENTATION OF DATA

Floating Point Numbers. Lecture 9 CAP

CHW 261: Logic Design

IEEE Floating Point Numbers Overview

ECE232: Hardware Organization and Design

M1 Computers and Data

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

COMP Overview of Tutorial #2

Chapter 4. Operations on Data

Numerical Representations On The Computer: Negative And Rational Numbers

Signed Multiplication Multiply the positives Negate result if signs of operand are different

Introduction to Scientific Computing Lecture 1

Integers and Floating Point

Chapter Three. Arithmetic

Computer Systems C S Cynthia Lee

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

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Birkbeck (University of London) Department of Computer Science and Information Systems. Introduction to Computer Systems (BUCI008H4)

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

Numerical Representations On The Computer: Negative And Rational Numbers

INTEGER REPRESENTATIONS

Floating Point. The World is Not Just Integers. Programming languages support numbers with fraction

±M R ±E, S M CHARACTERISTIC MANTISSA 1 k j

Description Hex M E V smallest value > largest denormalized negative infinity number with hex representation 3BB0 ---

Scientific Computing. Error Analysis

CS101 Lecture 04: Binary Arithmetic

Representing numbers on the computer. Computer memory/processors consist of items that exist in one of two possible states (binary states).

Bits, Words, and Integers

Digital Fundamentals

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

Question 4: a. We want to store a binary encoding of the 150 original Pokemon. How many bits do we need to use?

Divide: Paper & Pencil

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

CS 61C: Great Ideas in Computer Architecture Floating Point Arithmetic

CO212 Lecture 10: Arithmetic & Logical Unit

Computer (Literacy) Skills. Number representations and memory. Lubomír Bulej KDSS MFF UK

Experimental Methods I

CMPSCI 145 MIDTERM #2 SOLUTION KEY SPRING 2015 April 3, 2015 Professor William T. Verts

Floating Point Numbers

CS367 Test 1 Review Guide

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

ecture 25 Floating Point Friedland and Weaver Computer Science 61C Spring 2017 March 17th, 2017

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

A Level Computing. Contents. For the Exam:

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

More about Binary 9/6/2016

Organisasi Sistem Komputer

The Sign consists of a single bit. If this bit is '1', then the number is negative. If this bit is '0', then the number is positive.

Digital Computers and Machine Representation of Data

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

UNIT 7A Data Representation: Numbers and Text. Digital Data

Introduction to Computer Systems Recitation 2 May 29, Marjorie Carlson Aditya Gupta Shailin Desai

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

LAB WORK NO. 2 THE INTERNAL DATA REPRESENTATION

Introduction to Computer Science-103. Midterm

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

Representing Information. Bit Juggling. - Representing information using bits - Number representations. - Some other bits - Chapters 1 and 2.3,2.

15213 Recitation 2: Floating Point

Data Representation 1

COMS 1003 Fall Introduction to Computer Programming in C. Bits, Boolean Logic & Discrete Math. September 13 th

Computational Methods CMSC/AMSC/MAPL 460. Representing numbers in floating point and associated issues. Ramani Duraiswami, Dept. of Computer Science

CS 61C: Great Ideas in Computer Architecture Performance and Floating Point Arithmetic

What we need to know about error: Class Outline. Computational Methods CMSC/AMSC/MAPL 460. Errors in data and computation

Chapter 5 : Computer Arithmetic

Machine Arithmetic 8/31/2007

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

Transcription:

Set Theory in Computer Science Binary Numbers Part 1B Bit of This and a Bit of That What is a Number? Base 10 Number We use the Hindu-Arabic Number System positional grouping system each position is a power of 10 Binary numbers based on the same system use powers of 2 rather than 10 each digit is in the set { 0, 1 } The number 1783 is... 10 4 10 3 10 2 10 1 10000 1000 100 10 0 1 7 8 1000 + 700 + 80 + 3 = 1783 10 0 1 3 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 3 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 4 Binary Number Example Binary Number Example The number 0110 1001 is... 2 7 2 6 2 5 2 4 2 3 2 2 2 1 128 64 32 16 8 4 2 1 2 0 0 1 1 0 1 0 0 1 64 + 32 + 8 + 1 = 105 The number 1101 1011 is... 2 7 2 6 2 5 2 4 2 3 2 2 2 1 128 64 32 16 8 4 2 1 2 0 1 1 0 1 1 0 1 1 128 + 64 + 16 + 8 + 2 + 1 = 219 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 5 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 6 1

Numbers are Tuples Looking at Numbers In Hindu-Arabic system, the order of the symbols is important so they are tuples e.g. 123 321 Other number styles use sets i.e. the ancient Egyptian system Numbers are tuples 1947 1974 Members of the decimals number are also members of the set {0, 1, 2, 9} 1947 (1,9,4,7) 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 7 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 8 Looking at Binary Numbers Looking at Binary Numbers Binary numbers are tuples 10010100 11100000 Members of the binary number are also members of the set {0, 1} So, for a binary number B, all x B holds the following: x {0, 1} 10100111 (1,0,1,0,0,1,1,1) 10100111 (1,0,1,0,0,1,1,1) 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 9 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 10 So. Let's Make a Set-Based System {1776, 1846, 1947} { (1,7,7,6), (1,8,4,6) (1,9,4,7) } We are mostly used to tuple-based number systems But, for most of history, people used sets Let's create one 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 11 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 12 2

Floating Point Numbers Floating Point Numbers Real numbers are real complex Often, programs need to perform mathematics on real numbers Floating point numbers are used to represent quantities that cannot be represented by integers 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 14 Floating Point Numbers IEEE 754 Why? regular binary numbers can only store whole positive and negative values many numbers outside the range representable within the system's bit width (too large/small) Practically modern computers use the IEEE 754 Standard to store floating-point numbers Represent by a mantissa and an exponent similar to scientific notation the value of a number is: mantissa 2 exponent uses signed magnitude 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 15 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 16 IEEE 754 IEEE 754 Single Precision (32 bit) Comes in three forms: single-precision: 32-bit double-precision: 64-bit quad-precision: 128-bit Also supports special values: negative and positive infinity and "not a number" for errors (e.g. 1/0) 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 17 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 18 3

Fractional Field Fractional Field The fraction field number that represents part of the mantissa If a number is in proper scientific notation it always has a single digit before the decimal place for decimal numbers, this is 1..9 (never zero) for base-2 numbers, it is always 1 So, do we need to store the leading 1? It will always be a 1 The faction field, therefore only represents the fractional portion of a binary number the integer portion is assumed to be 1 this increases the number of significant digits that can be represented (by not wasting a bit) 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 19 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 20 Exponent Field Exponent Field The exponent field supports negative and positive values but does not use signmagnitude or 2's complement Uses a "biased" integer representation fixed value is added to the exponent before storing it when interpreting the stored data, this fixed value is then subtracted Bias is different depending on precision single precision: 127 double precision: 1023 quad precision: 16383 For example, for single precision exponent of 12 stored as: (+12 + 127) = 139 exponent of -56 stored as: (-56 + 127) = 71 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 21 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 22 Interpretation: Normal Case Interpretation: Zero Exponent Field: not all 0's or all 1's Fraction Field: Any Exponent Field: all 0's Fraction Field: all 0's ± (1.fraction) 2 (exponent bias) 0 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 23 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 24 4

Interpretation: Tiny Numbers Interpretation: Infinity Exponent Field: all 0's Fraction Field: Any Exponent Field: All 1's Fraction Field: 0 ± (0.fraction) 2 (1 bias) ± infinity 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 25 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 26 Interpretation: Invalid Numbers Interpretation: Invalid Numbers Exponent Field: All 1's Fraction Field: Not 0 NaN 1 / 0 Not a number (NaN) Naan 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 27 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 28 Something Else About Numbers Let's Encode Some Numbers! The number 36.74 is... 10 2 10 1 10 0 100 10 1 0 3 6 10-1 1/10 7 10-2 1/100 4 This is actually fun! = (3 10) + (6 1) + 7/10 + 4/100 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 30 5

Binary Fractions! Let's Encode 14.25 (32 bit) 5 and 3 / 8 is... 2 4 2 3 2 2 2 1 2 0 2-1 2-2 16 8 4 2 1 1/2 1/4 0 0 1 0 1 0 1 = 4 + 1 + 1/4 + 1/8 = 101.011 2-3 1/8 1 First, we need to convert 14.25 to its binary equivalent So, we need to calculate the integer part and fraction part of the number Everything after the decimal is a fraction 0.25 is actually 25 / 100 we need to find the base 2 equivalent (1/4) 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 31 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 32 Step 1: Convert to binary Step 2: Scientific Notation 14 1110 0.25 1/4 0.01 binary 01 / 100 IEEE stores the data in scientific notation So we move the "binary point" over Hence: 14.25 1110.01 1110.01 1.11001 2 3 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 33 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 34 Step 2: Scientific Notation Step 3: Encode In binary scientific notation, the leading digit is always going to be 1 Why store it? IEEE doesn't. Only data after the point is encoded Fraction 1.11001 2 3 (1 +.11001) 2 3 3 + 127 bias 11001 + padding zeroes 0 for positive 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 35 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 36 6

Result: 14.25 (32 bit) Result: 14.25 (32 bit) The following is the encoded version of 14.25 The rules are similar for double-precision We can also convert this into bytes Note: the floating point fields don't really "fit" cleanly into actual bytes 0 10000010 11001000000000000000000 01000001 01100100 00000000 00000000 41 64 00 00 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 37 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 38 Example 2: Encode 13.75 (32 bit) Step 1: Convert to binary First, we need to convert 13.75 to its binary equivalent So, we need to calculate the integer part and fraction part of the number Everything after the decimal is a fraction 0.75 is actually 75 / 100 we need to find the base 2 equivalent (3/4) 13 1101 0.75 3/4 0.11 Hence: 13.75 1101.11 binary 11 / 100 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 39 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 40 Step 2: Scientific Notation Step 2: Scientific Notation IEEE stores the data in scientific notation So we move the "binary point" over In binary scientific notation, the leading digit is always going to be 1 Why store it? IEEE doesn't. Only data after the point is encoded Fraction 1101.11 1.10111 2 3 1.10111 2 3 (1 +.10111) 2 3 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 41 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 42 7

Step 3: Encode Result: 13.75 (32 bit) The following is the encoded version of 13.75 The rules are similar for double-precision 3 + 127 bias 10111 + padding zeroes 0 for positive 0 10000010 10111000000000000000000 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 43 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 44 Result: 13.75 (32 bit) More Single-Precision Examples We can also convert this into bytes Note: the floating point fields don't really "fit" cleanly into actual bytes 01000001 01011100 00000000 00000000 41 5C 00 00 Zero: 0 00000000 00000000000000000000000 Positive Infinity: 0 11111111 00000000000000000000000 Negative Infinity: 1 11111111 00000000000000000000000 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 45 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 46 Floats Are Tuples Tuples and Floats Just like regular binary numbers, floating-point numbers of tuples They consist of three fields making them 3-tuples Its all set theory, folks! (sign, exponent, fraction) 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 48 8

Encoding of 14.25 Set Notation of 14.25 Sign is a 1-tuple Exponent is a 8-tuple Fraction is a 23-tuple 0 10000010 11001000000000000000000 ( (0), (1,0,0,0,0,0,1,0), (1,1,0,0,1,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0) ) 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 49 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 50 Bit Vectors Sets and Bits Bit Vectors A bit vector is a way to store sets using bits Also known as a bit array, bit set, and bit map Compact format that can perform a set operations with a single operation (fast!) 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 52 Bit Vectors Example 1 Each object in the universe is given a single bit in the bit array If the x A, then that bit is 1, otherwise 0 Order is important, so this is a tuple approach U = { fry, bender, farnsworth, leela, zoidberg} A = { fry, bender, leela } U = 11111 A = 11010 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 53 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 54 9

Example 2 Why this is useful U = { 2, 3, 5, 7, 11, 13, 17, 19} A = { 3, 5, 11, 19 } U = 11111111 A = 01101001 Computers can easily perform and & or operations on bytes (or multiple bytes) This means set operations can be performed amazingly fast 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 55 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 56 Let's look at the definitions again Let's look at the definitions again The definitions of union and intersection are nearly identical The relationship between the elements is defined using an and or or We can apply a bit-wise-and & a bit-wise-or to our bit array It will apply the operation to each of the bits in matching columns A B = { x x A or x B } A B = { x x A and x B } A B = { x x A or x B } A B = { x x A and x B } 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 57 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 58 Let's look at the definitions again Example: Union (using or) So, each bit in A will be compared to its matching bit in B Bit match can do sets! U = {a,b,c,d,e,f,g} A = 0111000 = {b,c,d} B = 0001110 = {d,e,f} A B = { x x A or x B } A B = { x x A and x B } 0111000 or 0001110 0111110 = {b,c,d,e,f} 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 59 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 60 10

Example: Intersection (using and) Complement U = {a,b,c,d,e,f,g} A = 0111000 = {b,c,d} B = 0001110 = {d,e,f} Then, how do we do a complement of a set A? We must flip all the bits from 1 to 0, and 0 to 1 We can use a binary-not or the XOR operation 0111000 and 0001110 0001000 = {d} A' = { x x A } 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 61 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 62 Java/C Code Exclusion Intersection : a & b Union : a b Complement : ~a && and are Boolean. These are bit-wise. Finally, how do we do set difference? The subtract operator will not work Let's look at the definition a bit more closely A - B = { x x A and x B } 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 63 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 64 Exclusion Java/C Code It's essentially the definition of intersection Except, the second operand is the definition of complement. Just complement the second operand Exclusion : a & ~b A - B = { x x A and x B } 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 65 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 66 11

Limits of Bit Vectors CSC 130 is waiting for you! Bit vectors, while useful, do have some notable limitations They only work on finite, countable sets For all other cases, you will have to work use a more advanced ADT For most cases, a very sophisticated list or tree can be used You will need to know: lists / trees sorting binary-searches Big-O 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 67 2/2/2018 Sacramento State - Cook - CSc 28 - Spring 2018 68 12