Presentation is loading. Please wait.

Presentation is loading. Please wait.

SAK3207 - Chapter 21 Chapter 2 : Number System 2.1 Decimal, Binary, Octal and Hexadecimal Numbers 2.2 Relation between binary number system with other.

Similar presentations


Presentation on theme: "SAK3207 - Chapter 21 Chapter 2 : Number System 2.1 Decimal, Binary, Octal and Hexadecimal Numbers 2.2 Relation between binary number system with other."— Presentation transcript:

1 SAK3207 - Chapter 21 Chapter 2 : Number System 2.1 Decimal, Binary, Octal and Hexadecimal Numbers 2.2 Relation between binary number system with other number system 2.3 Representation of integer, character and floating point numbers in binary 2.4 Binary Arithmetic 2.5 Arithmetic Operations for One’s Complement, Two’s Complement, magnitude and sign and floating point number

2 SAK3207 - Chapter 22 2.1) Decimal, Binary, Octal and Hexadecimal Numbers Introduction Most numbering system use positional notation : N = a n r n + a n-1 r n-1 + … + a 1 r 1 + a 0 r 0 Where:N: an integer with n+1 digits r: base a i  {0, 1, 2, …, r-1}

3 SAK3207 - Chapter 23 Example: a) r = 10 (base 10) => decimal numbers symbol : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (10 different symbols) N = 278 => n = 2; a 2 = 2; a 1 = 7; a 0 = 8 = (2 x 10 2 ) + (7 x 10 1 ) + (8 x 10 0 ) HundredsOnes Tens

4 SAK3207 - Chapter 24 b) r = 2 (base-2) => binary numbers symbol: 0, 1 (2 different symbols) N = 1001 2 => n = 3; a 3 = 1; a 2 = 0; a 1 = 0; a 0 = 1 = (1 x 2 3 ) + (0 x 2 2 ) + (0 x 2 1 ) + (1 x 2 0 ) c) r = 8 (base-8) => Octal numbers symbol : 0, 1, 2, 3, 4, 5, 6, 7, (8 different symbols) N = 263 8 => n = 2; a 2 = 2; a 1 = 6; a 0 = 3 = (2 x 8 2 ) + (6 x 8 1 ) + (3 x 8 0 )

5 SAK3207 - Chapter 25 d) r = 16 (base-16) => Hexadecimal numbers symbol : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F (16 different symbols) N = 263 16 => n = 2; a 2 = 2; a 1 = 6; a 0 = 3 = (2 x 16 2 ) + (6 x 16 1 ) + (3 x 16 0 )

6 SAK3207 - Chapter 26 DecimalBinaryOctalHexadecimal 0000 1111 21022 31133 410044 510155 611066 711177 81000108 91001119 10101012A 11101113B 12110014C 13110115D 14111016E 15111117F 16100002010 There are also non-positional numbering systems. Example: Roman Number System 1987 = MCMLXXXVII

7 SAK3207 - Chapter 27 2.2) Relation between binary number system and others Binary and Decimal Converting a decimal number into binary Divide the decimal number by 2 and take its remainder The process is repeated until it produces the result of 0 The binary number is obtained by taking the remainder from the bottom to the top

8 SAK3207 - Chapter 28 Example: 53 10 => 53 / 2 = 26 remainder 1 26 / 2 = 13 remainder 0 13 / 2 = 6 remainder 1 6 / 2 = 3 remainder 0 3 / 2 = 1 remainder 1 1 / 2 = 0 remainder 1 = 110101 2 (6 bits) = 00110101 2 (8 bits) (note: bit = binary digit) Read from the bottom to the top

9 SAK3207 - Chapter 29 a) Converting a binary number into decimal Multiply each bit in the binary number with the weight (or position) Add up all the results of the multiplication performed The desired decimal number is the total of the multiplication results performed

10 SAK3207 - Chapter 210 Example: a) 111001 2 (6 bits) => (1x2 5 ) + (1x2 4 ) + (1x2 3 ) + (0x2 2 ) + (0x2 1 ) + (1x2 0 ) = 32 + 16 + 8 + 0 + 0 + 1 = 57 10 b) 00011010 2 (8 bits) = 2 4 + 2 3 +2 1 = 16 + 8 + 2 = 26 10

11 SAK3207 - Chapter 211 b) Binary and Octal Theorem If base R 1 is the integer power of other base, R 2, i.e. R 1 = R 2 d Every group of d digits in R 2 is equivalent to 1 digit in the R 1 base (Note: This theorem is used to convert binary numbers to octal and hexadecimal or the other way round)

