Vous êtes sur la page 1sur 7

Finding the r's and (r-1)'s complement

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.

Finding (r-1)'s complement:


We can do this easily by subtracting all the digits of that number from (r-1) where r is the base of that number. EXAMPLES: Find the 10's and 9's complement of (348)10. ans: 9 9 10 -3 4 8 6 5 2 here 652 is 10's comp of 348

9's comp

9 9 9 - 3 4 8 6 5 1 here 651 is 9's comp of 348

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.

Read about alphanumeric code from book Eg ..ASCII,EBCDICetc..


Excess-3 code is an example of weighted code. Excess-3 equivalent of a decimal number is obtained by adding 3 and then converting it to a binary format. For instance to find excess-3 representation of decimal number 4, first 3 is added to 4 to get 7 and then binary equivalent of 7 i.e. 0111 forms the excess-3 equivalent. Below is table representing excess-3 equivalent of decimal numbers (0-9): Decimal Number 0 1 2 3 4 5 6 7 8 9 Excess-3 Equivalent 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100

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.

5------------------------- 1000 | | 4 -----x-3 code--- 9s com | | 0111 1s compl

Integer Representation and Binary Arithmetic


Integer Representation

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

Arithmetic can be cumbersome.

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

Conversion of Byte Integer to Word


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

converts the signed byte in AL to a word in AX

Conversion of Word Integer to Byte


Remove upper byte of word. Retain only the lower byte. Meaningful only if original number can be represented by a byte.

Integer Arithmetic (1's Comp and 2's Comp)


Addition: Simply add the two binary representations. Subtraction: Find negative of one number, add to the second.

Addition in 1's Comp


Add binary representations of the two numbers. If there is a carry, add it back in on the right side.

51 00110011 + (-37) + 11011010 ---------1 00001101 + 1 ---------14 00001110

Addition in 2's Comp


Add binary representations of the two numbers. Disregard the carry.


51 00110011 + (-37) + 11011011 ---------14 1 00001110

Subtraction in 2's Comp

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.

Binary code & BCD code

Vous aimerez peut-être aussi