aspose file tools*
The moose likes Associate Certification (OCAJP 7/8) and the fly likes Base 10 (Decimal) to Base 2 (Binary) Adventures Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7/8)
Bookmark "Base 10 (Decimal) to Base 2 (Binary) Adventures" Watch "Base 10 (Decimal) to Base 2 (Binary) Adventures" New topic
Author

Base 10 (Decimal) to Base 2 (Binary) Adventures

Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1




Hello Java Ranch,

I am trying to convert 32,768 to binary... so I can then flip the bits and add 1 (two's complement or whatever) but keep getting different results or results that do not match up to what I find in online conversion calculators.

For instance if I repeatedly divide 32,768 by two and use the remainders to generate the binary number i obtain: 0000000000010001
If I list all the powers of 2 until I find one as large or larger than 32,768 I obtain:1000000000000000
This second answer seems to match up with online conversion calculators but my problem is the logic in dividing the number down by 2 is accurate but the resulting binary number is wrong and I am not sure why.

Can anyone please enlighten me on this?

http://www.wikihow.com/Convert-from-Decimal-to-Binary - im using this tutorial.

I would not be doing this if java wrote something in the API to automatically generate string representations of short values like is possible with int values.





Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

Ted North wrote:
For instance if I repeatedly divide 32,768 by two and use the remainders to generate the binary number i obtain: 0000000000010001

Really? How did that work, then?

Remember, if you're using that approach you need to reverse the number (the first digit you identify is the one on the right, and so on). But that doesn't explain why you've got a second 1. 32768 = 2^15 - it's a pure power of 2.
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Matthew Brown wrote:
Ted North wrote:
For instance if I repeatedly divide 32,768 by two and use the remainders to generate the binary number i obtain: 0000000000010001

Really? How did that work, then?

Remember, if you're using that approach you need to reverse the number (the first digit you identify is the one on the right, and so on). But that doesn't explain why you've got a second 1. 32768 = 2^15 - it's a pure power of 2.


Hi Matthew,

Your response solved my problem. I forgot that the remainders calculated through long division are in reverse order. So the last remainder calculated is actually going to be first bit on the far left. doh! I also looked at my long division and saw that I made a small error and that is why I have an extra 1...

A thousand thank-yous for helping out.

Also...

Why does this number have a negative sign bit if it is positive? 1 is negative and 0 is positive with binary and I guess everywhere else...
When I flip the bits and add one I obtain the value of: 0100000000000000 is this correct? this would be -32, 768 in base 10 the lowest MIN value of a Short variable?


Danjel Nyberg
Greenhorn

Joined: Sep 12, 2012
Posts: 15

Hi

You can also convert decimals to binary with:
Integer.toBinaryString(a);

Where a is of type int

and convert binary string to decimals with:
int a = Integer.parseInt(s1, 2);
where s1 is of type string

also adding and removing specific bits from a binary is possible with follwing code:



Kind Regards
/Danjel
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Danjel Nyberg wrote:Hi

You can also convert decimals to binary with:
Integer.toBinaryString(a);

Where a is of type int

and convert binary string to decimals with:
int a = Integer.parseInt(s1, 2);
where s1 is of type string

also adding and removing specific bits from a binary is possible with follwing code:



Kind Regards
/Danjel


Thanks Danjel. I wish every wrapper class had the 'toBinaryString()' method. Also, I have never seen a |= compound assignment operator before. Wow. Thanks for explaining how to convert a binary string to a decimal! This is helpful.

Have an awesome day.

Regards,

Ted North
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Base 10 (Decimal) to Base 2 (Binary) Adventures