Borrowed from lecturer notes by Omid Jafarinezhad Fundamentals of Programming (C) Group 8 Lecturer: Vahid Khodabakhshi Lecture Number Systems Department of Computer Engineering
Outline Numeral Systems Computer Data Storage Units Numeral Systems Conversion Calculations in Number Systems Signed Integer Representation Fractional and Real Numbers ASCII Codes Department of Computer Engineering /
Numeral Systems Decimal number system (base ) Binary number system (base ) Computers are built using digital circuits Inputs and outputs can have only two values: and or True (high voltage) or false (low voltage) Writing out a binary number such as is tedious, and prone to errors Octal and hex are a convenient way to represent binary numbers, as used by computers Octal number system (base 8) Hexadecimal number system (base 6) Department of Computer Engineering /
Numeral Systems Decimal Binary Octal Hexadecimal Base B : digit B - -------------------------------------------- Base : digit 9 ( -) Base : digit (-) Base 8 : digit (8 -) Base 6 : digit 5 (6 -) 5 6 5 6 5 6 8 8 9 9 A (decimal value of ) B (decimal value of ) C (decimal value of ) D (decimal value of ) E (decimal value of ) F (decimal value of 5) Department of Computer Engineering /
Computer Data Storage Units Bit OR Each bit can only have a binary digit value: or basic capacity of information in computer A single bit must represent one of two states: = How many state can encode by N bit? Department of Computer Engineering 5/
Department of Computer Engineering 6/ Binary Encoding 5 6 5 6 8 9 5
Department of Computer Engineering / Computer Data Storage Units Byte: A sequence of eight bits or binary digits 8 = 56 (..55) smallest addressable memory unit 5 6 Bit Order One Bit Memory Address
Computer Data Storage Units Kilo byte: b = b ~ b = x = KB = 8 b 6 = 6 x = 6 KB = 6556 b Mega byte: b = b = x = M Giga byte:. Department of Computer Engineering 8/
Numeral Systems Conversion Convert from Base-B to Base-: (A) B = (?) () = () (.) = (?) (56) 8 = (?) (b) 6 = (?) Convert from Base- to Base-B: (N) = (?) B () = (?) (9) = (?) 8 (96) = (?) 6 Department of Computer Engineering 9/
Convert from Base-B to Base-. Define bit order.. 6 5 - - -.... Example : Base- to Base- 6 5 - - - -. Department of Computer Engineering /
Convert from Base-B to Base-. Calculate Position Weight B bit order Decimal Point - - s s s /s /s 9 8. 5 6 Example : Base- to Base- Position Weight B = 6 5 - - - - 6 5 - - - -. Department of Computer Engineering /
Convert from Base-B to Base-. Multiplies the value of each digit by the value of its position weight 8 6 6 8.5.5.5.65 6 5 - - - -. 8 6 6 8.5.5.5.65 8 6 8..5.65 Department of Computer Engineering /
Convert from Base-B to Base-. Adds the results of each section 8 6 6 8.5.5.5.65 6 5 - - - -. 8 6 6 8.5.5.5.65 8 6 8..5.65.5 +.5 Department of Computer Engineering /
Convert from Base-B to Base- Examples: (a n- a n- a. a - a -m ) B = (N) N = (a n- B n- ) + (a n- B n- ) + + (a B ) + (a - B - ) + + (a -m B m ) () = ( ) + ( ) + ( ) + ( ) = () (56) 8 = ( 8 ) + ( 8 ) + (58 ) + (68 ) = (9) (b) 6 = ( 6 ) + (6 ) + (6 ) = (96) (E6.A) 6 = ( 6 ) + (6 ) + (66 ) + ( ( / 6)) + ( ( / (6 6))) = (?) 6-6 - Department of Computer Engineering /
Convert from Base- to Base-B (N) = ( a n- a n- a. a - a -m ) B Integer part Fraction part. Convert integer part to Base-B Consecutive divisions. Convert fraction part to Base-B Consecutive multiplication 5.5 5.5 Department of Computer Engineering 5/
Convert Integer Part to Base-B Repeat until the quotient reaches Write the reminders in the reverse order Last to first Examples: ( 5 ) = () 5 Department of Computer Engineering 6/ 6 6
Convert Integer Part to Base-B Examples: 9 88 6 8 6 56 5 8 8 96 9 6 59 8 6 6 (9) = (56) 8 (96) = (B) 6 Department of Computer Engineering /
Convert Fraction Part to Base-B Do While multiply fraction part by B (the result) drop the integer part of the result (new fraction) (result = ) OR (reach to specific precision) the integral parts from top to bottom are arranged from left to right after the decimal point Department of Computer Engineering 8/
Convert Fraction Part to Base-B Example:.5 =.5.5 =.5 (.5) = (.).5 =... =. Department of Computer Engineering 9/
Convert Fraction Part to Base-B Example:.6 =.. =. (.6) = (.). =.8.8 =.6.6 = Department of Computer Engineering /
Conversion binary and octal Binary to Octal = 8 Octal 5 6 Binary Each digit in octal format: digits in binary format If the number of digits is not dividable by, add additional zeros: a a a a. a - a - = = =. =. (.) = (.) = (.6) 8 6 Department of Computer Engineering /
Conversion binary and octal Octal to Binary Substitute each digit with binary digits (5) 8 = () () 8 = () (5) 8 = ( ) (.6) 8 = (. ) Department of Computer Engineering /
Conversion binary and Hexadecimal Binary to Hexadecimal = 6 Each digit in octal format: digits in binary format If the number of digits is not dividable by, add additional zeros: a a a a. a - a - (.) 6 = (.) 6 = (D.6) D 6 Department of Computer Engineering /
Conversion binary and Hexadecimal Hexadecimal to Binary Substitute each digit with binary digits (F5.) 6 = (. ). Department of Computer Engineering /
Conversion Octal binary Hexadecimal (5) 8 = (E5) 6 (5) 8 = () = () = (E5) 6 5 E 5 (FA5) 6 = () = () = (65) 8 = (65) 8 Department of Computer Engineering 5/
Calculations in Numeral Systems Addition Binary ( + ) () = () carried digits () () (6) + + + Department of Computer Engineering 6/
Calculations in Numeral Systems Addition Hexadecimal Octal 5 6 8 B D A 8 B D A B 5 E (8 + D ) 6 (8 + ) = () = (5) 6 ( + 6 ) () = () 8 6 Department of Computer Engineering /
Calculations in Numeral Systems Subtraction Binary Borrowed digit () = () Borrowed digits () () (6) Department of Computer Engineering 8/
Calculations in Numeral Systems Subtraction Hexadecimal 6 + = 6 6 + = 9 5 + 6 = 5 9 6 Octal C F 6+ 8 = 6 Department of Computer Engineering 9/
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 Department of Computer Engineering /
Sign-magnitude One sign bit + magnitude bits Positive: s = Negative: s= Range = {(-).. (+) } Two ways to represent zero: (+) ( ) Examples: (+ ) = (- ) = s Sign 6 5 Magnitude How many positive and negative integers can be represented using N bits? Positive: N- - Negative: N- - Department of Computer Engineering /
Two s Complement Negative numbers:. Invert all the bits through the number ~() = ~() =. Add one Example: + = - =? ~() + Only one zero () Range = {.. 8} - - Negative two's complement () -() = - ~() + Department of Computer Engineering /
ASCII Codes American Standard Code for Information Interchange First decision: bits for representation Final decision: 8 bits for representation 56 characters ASCII ( P ) = (5) 6 ASCII ( = ) = (D) 6 row number column number Hexadecimal 5 6 NUL DLE @ P ` p SOH DC! A Q a q STX DC " B R b r ETX DC # C S c s EOT DC $ D T d t 5 ENQ NAK % 5 E U e u 6 ACK SYN & 6 F V f v BEL ETB ' 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 Department of Computer Engineering /
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 subtraction Signed Integer Representation Sing-magnitude: one sign bit + magnitude bits Two s complement : (-N) = ~(N) + Fractional and Real Numbers ASCII Codes Department of Computer Engineering /