12 SAK3207 - Chapter 212 From the above theorem, assume that R 1 = 8 (base-8) octal R 2 = 2 (base-2) binary From the theorem above, R 1 = R 2 d 8 = 2 3 So, 3 digits in base-2 (binary) is equivalent to 1 digit in base-8 (octal)

13 SAK3207 - Chapter 213 From the stated theorem, the following is a binary-octal conversion table. BinaryOctal 0000 0011 0102 0113 1004 1015 1106 1117 In a computer system, the conversion from binary to octal or otherwise is based on the conversion table above.

14 SAK3207 - Chapter 214 Example: Convert these binary numbers into octal numbers: (a)00101111 2 (8 bits)(b) 11110100 2 (8 bits) Refer to the binary-octal conversion table above 000 101 111 = 57 8 057 Refer to the binary-octal conversion table above 11 110 100 = 364 8 364

15 SAK3207 - Chapter 215 1. Convert the following octal numbers into hexadecimal numbers (16 bits) (a)65 8 (b) 123 8 Refer to the binary-octal conversion table above 6 8 5 8 110 101 0000 0000 0011 0101 2 0 0 3 5 = 35 16 Refer to the binary-octal conversion table above 1 8 2 8 3 8 001 010 011 0000 0000 0101 0011 2 0 0 5 3 = 53 16

16 SAK3207 - Chapter 216 c) Binary and Hexadecimal The same method employed in binary-octal conversion is used once again. Assume that: R 1 = 16 (hexadecimal) R 2 =2 (binary) From the theorem: 16 = 2 4 Hence, 4 digits in a binary number is equivalent to 1 digit in the hexadecimal number system (and otherwise) The following is the binary-hexadecimal conversion table

17 SAK3207 - Chapter 217 BinaryHexadecimal 00000 00011 00102 00113 01004 01015 01106 01117 10008 10019 1010A 1011B 1100C 1101D 1110E 1111F Example: 1.Convert the following binary numbers into hexadecimal numbers: (a)00101111 2 Refer to the binary- hexadecimal conversion table above 0010 1111 2 =2F 16 2 F

18 SAK3207 - Chapter 218 2. Convert the following hexadecimal numbers into binary numbers (a)12B 16 (b) ABCD 16 Refer to the binary- hexadecimal conversion table above 1 2 B 16 0001 0010 1011 2 (12 bits) = 000100101011 2 Refer to the binary- hexadecimal conversion table above A B C D 16 1010 1011 1101 1110 2 = 1010101111011110 2

19 SAK3207 - Chapter 219 2.3) Representation of integer, character and floating point numbers in binary a) Introduction - Machine instructions operate on data. The most important general categories of data are: 1. Addresses – unsigned integer 2. Numbers – integer or fixed point, floating point numbers and decimal (eg, BCD (Binary Coded Decimal)) 3. Characters – IRA (International Reference Alphabet), EBCDIC (Extended Binary Coded Decimal Interchange Code), ASCII (American Standard Code for Information Interchange) 4. Logical Data - Those commonly used by computer users/programmers: signed integer, floating point numbers and characters

20 SAK3207 - Chapter 220 b) Signed Integer Representation Signed integers are usually used by programmers Unsigned integers are used for addressing purposes in the computer (especially for assembly language programmers) Three representations of signed integers: 1. Sign-and-Magnitude 2. Ones Complement 3. Twos Complement

21 SAK3207 - Chapter 221 i) Sign-and-Magnitude The easiest representation The leftmost bit in the binary number represents the sign of the number. 0 if positive and 1 if negative The balance bits represent the magnitude of the number.

22 SAK3207 - Chapter 222 Examples: i) 8 bits binary number __ __ __ __ __ __ __ __ 7 bits for magnitude (value) a) +7 = 0 0 0 0 0 1 1 1 b) –10 = 1 0 0 0 1 0 1 0 (–7 = 10000111 2 ) (+10 = 00001010 2 ) Sign bit 0 => +ve 1 => –ve

23 SAK3207 - Chapter 223 ii) 6 bits binary number __ __ __ __ __ __ 5 bits for magnitude (value) a) +7 = 0 0 0 1 1 1 b) –10 = 1 0 1 0 1 0 (–7 = 1 0 0 1 1 1 2 ) (+10 = 0 0 1 0 1 0 2 ) Sign bit 0 => +ve 1 => –ve

24 SAK3207 - Chapter 224 ii) Ones Complement In the ones complement representation, positive numbers are same as that of sign-and- magnitude Example: +5 = 00000101 (8 bit)  as in sign-and-magnitude representation Sign-and-magnitude and ones complement use the same representation above for +5 with 8 bits and all positive numbers. For negative numbers, their representation are obtained by changing bit 0 → 1 and 1 → 0 from their positive numbers

