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

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
Hi all, I am trying to convert decimal to binary in c

the max decimal number is 10000 so any integer from 0 to 10000 can be entered

10000 in binary is 14 digits long

my function is as follows

int fun(int num)

{
int remain;
int x=0;
int sum=0;
int binary=0

while(x<13)
{
remain=10*(num%2); //so it will be in factors of 10
num=num/2; // keeps diving number by 2

if(num!=0)
{
sum=pow(remain,x)+sum;
}
x=x+1

}

binary=sum;
return binary;

}

however this only prints of sum =1 no matter what number is taken in


any ideas? I am trying to get it so if the remainder is 1 I multiply by 10 and raise it to the power of x, then add it to the sum

thanks for any insight. you guys are such a great group!
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
The line "return binary;" is being called on the very first time the loop goes through, meaning, the loop only goes through once and then the function ends and returns.

Second of all, usually binary numbers are represented as strings when converting from a decimal to a binary. You're storing it in an integer, but you probably want to store it in a string (char array).

Third, x never changes, so if you remove the return line, it will be an infinite loop.
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
kainjow said:
The line "return binary;" is being called on the very first time the loop goes through, meaning, the loop only goes through once and then the function ends and returns.

Second of all, usually binary numbers are represented as strings when converting from a decimal to a binary. You're storing it in an integer, but you probably want to store it in a string (char array).

Third, x never changes, so if you remove the return line, it will be an infinite loop.


whoops i mistyped my code when typing it in here. should be correct now (as in the way i have it typed currently)


once again thanks for any help you can give
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
Why would you want the result as an integer? What are you going to do with a binary representation that requires it to be an integer? Maybe you have a good reason, but I agree with kainjow, it seems odd.

Anyway, it could be done more efficiently using bit shifting and masking. Here is a method I wrote to do this with an int:
Code:
void printBinary(int input, int maxDigits, int perRun) {
    int i = 0;
    unsigned mask = 0;
    
    for (i = 0; i < maxDigits; i++) {
        /* insert a space separator every perRun digits */
        if (perRun > 0 && (i - maxDigits) % perRun == 0) {
            printf(" ");
        }
        mask = 0x1 << (maxDigits - i);
        if (input & mask) {
            printf("1");
        } else {
            printf("0");
        }
    }
    printf("\n");
}
(That has some embellishments that could be taken out to make it smaller.)

ps. You are still missing some semicolons in your code at statement endings.
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
well its for a class , we can only use if,elses, whiles, and fors. the number has to be stored as an int

i have been trying to figure this out for seriously 8 hrs now and yea not fun anymore lol

any suggestions? was i even on the right track. i am new to programming so this is all new to me

once again thanks
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Storing it in an int is definitely a tiny bit more challenging.

Here's a little app that does it:
Code:
#include <stdio.h>

int dec2bin(int num)
{
	int bin = 0, k = 1;
	
	while (num)
	{
		bin += (num % 2) * k;
		k *= 10;
		num /= 2;
	}
	
	return bin;
}

int main()
{
	int num = 0;
	
	printf("Enter a number: ");
	scanf("%d", &num);
	
	printf("%d in binary is %d\n", num, dec2bin(num));
	
	return 0;
}
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
thanks, i am able to follow that except for the part involving k.is k just putting the remainder in the correct decimal place?

just so i understand this while(num) evaluates the loop while a number is present (non zero) correct?

once again thanks
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
dukebound85 said:
thanks, i am able to follow that except for the part involving k.is k just putting the remainder in the correct decimal place?
Well, num % 2 is the 1 or 0 of the binary number. It starts with the least significant bit, and ends with the most significant bit. So if you were to print it out at each iteration of the loop, it would be backwards (e.g. 12 would output 0011). But we want to store it as a number, not a string. So k represents the decimal place, starting with 1, then 10, then 100, etc, multiplied by the current bit (1 or 0), and then added to the current number.

dukebound85 said:
just so i understand this while(num) evaluates the loop while a number is present (non zero) correct?
Yes, the loop continues while num evaluates to true, which is when it's non-zero.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.