I need help with a program I am writing

Lol - I spent all of that time downstairs with sheets of paper trying to work it out! I went thorugh about 10 sheets;

First I tried to work out Decimal to Binary, and then a way to do it

for 5 to Binary (101)

5 / 2 2 rem 1
2 / 2 1 rem 0
1 / 2 0 rem 1

Backwards (this matters on other numbers) this is 101. And that works for all Binary Numbers. The Problem was that this is niether an equation nor anything like any kind of help programming it. I went through Logarithims - but I cannot remmember a thing about them (I'm only 16!) but i'm sure that would be a very good way to do it.

I'm sure thats the way you were trying to do it - but it doesn't help the 'how many times to divide?' question. Logarithims with a quick and easy Logging of the remander would sort this out - but I have no idea how to do it. This may help you!

However; I eventually gave up for today, and I will talk to my Maths teacher whom I am sure will know about Logarithims and then not only I will learn something about them but I can tell you if you haven't already worked it out by then. Decimal to Binary is not the easiest thing to do! even when you're just trying to find an equation let alone put it in code!

I felt bad about syaing that I would try my best to help you - and then not help you - so I went and found information on writing a Unicode to Binary Program:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdecimalclasschartodecimalconversiontopic.asp

This is single bit stuff - but it also has up to 64-bit stuff if you want it! It looks easy enough and I hope it can help you.

Hopefully I will be able to help you when I talk to my teacher about it - you don't get a degree in Maths without a great deal of knowledge about Logarithims! I will see what I can learn!

I hope that helped at least a bit - I'm sorry I couldn't find an equation! :confused: :confused: :confused: :confused:
 
David - I do have books on it, but its the process of practicing it for myself that is hard.

Lord Kalthorn - WOW!! You have some great info for me to look out. I can't tell you how much this is helping me. I will be researching Logarithims myself to see if I can figure some things out as well. Keep me posted on what you find out. Thanks again, this has been more then enough help!
 
Lol, its Ok - I'm starting to get a little interested in it too! I'm actually using huge chunks of my Half-Term researching the idea!
 
Here why don't you give this a try:
Code:
#include <stdio.h>
/*------------------------------------------------------------*/
/* Decimal to binary conversion */

void Decimal_2_Binary(unsigned int x, unsigned int c[32])
{
	int bit;		

	for(bit = 0; bit < 32; bit++)
	{
		c[bit] = ((x >> bit) & 1);
	}
}


/*------------------------------------------------------------*/

int main()
{
    int bit;
    unsigned int x;
    unsigned int c[32];		

	
    printf("\nEnter an integer number smaller than 4,294,967,295: ");
    scanf("%u", &x);
    printf("\nConverting decimal to binary:\n");

    Decimal_2_Binary(x, c);

    printf("	Binary == ");

    for(bit = 31; bit >= 0; bit--)
    {
	if(bit == 27 || bit == 23 || bit == 19 || bit == 15 || bit == 11 || bit == 7 || bit == 3)
	{
		printf(" ");
	}


	printf("%u", c[bit]);
    }

	printf("\n\n\n");
	return 0;
}
 
Very interesting! Thanks - I hope he gets it; he will be very happy!

The question is; how do you get 64-bit Binary! Ha! Ha! Ha! Mooooo!! I am a Cow
 
Yes that is very interesting, Traveller. I am copying it now and seeing how it works. I knew that I had to use somekind of loop for the program but I wasn't sure what or how. Thank you very much Traveller for posting this. Anyway so I am going to see how this works and check back with you guys.

P.S. - Lord Kalthorn - That is the next question, how to do the 64 bit binary program. LOL, my question never ends, it just gets bigger, :)
 
that is my way.maybe,it will help u.
#include<stdio.h>
#define MAX 30
#define N 2
main()
{
int a[MAX];
int i,m,n,b;
clrscr();
puts("please enter a number\n");
scanf("%d",&m);
i=0;
do{
b=m%N;
a=b;
m=(m-b)/N;
i++;
}while(m>0);
for(n=i-1;n>=0;n--)
printf("%d",a[n]);
printf("\n");
getch();
}

 
Thanks alone, unfortunatly I have been busy lately and haven't had a chance to check out Traveller's code or your code. Hopefully I will have some time tonight to check them out. Thanks to everyone for the input I have been recieving.
 
ObiWan506 said:
Thanks alone, unfortunatly I have been busy lately and haven't had a chance to check out Traveller's code or your code. Hopefully I will have some time tonight to check them out. Thanks to everyone for the input I have been recieving.
Ok, let us know how you get on!
 
Back
Top Bottom