File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Binary numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Binary numbers" Watch "Binary numbers" New topic
Author

Binary numbers

Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
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<br />Sun Certified Java Programmer
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Arathi
Here is part of your post
I did the same way as above
12 => 00001100
inverting we get 11110011
by adding we get
-12=> 11110011

you have inverted 12 but not added 1 to it.
Inverting 12 , we get 11110011 ,
when you add 1 to it
you get 11110100
which is -12.
Hope this helps
Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
Originally posted by Shivaji Marathe:
Arathi
Here is part of your post

you have inverted 12 but not added 1 to it.
Inverting 12 , we get 11110011 ,
when you add 1 to it
you get 11110100
which is -12.
Hope this helps

Thanks shivaji. I got it. Will you tell me one more thing when you are adding one which kind of binary addition you are doing
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Binary numbers
 
Similar Threads
Where am I wrong??
~ operator
Doubt with bit shifting,Please help
Using ~ (Unary bitwise complement) for Zero
Casting int value to Byte