# Converting base 10 to base 2

Discussion in 'Mac Programming' started by MDMstudios, Apr 8, 2008.

1. ### MDMstudios macrumors member

Joined:
Mar 18, 2008
#1
Hello, I am making a program that needs to have two ints, one of which is a decimal number the other is the binary form of that number, I have tried everything I can think of, but none of my ideas work, so I was wondering if any of you know how to do this. Thanks!

2. ### GimmeSlack12 macrumors 603

Joined:
Apr 29, 2005
Location:
San Francisco
#2
3. ### kainjow Moderator emeritus

Joined:
Jun 15, 2000
#3
Here is a little function I wrote a while ago that does just that:

Code:
```int dec2bin(int num)
{
int bin = 0, k = 1;

while (num)
{
bin += (num % 2) * k;
k *= 10;
num /= 2;
}

return bin;
}```

4. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#4
kainjow's solution is interesting, but is only good for values of num less than 2^11 and greater than or equal to 0. Above 2^11 bin will overflow and unfortunate things will start happening. For negatives I'm not sure what the result will be, but I don't think it will be the 2s complement representation. The positive part is that you can print the result with %d and the number will "look" binary.

I'm not sure if this was precisely what you were after or not. It's certainly an interpretation of your original question. I had a different one (that you just want to display an int in its binary representation). Since integers are stored in binary anyway and you're using a binary computer, you can take advantage of that and just print based on each bit being set. This was what I came up with. it may not be the most optimal, but should work.

Code:
```#include <stdio.h>
void printbinary(int);
int main(int argc, char *argv[]) {
int number=20343;
printbinary(number);
}

void printbinary(int number) {
int x;
for(x = 31;x>=0;x--) {
if((number & (1 << x)) != 0) {
printf("1");
} else {
printf("0");
}
}
printf("\n");
}```
This could be adapted to store to a char[33] for later use pretty easily.

-Lee

P.S. I guess it says something about kainjow's mindset and my own, being that we just assumed you wanted C/C++/Obj-C, mine leaning towards C than C++ due to the printf rather than cout. If you are trying to do this in a language that isn't easily adapted to from C, let us know.

P.P.S. This assumes 32-bit integers. that might be a bad assumption. 8*sizeof(int) could be substituted as needed for the bit-width of an int.

5. ### MDMstudios thread starter macrumors member

Joined:
Mar 18, 2008
#5
Thanks yall, I finally got something working that can do triple digit numbers.

Code:
```#include <stdio.h>

int main (void) {

unsigned long int num;
long double tmpNum = .1;
int dec;
printf("Type in the decimal number you want to convert\n");
scanf("%i", &dec);

while ( dec != 0 )
{
if (dec % 2 == 1)
{
tmpNum *= 10;
num += tmpNum;
}

else
tmpNum *= 10;

dec = dec / 2;
}
printf("%i", num);

return 0;
}```

6. ### ChrisA macrumors G4

Joined:
Jan 5, 2006
Location:
Redondo Beach, California
#6
I think the simplest solution is this...

Lets say "d" is the "normal integer" and "b is is to be the binary one.
It is just one line: b+= 10^n*(bit number n of d) for
all vales of n.

7. ### jeremy.king macrumors 603

Joined:
Jul 23, 2002
Location:
Fuquay Varina, NC
#7
The Java way

Code:
```Integer.toBinaryString(number);
```
where number is an int

8. ### HiRez macrumors 603

Joined:
Jan 6, 2004
Location:
Western US
#8
Here is one I wrote a long time ago that prints up to 64-bit with some ASCII borders:
Code:
```void showBinary(long long input) {
int i = 0;
printf("+--------+--------+--------+--------+--------+--------+--------+--------+\n");
for (i = 0; i < 64; i++) {
if (i % 8 == 0) {
printf("|");
}
mask = 0x1 << (63 - i);
printf("1");
} else {
printf("0");
}
}
printf("|\n");
printf("+--------+--------+--------+--------+--------+--------+--------+--------+\n");
}```
EDIT: Oops, I missed the part about "decimal". When you say that, you do mean an integer value, right? Not a float type? Well, I'll leave this here anyway.

9. ### AlmostThere macrumors 6502a

#9
C++ way

The c++ way to do this is to use a bitset:

Code:
```#include <iostream>
#include <bitset>

using namespace std;

int main()
{
cout << bitset<16>(123) << endl;
cout << bitset<16>(string("101100101")).to_ulong() << endl;
}

```
NB. Bitset takes an unsigned integral template parameter - rather than 16 as here, which just seems clearer, you could use the appropriate number of digits from std::numeric_limits e.g. numeric_limits<unsigned long>::digits.

Output is:
Code:
```0000000001111011
357
```