Numbers and Arithmetic and Logical Operation Representation of Numbers and Arithmetic and Logical Operation on them
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}
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: 1 0 0 0 1 1 0 0 1 0 MSD LSD
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*101 + 5*100 = 10 + 5 (1010)2 = 1*23 + 0*22 + 1*21 + 0*20 = 8+2=(10)10 (B7)16 = B*161 + 7*160 = 11*16 + 7 = (183)10
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
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
Binary to Decimal Conversion (Unsigned Numbers) Example: (0001 1001)2 = (….b2b1b0)10 ? 0001 1001 = 1*24 + 1* 23 + 0*22 + 0*21 + 1*20 = (25)10 (1101 0111 )2 = (….b2b1b0)10 ? 1101 0111 = 1*27 + 1* 26 + 0*25 + 1*24 + 0*23 + 1* 22 + 1*21 + 1*20 = (215)10
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 = (1111001)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
Hex To Binary Conversion (Unsigned Numbers) (N)16 (N)10 (N)2 Example: (ABC)16 = (…..b3b2b1b0)2 ??? (ABC)16 = 10*162 + 11*161 + 12*160 = 2560 + 176 + 12 = (2748)10 The result is: (101010111100)2 Simpler solution is: (ABC)16 is (1010 1011 1100)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
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 = (1010 1011 1100)2
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: (01 1011)2 = (…..b3b2b1b0)16 (1B)16 (1 1011 1101 1111 0001 1111 0101)2 = (…..b3b2b1b0)16 1BDF1F5 = (1BDF1F5) 16
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
Bitwise Addition of Binary Numbers (Unsigned Numbers) Example: Add binary number A = 11101100 and B = 01110011 Do the addition one column at a time A 11101100 B + 01110011 --------------- Result 101011111 1 1
Bitwise Subtraction of Binary Numbers (Unsigned Numbers) Example: Subtract the binary numbers B = 01101010 from A = 01110011 Do the subtraction one column at a time A 0 1 1 1 0 0 1 1 B - 0 1 1 0 1 0 1 0 ------------------- Result 0 0 0 0 1 0 0 1 10
Another example of subtraction: ------------------------------------- Result = 0 1 1 1 1 0 0 1 11 11 1 10 10 10 10 10
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
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 0 1 1 1 1 1 1 1 MSB & Sign bit LSB
8-bit Representation of Signed Binary Number 0000 0000 0 0000 0001 0000 0010 . 0111 1101 0111 1110 0111 1111 127 1000 0000 -128 1000 0001 1000 0010 1111 1101 1111 1110 1111 1111 -1 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)
0000 0000 0000 0001 1111 1111 1 -1 0000 0010 1111 1110 2 -2 3 0000 0011 1111 1101 -3 . . Signed Binary/ Decimal Number Wheel [128 negative numbers] [128 positive numbers] . . . . 1000 0011 -125 125 0111 1101 -126 1000 0010 126 0111 1110 -127 1000 0001 127 -128 0111 1111 1000 0000
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.
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
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 = (00010010 )2
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 : 0001 0010 1’s Complement: 1110 1101 Add 1: + 0000 0001 Result: 1110 1110 Therefore, (-18)10 = (1110 1110) 2 1
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
Converting a Positive 2’s Complement Binary Number to Decimal Representation: Example: Represent (0001 1000)2 as a signed decimal number Since MSB = 0 then we can use unsigned conversion (0001 1000)2 = 1*24 + 1*23 + 0*22 + 0*21 + 0*20 = (24)10
Converting a Negative 2’s Complement Number to Decimal Representation: Example: Represent (1001 1000)2 as a signed decimal number Since MSB = 1 then the number is negative Use signed conversion process Solution: The number: 1001 1000 1’s Complement: 0110 0111 Add 1: + 0000 0001 Result: 0110 1000 Decimal 104 -ve sign -104 Therefore (1001 1000)2 = (-104)10
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 0000 1000 + 0000 1010 0001 0010 (0001 0010)2 = 1*24 + 0*23 + 0*22 + 1*21 + 0*20 = (18)10 1
Addition of 8-bit (positive and negative) signed number: Perform the addition: 8 + (-17) Find the 2’s complement representation (8)10 = 0000 1000 (-17)10 = ??? (17)2 0001 0001 1’s complement: 1110 1110 Add 1 + 0000 0001 Result 1110 1111 Therefore (-17)10 = (1110 1111)2
Now (8)10 0000 1000 (-17)10 + 1110 1111 Result (1111 0111)2 = (?) 10 Convert the result to decimal representation: 1111 0111 take 1’s complement 0000 1000 Add 1 + 0000 0001 Result (0000 1001)2 = (+9)10 Therefore, (1111 0111)2 is -9 in decimal
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
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.
Logical OR: Logical And: A or B Number Binary 8-bit A 1110 1100 Result 1111 1111 A B A or B 1 Logical And: A and B Number Binary 8-bit A 1110 1100 B 0111 0011 Result 0110 0000 A B A and B 1
Logical XOR: Logical NOT: A xor B Number Binary 8-bit A 1110 1100 Result 1001 1111 A B A xor B 1 Logical NOT: Not A Number Binary 8-bit A 1110 1100 Result 0001 0011 A Not A 1