Consider that we have 8 bits to represent the number. The binary representation of $42$ is $00101010$.
- In sign-magnitude form, the MSB is used to represent the sign bit and remaining bits represent the magnitude. So $+42$ is represented as:
and $-42$ is represented as:
- In 1’s complement form:
- If the number is +ve, convert it to binary and make its length to the given bit size. +42 is represented as $\textbf{00101010}$.
- If the number is -ve, the representation is simply the 1’s complement of that number. So the 1’s complement representation of $00101010$ is :
- In 2’s complement form:
- If the number is +ve, convert it to binary and make its length to the given bit size. +42 is represented as $\textbf{00101010}$.
- If the number is -ve, the representation is simply the 2’s complement of that number. So the 2’s complement representation of $00101010$ is :
(1’s complement of 00101010) + 1= (11010101 + 1) =
Note: If the number is +ve: sign-magnitude, 1’s complement and 2’s complement representation are all same.
To perform the subtraction $42 – 20$ in 2’s complement , we simply add 42 with 2’s complement of 20.
$42 – 20 = 00101010 + 2’s(00010100) = 00101010 + (11101100) = 1 00010110$.
- If the resultant has a carry, the answer is +ve and we discard the carry.
- If the resultant has no carry, the answer is -ve and to get the final value, we simply find the 2’s complement of the answer.
In this case, we have a carry, so just discard it. Value becomes $+(00010110)_2 = \textbf{+22}$