Hi all, I have doubt. Can anybody explain me how to take 2's comlement of a -ve number. What I know is we have to invert all 0's to 1 and 1 to 0. and add 1. The result will represent the 2's complement of -ve number But I tried that way Example -5 5 => 00000101 by inverting 0 to 1 and 1 to 0's 11111010 adding 1 -5 => 11111011 This value is correct when I checked it out using toBinaryString() of Integer class But when I tried with -12 I did the same way as above 12 => 00001100 inverting we get 11110011 by adding we get -12=> 11110011 Whereas when i found out the value for -12 with toBinaryString(), the answer was 11110100 The same thing is happening for other numbers also. For some number 2' complement procedure is working for other number it is not. So please tell me how to solve this.

Arathi It looks like your problem is in the adding of 1 to the number. When you add a number in base 10 we all know you have to 'carry' anything over 10 to the next number, the same ting applies to binary numbers too (as well as hex, octal and all the others). With your example of -12: 12 is: 00000000 00000000 00000000 00001100 the twos compliment is 11111111 11111111 11111111 11110011 when you add one to the least significant bit which is a 1 the result is 2 but you cant ahve a two so you have to make it a 0 and carry over the extra to the next bit. So you carry a 1 to the next bit which is also a 1. Again, you'll end up with a 0 as the result and carrying a 1 to the next bit which is a 0. Adding the 1 the result is 1 so you no longer have to carry any extra and the answer is: 11111111 11111111 11111111 11110100 Notice also that I used all 4 bytes to do this, many times it's ok to get by with just using one byte but sometimes it'll give you the wrong answer. As an example figure this out using only 8 bits then using all 32: -5 >>> 2 hope this helps [ January 16, 2002: Message edited by: Dave Vick ]

Dave

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com