Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MDMstudios

macrumors member
Original poster
Mar 18, 2008
36
0
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!
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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;
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.
 

MDMstudios

macrumors member
Original poster
Mar 18, 2008
36
0
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;
}
 

ChrisA

macrumors G5
Jan 5, 2006
12,917
2,169
Redondo Beach, California
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!

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.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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;
	unsigned mask = 0;
	printf("+--------+--------+--------+--------+--------+--------+--------+--------+\n");
	for (i = 0; i < 64; i++) {
		if (i % 8 == 0) {
			printf("|");
		}
		mask = 0x1 << (63 - i);
		if (input & mask) {
			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.
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.