Here is the method which I learnt during my electronic days at college to get 2's complement without writing one's complement.
Let me start with example which makes most of us to understand things more better.
Let us represent -23 in binary form
+23 can be represented as follows
00000000000000000000000000010111
<---
start from right to left , keep on writing the same digit till you find a digit which is 1 (in our case it is the first digit itself ) after that simply invert the digits.
i.e
00000000000000000000000000010111
^1 ( ^ = start inverting here )
so -23 would look like
11111111111111111111111111101001 = -23
^
Similary based on the above rule we can find -20 as follows
Start with +20
00000000000000000000000000010100 = 20
start from right to left
00000000000000000000000000010100
^100 ( ^ = start inverting here )
Write the same digits till you encounter the first 1 and then flip the remaining ones
so -20 would look like
11111111111111111111111111101100
Hope this helps everybody. Experts please give feedback.
Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.