Presentation is loading. Please wait.

Presentation is loading. Please wait.

Numbers and Arithmetic and Logical Operation

Similar presentations


Presentation on theme: "Numbers and Arithmetic and Logical Operation"— Presentation transcript:

1 Numbers and Arithmetic and Logical Operation
Representation of Numbers and Arithmetic and Logical Operation on them

2 Representation of Numbers
In general, a number expressed in base r is written as (….. a3, a2, a1, a0)r Where the ai are the symbols of the number system. The base r tells us how many different symbols the ai can have For example: r = 10  ai e {0, 1, 2,3,4, 5,6 7, 8,9} r = 2  ai e {0, 1} r = 8  ai e {0, 1, 2,3,4, 5,6 7} r = 16  ai e {0, 1, 2,3,4, 5,6 7, 8,9, A, B, C, D, E, F}

3 The right most symbol of a0 is called the least significant digit (LSD) While the left most symbol of an-1 is called most significant digit (MSD) Example: MSD LSD

4 Value of Numbers In digital systems, the determination of value of signed numbers (positive and negative numbers) is different than unsigned numbers (just positive numbers) Value of Unsigned Numbers: The value of unsigned number Value {(….a3a2a1a0)r} = …. + a3r3 + a2r2+a1r1+a0r0 In which ai is the ith coefficient and r is the base Example: (15)10 = 1* *100 = (1010)2 = 1*23 + 0*22 + 1*21 + 0*20 = 8+2=(10)10 (B7)16 = B* *160 = 11* = (183)10

5 Conversion of Unsigned Numbers
In general, while the representation of a number in base x is not the same as representation of the same number in base y, it does have the same representation in base 10. So, to convert number N in base x [ written as (N)x ], to a number in base y [ written as (N)y ], we must first convert (N)x to its base 10 representation [(N)10 ], then equate this to base y representation and solve for coefficients. Therefore, to convert N from base X to base Y we can do: (N)x  (N)10  (N)y ….a3x3 + a2x2+a1x1+a0x0 = (N)10 = … b3y3 + b2y2+b1y1+b0y0

6 Therefore, to convert N from base x to base y we can do:
(N)x  (N)10  (N)y ….a3x3 + a2x2 + a1x1 + a0x0 = (N)10 = … b3y3 + b2y2 + b1y1 + b0y0 Given the base x number ( ….. a3, a2, a1, a0) x Find the base 10 value: ….a3x3 + a2x2 + a1x1+a0x0 = (N)10 Equate to base y expression: (N)10 = … b3y3 + b2y2 +b1y1+b0y0 Solve for coefficients (…b3b2b1b0) Write the number in base y They are known

7 Binary to Decimal Conversion (Unsigned Numbers)
Example: ( )2 = (….b2b1b0)10 ? = 1* * *22 + 0* *20 = (25)10 ( )2 = (….b2b1b0)10 ? = 1* * *25 + 1* * * *21 + 1*20 = (215)10

8 Decimal To Binary Conversion (Unsigned Numbers)
To convert from base 10 (decimal to binary) we need to divide the number in base 10 which is (N)10 by 2 several times until division by 2 is no longer possible. (dividend becomes 0) Example: (121) 10 = ( … b3b2b1b0)2 (121) 10 = ( )2 1) Given the number in base 10 (a3a2a1a0)10 2) Divide the number by 2 several times until dividend becomes 0 3) write the remainders from bottom to top 4) This is the representation of (N)10 in (N)2 Number Dividend Remainder 121 / 2 60 1 60 /2 30 30 /2 15 15 /2 7 7 /2 3 3 /2 1 / 2

9 Hex To Binary Conversion (Unsigned Numbers)
(N)16  (N)10  (N)2 Example: (ABC)16 = (…..b3b2b1b0)2 ??? (ABC)16 = 10* * *160 = = (2748)10 The result is: ( )2 Simpler solution is: (ABC)16 is ( )2 Number Dividend Remainder 2748/ 2 1374 1374 /2 687 687 /2 343 1 343 /2 171 171 /2 85 85 /2 42 42 /2 21 21 /2 10 10 /2 5 5 /2 2 2 /2 1 / 2

10 Exception 1: A Hex representation of any number can be converted to its binary representation directly by determining the 4 bit binary representation of each hex digit and concatenating the 4 bit binary word in which the hex digit appears For (ABC)16 = (….b3b2b1b0)2 (A)16 = (1010) 2 (B)16 = (1011) 2 (C) 16 = (1100) 2 Therefore, (ABC)16 = ( )2

11 Exception 2: The binary representation of any number can be converted to the hex representation by partitioning the binary number into groups of 4 digits starting from LSD (the rightmost digit) to left Example: ( )2 = (…..b3b2b1b0)16 (1B)16 ( )2 = (…..b3b2b1b0)16 1BDF1F = (1BDF1F5) 16

12 Reasons for Using Hex Numbers
Computer and digital systems operate on binary numbers. Binary numbers {0,1} constitute the building blocks of computer machine language Hex representation of a binary number is easier and more convenient for humans to communicate Converting between hex and binary representation of the number is easy

13 Bitwise Addition of Binary Numbers (Unsigned Numbers)
Example: Add binary number A = and B = Do the addition one column at a time A B Result 1 1

14 Bitwise Subtraction of Binary Numbers (Unsigned Numbers)
Example: Subtract the binary numbers B = from A = Do the subtraction one column at a time A B Result 10

15 Another example of subtraction:
Result = 11 11 1 10 10 10 10 10

