Fundamentals of Programming Lecture 2 Number Systems & Arithmetic Lecturer : Ebrahim Jahandar Some Parts borrowed from slides by IETC1011-Yourk University
Common Number Systems System Base Symbols Used by humans? Used in computers? Decimal 10 0, 1, 9 Yes No Binary 2 0, 1 No Yes Octal 8 0, 1, 7 No No Hexa-decimal 16 0, 1, 9, A, B, F Yes No 2
Quantities/Counting (1 of 3) Decimal Binary Octal Hexa-decimal 0 0 0 0 1 1 1 1 2 10 2 2 3 11 3 3 4 100 4 4 5 101 5 5 6 110 6 6 7 111 7 7 3
Quantities/Counting (2 of 3) Decimal Binary Octal Hexa-decimal 8 1000 10 8 9 1001 11 9 10 1010 12 A 11 1011 13 B 12 1100 14 C 13 1101 15 D 14 1110 16 E 15 1111 17 F 4
Quantities/Counting (3 of 3) Decimal Binary Octal Hexa-decimal 16 10000 20 10 17 10001 21 11 18 10010 22 12 19 10011 23 13 20 10100 24 14 21 10101 25 15 22 10110 26 16 23 10111 27 17 5
The possibilities: Conversion Among Bases Decimal Octal Binary Hexadecimal pp. 40-46 6
Quick Example 25 10 = 11001 2 = 31 8 = 19 16 Base 7
Decimal to Decimal (just for fun) Decimal Octal Binary Hexadecimal Next slide 8
Weight 125 10 => 5 x 10 0 = 5 2 x 10 1 = 20 1 x 10 2 = 100 125 Base 9
Binary to Decimal Decimal Octal Binary Hexadecimal 10
Technique Binary to Decimal Multiply each bit by 2 n, where n is the weight of the bit The weight is the position of the bit, starting from 0 on the right Add the results 11
Bit 0 Example 101011 2 => 1 x 2 0 = 1 1 x 2 1 = 2 0 x 2 2 = 0 1 x 2 3 = 8 0 x 2 4 = 0 1 x 2 5 = 32 43 10 12
Hexadecimal to Decimal Decimal Octal Binary Hexadecimal 13
Technique Hexadecimal to Decimal Multiply each bit by 16 n, where n is the weight of the bit The weight is the position of the bit, starting from 0 on the right Add the results 14
Example ABC 16 => C x 16 0 = 12 x 1 = 12 B x 16 1 = 11 x 16 = 176 A x 16 2 = 10 x 256 = 2560 2748 10 15
Decimal to Binary Decimal Octal Binary Hexadecimal 16
Technique Decimal to Binary Divide by two, keep track of the remainder First remainder is bit 0 (LSB, least-significant bit) Second remainder is bit 1 Etc. 17
Example 125 10 =? 2 2 125 2 62 1 2 31 0 2 15 1 2 7 1 2 3 1 2 1 1 0 1 125 10 = 1111101 2 18
Hexadecimal to Binary Decimal Octal Binary Hexadecimal 19
Technique Hexadecimal to Binary Convert each hexadecimal digit to a 4-bit equivalent binary representation 20
10AF 16 =? 2 Example 1 0 A F 0001 0000 1010 1111 10AF 16 = 0001000010101111 2 21
Decimal to Octal Decimal Octal Binary Hexadecimal 22
Technique Divide by 8 Keep track of the remainder Decimal to Octal 23
1234 10 =? 8 Example 8 1234 8 154 2 8 19 2 8 2 3 0 2 1234 10 = 2322 8 24
Decimal to Hexadecimal Decimal Octal Binary Hexadecimal 25
Technique Decimal to Hexadecimal Divide by 16 Keep track of the remainder 26
1234 10 =? 16 Example 16 1234 16 77 2 16 4 13 = D 0 4 1234 10 = 4D2 16 27
Binary to Octal Decimal Octal Binary Hexadecimal 28
Technique Binary to Octal Group bits in threes, starting on right Convert to octal digits 29
1011010111 2 =? 8 Example 1 011 010 111 1 3 2 7 1011010111 2 = 1327 8 30
Binary to Hexadecimal Decimal Octal Binary Hexadecimal 31
Technique Binary to Hexadecimal Group bits in fours, starting on right Convert to hexadecimal digits 32
1010111011 2 =? 16 Example 10 1011 1011 2 B B 1010111011 2 = 2BB 16 33
Exercise Convert... Decimal Binary Octal Hexa-decimal 33 1110101 703 1AF 34
Exercise Convert Decimal Binary Octal Hexa-decimal 33 100001 41 21 117 1110101 165 75 451 111000011 703 1C3 431 110101111 657 1AF 35
Common Powers (1 of 2) Base 10 Power Preface Symbol 10-12 pico p 10-9 nano n 10-6 micro 10-3 milli m 10 3 kilo k 10 6 mega M 10 9 giga G 10 12 tera T Value.000000000001.000000001.000001.001 1000 1000000 1000000000 1000000000000 36
Common Powers (2 of 2) Base 2 Power Preface Symbol 2 10 kilo k 2 20 mega M 2 30 Giga G Value 1024 1048576 1073741824 What is the value of k, M, and G? In computing, particularly w.r.t. memory, the base-2 interpretation generally applies 37
Review multiplying powers For common bases, add powers a b a c = a b+c 2 6 2 10 = 2 16 = 65,536 or 2 6 2 10 = 64 2 10 = 64k 38
Two 1-bit values Binary Addition (1 of 2) A B A + B 0 0 0 0 1 1 1 0 1 1 1 10 two 39
Two n-bit values Binary Addition (2 of 2) Add individual bits Propagate carries E.g., 1 1 10101 21 + 11001 + 25 101110 46 40
Decimal (just for fun) Multiplication (1 of 3) 35 x 105 175 000 35 3675 41
Multiplication (2 of 3) Binary, two 1-bit values A B A B 0 0 0 0 1 0 1 0 0 1 1 1 42
Multiplication (3 of 3) Binary, two n-bit values As with decimal values E.g., 1110 x 1011 1110 1110 0000 1110 10011010 43
Signed Integer Representation Negative numbers must be encode in binary number systems Well-known methods Sign-magnitude One s Complement Two s Complement Which one is better? Calculation speed Complexity of the computation circuit 44
Sign-magnitude One sign bit + magnitude bits Positive: s = 0 Negative: s= 1 Range = {(-127) 10.. (+127) 10 } Two ways to represent zero: 00000000 (+0) 10000000 ( 0) Examples: (+ 43) 10 = 00101011 (- 43) 10 = 10101011 7 s Sign 6 5 4 3 2 Magnitude 1 0 How many positive and negative integers can be represented using N bits? Positive: 2 N-1-1 Negative: 2 N-1-1 45
Two s Complement Negative numbers: 1. Invert all the bits through the number ~(0) = 1 ~(1) = 0 2. Add one Example: +1 = 00000001-1 =? ~(00000001) 11111110 11111110 + 1 11111111 Only one zero (00000000) Range = {127.. 128} 46
A proposition is a statement that can be either true or false The sky is blue I is a English major x == y Not propositions: Are you Bob? x = 7 Boolean propositions 47
We use Boolean variables to refer to propositions Usually are lower case letters starting with p (i.e. p, q, r, s, etc.) A Boolean variable can have one of two values true (T) or false (F) A proposition can be Boolean variables A single variable: p An operation of multiple variables: p (q r) 48
Introduction to Logical Operators About a dozen logical operators Similar to algebraic operators + * - / In the following examples, p = Today is Friday q = Today is my birthday 49
Logical operators: Not A not operation switches (negates) the truth value Symbol: or ~ In C++ and Java, the operand is! p = Today is not Friday p T F p F T 50
Logical operators: And An and operation is true if both operands are true Symbol: It s like the A in And In C++ and Java, the operand is && p q = Today is Friday and today is my birthday p q p q T T T T F F F T F F F F 51
Logical operators: Or An or operation is true if either operands are true Symbol: In C++ and Java, the operand is p q = Today is Friday or today is my birthday (or possibly both) p q p q T T T T F T F T T F F F 52
Logical operators: Exclusive Or An exclusive or operation is true if one of the operands are true, but false if both are true Symbol: Often called XOR p q (p q) (p q) In Java, the operand is ^ (but not in C++) p q = Today is Friday or today is my birthday, but not both p q p q T T F T F T F T T F F F 53
ASCII Codes American Standard Code for Information Interchange First decision: 7 bits for representation Final decision: 8 bits for representation 256 characters ASCII ( P ) = (50) 16 ASCII ( = ) = (3D) 16 row number column number Hexadecimal 0 1 2 3 4 5 6 7 0 NUL DLE 0 @ P ` p 1 SOH DC1! 1 A Q a q 2 STX DC2 " 2 B R b r 3 ETX DC3 # 3 C S c s 4 EOT DC4 $ 4 D T d t 5 ENQ NAK % 5 E U e u 6 ACK SYN & 6 F V f v 7 BEL ETB ' 7 G W g w 8 BS CAN ) 8 H X h x 9 TAB EM ( 9 I Y i y A LF SUB * : J Z j z B VT ESC + ; K ] k } C FF FS, > L \ l D CR GS - = M [ m { E SO RS. < N ^ n ~ F SI US /? O _ o 54
Summary Numeral Systems Decimal, Binary, Octal, Hexadecimal Computer Data Storage Units Bit, Byte, Kilo byte, Giga byte, Numeral Systems Conversion Convert between different bases Calculations in Number Systems Addition and multiplication Boolean Algebra Boolean Operations Signed Integer Representation Sing-magnitude: one sign bit + magnitude bits Two s complement : (-N) = ~(N) + 1 ASCII Codes 55