25 SAK3207 - Chapter 225 Example: Convert –5 into ones complement representation (8 bit) Solution: First, obtain +5 representation in 8 bits  00000101 Change every bit in the number from 0 to 1 and vice-versa. –5 10 in ones complement is 11111010 2

26 SAK3207 - Chapter 226 Exercise: Get the representation of ones complement (6 bit) for the following numbers: i)+7 10 ii) –10 10 Solution: (+7) = 000111 2 Solution: (+10) 10 = 001010 2 So, (-10) 10 = 110101 2

27 SAK3207 - Chapter 227 iii) Twos complement Similar to ones complement, its positive number is same as sign-and-magnitude Representation of its negative number is obtained by adding 1 to the ones complement of the number.

28 SAK3207 - Chapter 228 Example: Convert –5 into twos complement representation and give the answer in 8 bits. Solution:  First, obtain +5 representation in 8 bits  00000101 2  Obtain ones complement for –5  11111010 2  Add 1 to the ones complement number:  11111010 2 + 1 2 = 11111011 2  –5 in twos complement is 11111011 2

29 SAK3207 - Chapter 229 Exercise: Obtain representation of twos complement (6 bit) for the following numbers i)+7 10 ii)–10 10 Solution: (+7) = 000111 2 Solution: (+10) 10 = 001010 2 (-10) 10 = 110101 2 + 1 2 = 110110 2 So, twos compliment for –10 is 110110 2

30 SAK3207 - Chapter 230 c) Character Representation For character data type, its representation uses codes such as the ASCII, IRA or EBCDIC. Note: Students are encouraged to obtain the codes

31 SAK3207 - Chapter 231 d) Floating point representation In binary, floating point numbers are represented in the form of : +S x B +E and the number can be stored in computer words with 3 fields: i) Sign (+ve, –ve) ii) Significand S iii) Exponent E and B is base is implicit and need not be stored because it is the same for all numbers (base-2).

32 SAK3207 - Chapter 232

33 SAK3207 - Chapter 233 2.4) Binary Arithmetics 1. Addition ( + ) 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 10 1 + 1 + 1 = (1 + 1) + 1 = 10 + 1 = 11 2 Example: i. 010111 2 + 011110 2 = 110101 2 ii. 100011 2 + 011100 2 = 111111 2

34 SAK3207 - Chapter 234 2. Multiplication ( x ) 0 x 0 = 0 0 x 1 = 0 1 x 0 = 0 1 x 1 = 1 3. Subtraction ( – ) 0 – 0 = 0 0 – 1 = 1 (borrow 1) 1 – 0 = 1 1 – 1 = 0

35 SAK3207 - Chapter 235 4. Division ( / ) 0 / 1 = 0 1 / 1 = 1 Example: i. ii.

36 SAK3207 - Chapter 236 Example: i. 010111 2 - 001110 2 = 001001 2 ii. 100011 2 - 011100 2 = 000111 2 Exercise: i. 1000100 – 010010 ii. 1010100 + 1100 iii. 110100 – 1001 iv. 11001 x 11 v. 110111 + 001101 vi. 111000 + 1100110 vii. 110100 x 10 viii. 11001 - 1110

37 SAK3207 - Chapter 237 2.5) Arithmetic Operations for Ones Complement, Twos Complement, sign-and- magnitude and floating point number Addition and subtraction for signed integers Reminder: All subtraction operations will be changed into addition operations Example: 8 – 5 = 8 + (–5) –10 + 2 = (–10) + 2 6 – (–3) = 6 + 3

38 SAK3207 - Chapter 238 1. Sign-and-Magnitude Z = X + Y  There are a few possibilities: i.If both numbers, X and Y are positive oJust perform the addition operation Example: 5 10 + 3 10 = 000101 2 + 000011 2 = 001000 2 = 8 10

39 SAK3207 - Chapter 239 ii. If both numbers are negative oAdd |X| and |Y| and set the sign bit = 1 to the result, Z Example: –3 10 – 4 10 = (–3) + (–4) = 100011 2 + 100100 2 Only add the magnitude, i.e.: 00011 2 + 00100 2 = 00111 2 Set the sign bit of the result (Z) to 1 (–ve) = 100111 2 = –7 10

40 SAK3207 - Chapter 240 iii. If signs of both number differ oThere will be 2 cases a) | +ve Number | > | –ve Number | Example: (–2) + (+4), (+5) + (–3)  Set the sign bit of the –ve number to 0 (+ve), so that both numbers become +ve.  Subtract the number of smaller magnitude from the number with a bigger magnitude

