Numbering Systems Contents: Binary & Decimal. Converting From: B D, D B. Arithmetic operation on Binary. Addition & Subtraction using Octal & Hexadecimal 2 s Complement, Subtraction Using 2 s Complement.
Objectives Understand the binary number system and decimal number system Be able to convert from binary to decimal Be able to convert from decimal to binary Be able to make binary addition Understand 2 s complement and Subtraction using 2 s complement
Binary & Decimal Systems
Bits & Bytes Bits are binary digits; they are either 0s or 1s. Bit (b) Binary digit, a 1 or 0 Computers operate with electronic switches that are either "on" or "off", corresponding to 1 or 0. A byte is a group of eight bits Byte (B) 8 bits
Decimal System Since we use only 10 digits in our daily calculations, our system of representing numbers is called Decimal System. So the numerical value Eight thousands nine hundred fifty four is represented as: 8 9 5 4 = 8*10 3 + 9*10 2 + 5*10 1 + 4*10 0
Decimal number system Base 10 because it uses ten symbols 0,1,2,3,4,5,6,7,8,9 2134 could also be written as: (2x10 3 ) + (1x10 2 ) + (3x10 1 ) + (4x10 0 )
Binary System Since a computer is a machine that can only work on two states: ON or OFF or two electrical pulse states High or Low, or two logical states True or False or simply 1 or 0 So the alphabet in computers is only formed of two entities (rather than 10 in the decimal system). That is why it is called Binary System.
Binary number system Base 2 because it uses 2 symbols 0 & 1 Computers recognize and process data using the binary (Base 2) numbering system The position, or place, of each digit represents the number 2 the base number raised to a power (exponent), based on its position (2 0, 2 1, 2 2, etc.) 10110 = (1 x 2 4 = 16) + (0 x 2 3 = 0) + (1 x 2 2 =4) + (1 x 2 1 = 2) + (0 x 2 0 = 0) = 22
Binary System Numerical Values in Binary System are represented as a combination of 1 s and 0 s, For example Decimal Binary 0 0 1 1 2 10 3 11 4 100.. 8 1000 9 1001 etc..
Binary System Notice the Following: Decimal Binary 3 = 3*10 0 11 = 1*2 1 + 1*2 0 10 = 1*10 1 + 0*10 0 1010 = 1*2 3 +0*2 2 +1*2 1 +0*2 0 So what is the decimal value of this binary number: 1 0 0 1 0 1?
Converting From Binary Decimal Decimal Binary
Converting from binary to decimal Multiply the binary digits by the base number (2), raised to the exponent of its position starting at 0. Binary no. 1 0 0 1 0 1 0 Position 6 5 4 3 2 1 0 2^posn. 2^6 2^5 2^4 2^3 2^2 2^1 2^0 Answer: 1*2^6+0*2^5+0*2^4+1*2^3+0*2^2+1*2^1+0*2^0 = 64 + 0 + 0 + 8 + 0 + 2 + 0 = 74
Binary Fractions The decimal value is calculated in the same way as for non-fractional numbers, the exponents are now negative.
Example: 101.001 (binary) =1* 2 2 + 0* 2 1 + 1* 2 0 + 0* 2-1 + 0* 2-2 + 1 x 2-3 = 4 +0 + 1 + 0 + 0 + 1 5 8 = = 5.125 (decimal) Note: 1 2 2-1 = = 0.5 1 4 2-2 = = 0.25 1 2-3 = 8 = 0.125 1 8 2-4 1 = = 0.0625 etc. 16
Try these Binary Decimal equivalent 0011? 01000010? 1111? 0101.01? 11.11? 01010.1101?
Converting from decimal to binary Divide the decimal number by two. Note the result & remainder. Repeat the process until the result is zero. The binary number equivalent is got reading all the remainders in the reverse order to which they were got.
Converting from decimal to binary E.g. decimal 46 46/2 = 23 rem 0 23/2 = 11 rem 1 11/2 = 5 rem 1 5/2 = 2 rem 1 2/2 = 1 rem 0 1/2 = 0 rem 1 Binary number is: 101110
converting decimal to binary fractions Consider left and right of the decimal point separately. The stuff to the left can be converted to binary as before. _
Example: Give 27.6875 (base 10) in base 2 27 (base 10) to binary (base 2): 27/2 = 13 rem 1 13/2 = 6 rem 1 6/2 = 3 rem 0 3/2 = 1 rem 1 1/2 = 0 rem 1 27 (base 10) = 11011 (base 2)
Cont. Example.6875 (base 10) to binary (base 2): Integer Fraction Coefficient 0.6875 *2 = 1 + 0.3750 1 0.3750 *2 = 0 + 0.7500 0 0.7500 *2 = 1 + 0.5000 1 0.5000 *2 = 1 + 0.0000 1 0.6875 (base 10) =.1011 (base 2) So, 27.6875 (base 10) = 11011.1011 (base 2)
Try these: Decimal Binary equivalent 34? 67.44? 234? 56.678? 25? 102.75?
Binary Addition& Subtraction
Binary addition The binary addition table: 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0 (carry 1) Overflow: If there is not enough room to hold the result correctly.
Binary subtraction The binary subtraction table: 0-0 = 0 1-0 = 1 1-1 = 0 0-1 = 1 (borrow 1)
Binary Addition Add Two binary numbers 0101 (5 base 10) and 0011 (3 base 10) 0 1 0 1 5 + 0 0 1 1 + 3 1 0 0 0 8
Binary Addition 1 1 1 1 1 1 0 1 1 1 + 0 1 1 1 0 0 2 3 2 2-2 2 2 2 1 0 1 0 0 1 1 Verification 55 10 + 28 10 83 10 64 32 16 8 4 2 1 1 0 1 0 0 1 1 = 64 + 16 + 2 +1 = 83 10
Binary Addition ex 1 0 0 1 1 1 + 0 1 0 1 1 0 + Verification 128 64 32 16 8 4 2 1 = =
Binary Addition & Subtraction Accomplished exactly the same as decimal Only two values per position (0 or 1) Examples: Addition Subtraction Carry 1 1 1 1 1 0 1 0 0 1 1 1 + 0 1 0 0 1 0 1 1 1 1 1 1 0 0 1 0 Borrow 1 1 1 1 0 1 0 0 1 1 1-0 1 0 0 1 0 1 1 0 1 0 1 1 1 0 0
Binary Addition & Subtraction Exercise Add the following two binary numbers 0 1 1 1 1 0 0 1 + 1 0 0 1 0 1 0 1 Subtract the following two numbers 1 1 0 0 0 1 0 1-0 0 1 0 1 0 0 1
Binary Multiplication& Division
Binary Multiplication Each bit from 2 nd number is multiplied by each bit in first number, then the result is added in order after shifting 1position each time. 0*0=0 0*1=0 1*0=0 1*1=1
Binary Multiplication Example 100110 (38) 1110 (14) 000000 100110 100110 + 100110 1000010100 (532)
Binary Division Using long division like decimal division 1 1.1 10 1 1 1 1 0 1 1 1 0 1 0 1 0 0 0
Octal Addition& Subtraction
Octal Addition Just as with decimal addition, octal uses carry when the sum of the values of a position exceeds 7 10 [or 7 8 ] Carry 1 1 1 1 2 3 4 + 5 6 7 2 02 3
Octal Addition 1 1 6 4 3 7 8 + 2 5 1 0 8 9 9-8 8 (subtract Base (8)) 1 1 1 4 7 8
Octal Addition ex 3 5 3 6 8 + 2 4 5 7 8 - (subtract Base (8))
Octal Subtraction Just as with decimal subtraction, Octal uses borrow when the difference between the values of a position requires it Borrow 1 256-137 117
Octal Subtraction 8 0 0 8 1 1 1 4 7 8 8 9-6 4 3 7 8 2 5 1 0 8
Octal Subtraction ex 3 5 3 6 8-2 4 5 7 8
Hexadecimal Addition& Subtraction
Hexadecimal Addition Just as with decimal addition, hexadecimal uses carry when the sum of the values of a position exceeds 15 10 [or F 16 ] Carry 1 1 1 5 6 7 8 1 2 3 4 + 8 A 1 1 D E F 0 E 0 8 9 F 1 2 4 H Note that FFFFH may be treated as -1 depending on whether we are dealing with signed or unsigned values
Hexadecimal Addition 1 1 7 C 3 9 16 + 3 7 F 2 16 20 18 11-16 16 (subtract Base (16)) B 4 2 B 16
Hexadecimal Addition 8 A D 4 16 + 5 D 6 16 - (subtract Base (16)) 16
Hexadecimal Subtraction Just as with decimal subtraction, hexadecimal uses borrow when the difference between the values of a position requires it Borrow 1 1 1 8 A 1 1 D E F 0-5 6 7 8 1 2 3 4 3 3 9 9 C C B 4 H
Hexadecimal Subtraction B 16 7 C 3 9 16 19-3 7 F 2 16 4 4 4 7 16
Hexadecimal Subtraction 8 A D 4 16-5 D 6 16 16
2 s Complement & Subtraction using 2 s Complement
How To Represent Signed Numbers Plus and minus sign used for decimal numbers: 25 (or +25), -16, etc. For computers, desirable to represent everything as bits. Three types of signed binary number representations: signed magnitude, 1 s complement, 2 s complement. In each case: left-most bit indicates sign: positive (0) or negative (1). Consider signed magnitude: 00001100 2 = 12 10 10001100 2 = -12 10 Sign bit Magnitude Sign bit Magnitude
One s Complement Representation The one s complement of a binary number involves inverting all bits. 1 s comp of 00110011 is 11001100 1 s comp of 10101010 is 01010101 00001100 2 = 12 10 11110011 2 = -12 10 Sign bit Magnitude Sign bit Magnitude
Two s Complement Representation The two s complement of a binary number involves inverting all bits and adding 1. 2 s comp of 00110011 is 11001101 2 s comp of 10101010 is 01010110 To find negative of 2 s complement number take the 2 s complement. 00001100 2 = 12 10 11110100 2 = -12 10 Sign bit Magnitude Sign bit Magnitude
Two s Complement Shortcuts Algorithm Simply complement each bit and then add 1 to the result. Finding the 2 s complement of (01100101) 2 and of its 2 s complement N = 01100101 [N] =10011011 10011010 01100100 + 1 + 1 ------------- ------------- 10011011 01100101
1 s Complement Addition Using 1 s complement numbers, adding numbers is easy. For example, suppose we wish to add +(1100) 2 and +(0001) 2. Let s compute (12) 10 + (1) 10. (12) 10 = +(1100) 2 = 01100 2 in 1 s comp. (1) 10 = +(0001) 2 = 00001 2 in 1 s comp. Step 1: Add binary numbers Step 2: Add carry to low-order bit 0 1 1 0 0 Add + 0 0 0 0 1 -------------- 0 0 1 1 0 1 Add carry 0 -------------- Final Result 0 1 1 0 1
1 s Complement Subtraction Using 1 s complement numbers, subtracting numbers is also easy. For example, suppose we wish to subtract +(0001) 2 from +(1100) 2. Let s compute (12) 10 - (1) 10. (12) 10 = +(1100) 2 = 01100 2 in 1 s comp. (-1) 10 = -(0001) 2 = 11110 2 in 1 s comp. 1 s comp Step 1: Take 1 s complement of 2 nd operand Step 2: Add binary numbers Step 3: Add carry to low order bit Add 0 1 1 0 0-0 0 0 0 1 -------------- 0 1 1 0 0 + 1 1 1 1 0 -------------- 1 0 1 0 1 0 Add carry 1 -------------- Final Result 0 1 0 1 1
2 s Complement Addition Using 2 s complement numbers, adding numbers is easy. For example, suppose we wish to add +(1100) 2 and +(0001) 2. Let s compute (12) 10 + (1) 10. (12) 10 = +(1100) 2 = 01100 2 in 2 s comp. (1) 10 = +(0001) 2 = 00001 2 in 2 s comp. Step 1: Add binary numbers Step 2: Ignore carry bit Add Final Result 0 1 1 0 0 + 0 0 0 0 1 -------------- 0 0 1 1 0 1 Ignore
2 s Complement Subtraction Using 2 s complement numbers, follow steps for subtraction For example, suppose we wish to subtract +(0001) 2 from +(1100) 2. Let s compute (12) 10 - (1) 10. (12) 10 = +(1100) 2 = 01100 2 in 2 s comp. (-1) 10 = -(0001) 2 = 11111 2 in 2 s comp. 2 s comp Step 1: Take 2 s complement of 2 nd operand Step 2: Add binary numbers Step 3: Ignore carry bit Add Final Result 0 1 1 0 0-0 0 0 0 1 -------------- 0 1 1 0 0 + 1 1 1 1 1 -------------- 1 0 1 0 1 1 Ignore Carry
2 s Complement Subtraction: Example #2 Let s compute (13) 10 (5) 10. (13) 10 = +(1101) 2 = (01101) 2 (-5) 10 = -(0101) 2 = (11011) 2 Adding these two 5-bit codes carry Discarding the carry bit, the sign bit is seen to be zero, indicating a correct result. Indeed, (01000) 2 = +(1000) 2 = +(8) 10. 0 1 1 0 1 + 1 1 0 1 1 -------------- 1 0 1 0 0 0
2 s Complement Subtraction: Example #3 Let s compute (5) 10 (12) 10. (-12) 10 = -(1100) 2 = (10100) 2 (5) 10 = +(0101) 2 = (00101) 2 Adding these two 5-bit codes 0 0 1 0 1 + 1 0 1 0 0 -------------- 1 1 0 0 1 Here, there is no carry bit and the sign bit is 1. This indicates a negative result, which is what we expect. (11001) 2 = -(7) 10.
Thank You