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

posted 3 years ago

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.

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

Posts: 4568

9

posted 3 years ago

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.

- 1

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.

posted 3 years ago

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...

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?**

posted 3 years ago

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

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

posted 3 years ago

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

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