CS1001 Programming Fundamentals 3(3-0) Lecture 2 Binary Arithmetic and Over View of Software Development Lecture: Narong Wesnarat
Binary Arithmetic The individual digits of a binary number are referred to as bits Each bit represents a power of two Examples 01011 = 0 • 24 + 1 • 23 + 0 • 22 + 1 • 21 + 1 • 20 = 11 00010 = 0 • 24 + 0 • 23 + 0 • 22 + 1 • 21 + 0 • 20 = 2 00010 + 01011 01101 Binary addition 2 + 11 13 Equivalent decimal addition
Binary Arithmetic 0101 Binary Equivalent decimal multiplication × 0011 0000 0001111 5 × 3 15
Two’s Complement Convention for handling signed numbers in binary representation The leading bit is a sign bit Binary number with leading 0 is positive Binary number with leading 1 is negative Magnitude of positive numbers is just the binary representation Magnitude of negative numbers is found by performing the two’s complement Complement the bits Replace all the 1's with 0's, and all the 0's with 1's Add one to the complemented number Carry in most significant bit position is thrown away when performing arithmetic
Two's Complement Example Performing two's complement on the decimal 7 to get -7 Using a five-bit representation 7 = 00111 Convert to binary 11000 Complement the bits 11000 Add 1 to the complement + 00001 11001 Is -7 in two's complement (Answer)
Two's Complement Arithmetic Computing 8 - 7 using a two's complement representation with five-bit numbers In base 10 (Decimal System) 8 - 7 = 8 + (-7) = 1 Using Two’s Complement Arithmetic 01000 Two's complement of 8 11001 Two's complement of -7 01000 Add 8 and -7 + 11001 100001 00001 Is the five-bit result Throw away the high-order carry as we are using a five bit representation
Integer Constants Integer constants are positive or negative whole numbers Integer constant forms Decimal (base 10) Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Octal (base 8) Digits 0, 1, 2, 3, 4, 5, 6, 7 Hexadecimal (base 16) Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a , b, c, d, e, f Consider 31 oct and 25 dec
Hexadecimal Constants Letters represent the hex digits a or A - 10 d or D - 13 b or B - 11 e or E - 14 c or C - 12 f or F - 15 Examples (in C language) 0x2C 0XAC12EL The type of the constant depends on its size, unless the type specifier is used
1.4 The Software Development Method (Hanly’s Text book) Specify the problem requirements Analyse the problem Design the algorithm to solve the problem Implement the algorithm Test and verify the completed program Maintain and update the program
1.5 Applying the software Development Method CASE STUDY Converting Miles to Kilometers Problem Convert distances from miles into kilometers. Analysis DATA REQUIREMENTS Problem Inputs: miles /* the distance in miles */ Problem Output: kms /*the distance in kilomieters*/ Relevant Formula: 1 mile = 1.609 kilometers
1.5 Applying the software Development Method Design ALGORITHM Get the distance in miles Convert the distance to kilometers Display the distance in kilometers Altorithm with Refinements 2.1 The distance in kilometers is 1.609 times the distance in miles
1.5 Applying the software Development Method IMPLEMENTATION Figure 1.14 Miles-to-Kilometers Conversion Program
Basic Software Design Tools Simple program design tools Flowchart Pseuso-code Visit a link for complete Flowchart Symbols at the instructor’s Web site
Flowchart Analyzing the Flowchart (Checking your algorithm) Suppose we Enter N = 3, The process of Printing 1 -3 will be: Step 1 2 3 4 5 6 N I Printed on Paper 12 123 Decision No Yes Stop
Introduction Pseudo-Code Pseudo-code is a way to express an algorithm ... ... using a structured form of language ... ... that is (hopefully) unambiguous ... ... and resembles a programming language. There are no rules ... ... but there might be standards.
Introduction Pseudo-Code The Sequence (instructions in series) some statement another statement If-Then-Else statement (decision) If some condition is TRUE Then some statements Else another statement Else (maybe) no statement
Introduction Pseudo-Code While-Do statement (loop) WHILE the condition is TRUE do some statement another statement ENDWHILE or WEND Repeat-Until statement (loop) REPEAT UNTIL the condition is TRUE
Introduction Pseudo-Code Case statement (multiple decision) CASE condition is : value-1 : statement another statement value-2: statement value-3: statement ENDCASE
Introduction Pseudo-Code (more) Keywords: Read , Write used for input description (also: Input, Ouput) true , false used to state a logical result Add , Subtract Multiply , Divide used for calculations AND , OR , NOT used for combined Conditions Hints and Tips: In principal there are 3 Pseudocode structures: Sequence, Loop and Decision. With those three, you can describe any Algorithm (solution to a problem), using Pseudo-code.
Introduction Pseudo-Code Some examples If-Then_Else (decision) Problem: Read one number and check if it is positive! Pseudo-Code: Read number If number > zero Then Write “Number is Positive!” Else Write “Number is Negative!” This simple algorithm will read one Number as Input (from the keyboard if nothing else is specified), will check if that Number is Positive or not and will print the result (on the Monitor, if nothing else is specified)
Introduction Pseudo-Code The solution above will not solve all cases though! That’s because the number could also be zero, and then you cannot tell whether it is positive or negative! So we improve our solution to the problem by changing the PSEUDOCODE to: Read number If number > zero Then Write “Number is Positive!” Else If number = zero Then Write “Number is Zero!” Else Write “Number is Negative!”
Introduction Pseudo-Code Suppose we would like to use the CASE-statement here, then it would look like following: Read Number CASE Number is : > zero : Write “Number is Positive!” = Zero : Write “Number is zero!” < Zero : Write “Number is Negative!” ENDCASE
Structured Flow chart Flowchart should be structured These two flowchart gives the same result but the right one is better. Flowchart should be structured easier to implement in high level language easier to find errors Use only standard structures sequence IF… THEN … ELSE … REPEAT…. UNTIL ….. WHILE …. DO ….. ….. WEND
Flowchart & Pseudo-code comparison Read N I = 0 REPEAT Print I I = I + 1 UNTIL I = N