41 SAK3207 - Chapter 241 Sample solution: Change the sign bit of the –ve number to +ve (–2) + (+4) = 100010 2 + 000100 2 = 000100 2 – 000010 2 = 000010 2 = 2 10 b) | –ve Number | > | +ve Number |  Subtract the +ve number from the –ve number Example: (+3 10 ) + (–5 10 )= 000011 2 + 100101 2 = 100101 2 – 000011 2 = 100010 2 = –2 10

42 SAK3207 - Chapter 242 2. Ones complement In ones complement, it is easier than sign-and- magnitude  Change the numbers to its representation and perform the addition operation  However a situation called Overflow might occur when addition is performed on the following categories: 1. If both are negative numbers 2. If both are in difference sign and |+ve Number| > | –ve Number|

43 SAK3207 - Chapter 243 Overflow => the addition result exceeds the number of bits that was fixed 1. Both are –ve numbers Example: –3 10 – 4 10 = (–3 10 ) + (–4 10 ) Solution:  Convert –3 10 and –4 10 into ones complement representation +3 10 = 00000011 2 (8 bits) –3 10 = 11111100 2 +4 10 = 00000100 2 (8 bits) –4 10 = 11111011 2

44 SAK3207 - Chapter 244  Perform the addition operation (–3 10 ) => 11111100 (8 bit) + (–4 10 ) => 11111011 (8 bit) –7 10 111110111 (9 bit) 11110111 + 1 1111000 2 = –7 10 Overflow occurs. This value is called EAC and needs to be added to the rightmost bit. the answer

45 SAK3207 - Chapter 245 2. | +ve Number| > |–ve Number|  This case will also cause an overflow Example: (–2) + 4 = (–2) + (+4) Solution:  Change both of the numbers above into one’s complement representation –2 = 11111101 2 +4 = 00000100 2  Add both of the numbers (–2 10 ) => 11111101 (8 bit) + (+4 10 ) => 00000100 (8 bit) There is an EAC +2 10 100000001 (9 bit)

46 SAK3207 - Chapter 246  Add the EAC to the rightmost bit 00000001 + 1 00000010 2 = +2 10 the answer Note: For cases other than 1 & 2 above, overflow does not occur and there will be no EAC and the need to perform addition to the rightmost bit does not arise

47 SAK3207 - Chapter 247 3. Twos Complement Addition operation in twos complement is same with that of ones complement, i.e. overflow occurs if: 1.If both are negative numbers 2.If both are in difference and |+ve Number| > |–ve Number|

48 SAK3207 - Chapter 248 1. Both numbers are –ve Example: –3 10 – 4 10 = (–3 10 ) + (–4 10 ) Solution:  Convert both numbers into twos complement representation +3 10 = 000011 2 (6 bit) –3 10 = 111100 2 (one’s complement) –3 10 = 111101 2 (two’s complement) –4 10 = 111011 2 (one’s complement) –4 10 = 111100 2 (two’s complement) 

49 SAK3207 - Chapter 249 111100(–3 10 ) 111011(–4 10 ) = 111001 2 (two’s complement) = –7 10 Perform addition operation on both the numbers in twos complement representation and ignore the EAC. 1111001 Ignore the EAC The answer

50 SAK3207 - Chapter 250 Note: In two’s complement, EAC is ignored (do not need to be added to the leftmost bit, like that of one’s complement)

51 SAK3207 - Chapter 251 2. | +ve Number| > |–ve Number| Example: (–2) + 4 = (–2) + (+4) Solution:  Change both of the numbers above into twos complement representation –2 = 111110 2 +4 = 000100 2  Perform addition operation on both numbers (–2 10 ) => 111110 (6 bit) + (+4 10 ) => 000100 (6 bit) +2 10 1000010 Ignore the EAC

52 SAK3207 - Chapter 252 The answer is 000010 2­ = +2 10 Note: For cases other than 1 and 2 above, overflow does not occur. Exercise: Perform the following arithmetic operations in ones complement and also twos complement 1. (+2) + (+3)[6 bit] 2. (–2) + (–3)[6 bit] 3. (–2) + (+3)[6 bit] 4. (+2) + (–3)[6 bit] Compare your answers with the stated theory


Download ppt "SAK3207 - Chapter 21 Chapter 2 : Number System 2.1 Decimal, Binary, Octal and Hexadecimal Numbers 2.2 Relation between binary number system with other."

Similar presentations


Ads by Google