Converting base 10 to base 2

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

  1. macrumors member

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


  3. Moderator emeritus


    Here is a little function I wrote a while ago that does just that:

    int dec2bin(int num)
        int bin = 0, k = 1;
        while (num)
            bin += (num % 2) * k;
            k *= 10;
            num /= 2;
        return bin;
  4. macrumors 68040


    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.

    #include <stdio.h>
    void printbinary(int);
    int main(int argc, char *argv[]) {
      int number=20343;
    void printbinary(int number) {
      int x;
      for(x = 31;x>=0;x--) {
        if((number & (1 << x)) != 0) {
        } else {
    This could be adapted to store to a char[33] for later use pretty easily.


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

    Thanks yall, I finally got something working that can do triple digit numbers.

    #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;
    				tmpNum *= 10;
    		dec = dec / 2;
    	printf("%i", num);
        return 0;
  6. macrumors G4

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


    The Java way

    where number is an int
  8. macrumors 601


    Here is one I wrote a long time ago that prints up to 64-bit with some ASCII borders:
    void showBinary(long long input) {
    	int i = 0;
    	unsigned mask = 0;
    	for (i = 0; i < 64; i++) {
    		if (i % 8 == 0) {
    		mask = 0x1 << (63 - i);
    		if (input & mask) {
    		} else {
    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. macrumors 6502a

    C++ way

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

    #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:

Share This Page