Académique Documents
Professionnel Documents
Culture Documents
Here we are going to learn how to convert a number to its r's and (r-1)'s complement. Method: Let 'N' is a number and r is its base where r>1 and in N, 'n' is the number of digits before its decimal point then we can write r's complement of number = r^n-N EX. N = (23)10 here r = 10 n = 2 and N = 23 hence we can write the 10's complement of this number as 10^2 - 23 = 77. hence we can say that 10's comp of 23 is 77. Although this method is good enough to solve any problem regarding to this concept, but we will follow different method for finding r's and r-1's complement. Easy Method: Let we have to find again the 10's comp of 23 then this method tells us to divide 3 from 10 and 2 from 9 (i.e 10-9). which gives us a result of 77. 9 10 - 2 3 7 7 i.e the generalized form of writing a r's comp of a number 'abc' which is in r base, we can write. (r-1) (r-1) r a b c
this difference gives us the r's comp of that number. i.e we can find r's complement of a number by subtracting its right most digit by r and all digits by r-1.
9's comp
from this method you can find the r's and (r-1)'s complement of any number with base r.
1s complement of a binary number N is obtained by the formula : -(2^n 1) N Where n is the no of bits in number N
Example
Convert binary number 111001101 to 1s complement. Method: N = 111001101 n=9 2^n = 256 = 100000000 2^n -1 = 255 = 11111111 1s complement of N = (100000000 1) -111001101 011111111 111001101 = 000110010 Answer: 1s complement of N is 000110010
Trick:
Invert all the bits of the binary number N = 111001101 1s complement of N is 000110010
2s complement:2s complement of a binary number N is obtained by the formula (2^n) N Where n is the no of bits in number N Example: Convert binary number 111001101 to 2s complement Method 2s complement of a binary no can be obtained by two step process Step 1 1s complement of number N = 000110010 Step 2 1s complement + 1 000110010 + 000000001 = 000110011 Answer 2s complement of a binary no 111001101 is 000110011
Trick : 2s complement can be represented by keeping all lower significant bits till first 1 as it is and taking complement of all upper bits after that. error detection and correction code - (EDAC, or "error checking and correction", ECC)
A collection of methods to detect errors in transmitted or stored data and to correct them. This is done in many ways, all of them involving some form of coding. The simplest form of error detection is a single added parity bit or a cyclic redundancy check. Multiple parity bits can not only detect that an error has occurred, but also which bits have been inverted, and should therefore be re-inverted to restore the original data. The more extra bits are added, the greater the chance that multiple errors will be detectable and correctable. Several codes can perform Single Error Correction, Double Error Detection (SECDEC). One of the most commonly used is the Hamming code.
Excess-3 code is also known as self complimenting code or reflective code, as 1s compliment of any number (0-9) is available within these 10 numbers. For example 1s complement of 9 (1100) is 0011.
Bit-- Binary Digit o 1 byte = 8 bits o 1 word = N bytes, take N to be 2 (e.g., 16 bit machine) Integer takes up 2 bytes; can be signed or unsigned.
Unsigned Integers
Can represent whole numbers from 0 to 65,535 16 o (0 to 2 - 1). In binary, this is from 2 o 0 to 11111111111111112 Internally, binary representation of decimal value as 16 bits.
Signed Integers
Need to reserve one bit for the sign. Three ways: o Sign-Magnitude o 1's Complement o 2's Complement
Sign-Magnitude
Uses most significant bit of the word to represent the sign. o 0 - Positive o 1 - Negative. Rest of the number is encoded in magnitude part
37 = -37 = 6712 -6712 00000000 00100101 10000000 00100101 = 00011010 00111000 = 10011010 00111000
Can represent numbers from -32,767 to 32,767. 15 15 o -2 +1 .. 2 -1 But, two representations for zero:
0 = 00000000 00000000 -0 = 10000000 00000000
1's Complement
Negative number is stored as bit-wise complement of corresponding positive number. Leftmost bit of positive number is 0. That of negative number is 1.
196 = 00000000 11000100 -196 = 11111111 00111011
Can represent numbers from -32,767 to 32,767. 15 15 o -2 +1 .. 2 -1 Arithmetic is easier than sign-magnitude. But, still have two representations for zero:
0 = 00000000 00000000 -0 = 11111111 11111111
2's Complement
Modern Method
Positive number represented in same way as other two methods Negative number obtained by taking 1's Complement of positive number and adding 1.
6713 = 00011000 00011101 1's Comp = 11100111 11100010 2's Comp = 11100111 11100011
Word integer can represent numbers from -32,768 to 32,767. 15 15 o -2 .. 2 -1 Byte integer can represent numbers from -128 to 127. 7 7 o -2 .. 2 -1 One version of zero:
00000000 00000000
Sign Extension Copy sign bit of the byte into all the bits of the upper byte of the word.
37 = 00100101 -> 00000000 00100101 -37 = 11011011 -> 11111111 11011011
cbw
o
Remove upper byte of word. Retain only the lower byte. Meaningful only if original number can be represented by a byte.
Addition: Simply add the two binary representations. Subtraction: Find negative of one number, add to the second.
Add binary representations of the two numbers. If there is a carry, add it back in on the right side.
Overflow
If two numbers have different signs, their sum will never overflow. If they have the same sign, they might overflow. Overflow has occurred if sign of result is different than sign of addends.