16 Signed Numbers and Signed Arithmetic
Computers and digital systems only operate on {0,1} Since there is only two symbols in the binary number system, these digital systems do not know of the existence of negative and positive numbers. They only know magnitude or positive binary numbers In digital world, there are no symbol for ‘+’ or ‘-’ However, we require positive and negative binary numbers be processed by the computer. When using the signed number system, we will interpret the string of binary bits differently

17 Note that the sign bit does not contribute to the magnitude
Let us construct a system containing one half positive and other half negative numbers Let the MSB (most significant bit) denote the sign of the binary number where if the MSB = 0  positive number MSB = 1  negative number Note that the sign bit does not contribute to the magnitude MSB & Sign bit LSB

18 8-bit Representation of Signed Binary Number
. Range of positive numbers: 0 through 2(8-1) -1 = 127 In general, for n-bit binary words 0 through 2(n-1) -1 Range of the negative numbers: -1 through -2(8-1) = -128 In general, for n-bit binary words -1 through -2(n-1)

19 1 -1 2 -2 3 -3 . . Signed Binary/ Decimal Number Wheel [128 negative numbers] [128 positive numbers] . . . . -125 125 -126 126 -127 127 -128

20 2’s Complement Representation of Signed Numbers
We now have two methods for representing and interpreting strings of binary numbers When it is sufficient to consider only positive numbers, we use the unsigned interpretation as discussed earlier When we want to consider both positive and negative numbers, we use the signed number wheel The interpretation of the numbers in the signed number wheel is called the 2’s complement representation. This wheel shows us the binary equivalent of signed decimal numbers, or, the decimal equivalent of signed binary numbers.

21 How to Obtain the Signed Binary Number from a Signed Small Decimal Number ?
If the signed decimal number is positive, Convert the decimal number to binary using the unsigned conversion process as discussed earlier If the signed decimal number is negative, then Determine the binary number representation of the absolute value of signed decimal number Take the 1’s complement of the number (flipping every single bit: 0 to 1 and 1 to 0) Add 1 to it

22 The signed conversion is:
Converting a Positive Decimal Number to the 2’s Complement Representation: Example: Represent (+18)10 as a signed 8-bit binary number Using unsigned number conversion process Number Dividend Remainder 18 / 2 9 9 /2 4 1 4 /2 2 2 /2 1 / 2 The signed conversion is: (+18)10 = ( )2

23 Converting a Negative Decimal Number to the 2’s Complement Representation:
Represent (-18)10 as a signed 8-bit binary number Solution: Abs(-18)10 : 1’s Complement: Add 1: Result: Therefore, (-18)10 = ( ) 2 1

24 How to Obtain the Signed Decimal Number of a Signed Binary Number
If the signed binary number is positive (i.e. MSB = 0), convert the binary number to decimal using the unsigned conversion process as discussed earlier If the signed binary number is negative (i.e., MSB = 1) Take the 1’s complement Add 1 Convert to decimal using unsigned process Put a negative sign in front of the decimal number

25 Converting a Positive 2’s Complement Binary Number to Decimal Representation:
Example: Represent ( )2 as a signed decimal number Since MSB = 0 then we can use unsigned conversion ( )2 = 1*24 + 1*23 + 0*22 + 0*21 + 0*20 = (24)10

26 Converting a Negative 2’s Complement Number to Decimal Representation:
Example: Represent ( )2 as a signed decimal number Since MSB = 1 then the number is negative Use signed conversion process Solution: The number: 1’s Complement: Add 1: Result: Decimal -ve sign Therefore ( )2 = (-104)10

27 Signed or 2’s Complement Arithmetic
Addition of 8-bit signed number : If both numbers are positive, use unsigned addition process. (perform bitwise addition) Note: If the result of the addition exceed 8-bits, then ignore the carry over bit. This is because the sum or difference of any two properly represented 8-bits signed number never exceeds 8-bits Addition of 8-bit positive signed number Example: perform the addition of (8)10 + (10)10 in binary ( )2 = 1*24 + 0*23 + 0*22 + 1*21 + 0*20 = (18)10 1

28 Addition of 8-bit (positive and negative) signed number:
Perform the addition: (-17) Find the 2’s complement representation (8)10 = (-17)10 = ??? (17) 1’s complement: Add Result Therefore (-17)10 = ( )2

29 Now (8) (-17) Result ( )2 = (?) 10 Convert the result to decimal representation: take 1’s complement Add Result ( )2 = (+9)10 Therefore, ( )2 is -9 in decimal

30 Example: Perform 65 – 68 in 8 bits signed numbering system.
65 – 68 is the same as (65)10 + (-68)10. The example is the same as the previous one. Therefore, you can follow the same step as explained to solve this problem Example: Perform -26 – 35 in 8 bits signed numbering system. -26 – 35 is the same as (-26)10 + (-35)10

31 Logical Operation The logical operators And, Or, Xor, Not are used to perform logical operation on 2 bits. The next few slides shows the performance of logical operators on all possible combination of two bits. To perform, any logical operation on two 8-bits binary numbers: Write two binary numbers and perform bit-wise logical operation on each bit of the two binary numbers.

32 Logical OR: Logical And: A or B Number Binary 8-bit A 1110 1100
Result A B A or B 1 Logical And: A and B Number Binary 8-bit A B Result A B A and B 1

33 Logical XOR: Logical NOT: A xor B Number Binary 8-bit A 1110 1100
Result A B A xor B 1 Logical NOT: Not A Number Binary 8-bit A Result A Not A 1


Download ppt "Numbers and Arithmetic and Logical Operation"

Similar presentations


Ads by Google