# Thread: decimal to binary conversion

1. Originally Posted by roelof
Oke,

I understand that,
But how can I know if a number is a 32 bit binary or a 64 bit binary.
In my opinion we can know that after the conversion.

Roelof
You know that by knowing about your compiler... in my compiler int is 32 bits... I know this and I can trust it.
If your compiler's library includes stdint.h you can be very explicit about variable sizes... for example int32_t guarantees a 32 bit integer.

If you are concerned about the user's entry overflowing the integer's range... you need to limit your user's input to acceptable values (almost always a necessity in real world programming, anyway)... There are a great many ways of doing this but in a training exercise it's safe to make a note to work on this later (once your more familiar with the language)

2. Oke,

So I can't know if I have to have to array of 32 of 64 bit.

Roelof

3. Originally Posted by roelof
Oke,

So I can't know if I have to have to array of 32 of 64 bit.

Roelof
Again... you know this because you are telling the compiler what to do... there's no way to back-track and discover this later. The C compiler won't track this for you and it won't produce errors if you mess up. It is up to you as a programmer to keep track of the variables and their sizes.

Here's a little example of limiting input to acceptable values, based on my previous example... (Note this is untested but it should be pretty close...)

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

int main (void)
{ int64_t number;  // 64 bit input space
int32_t  filter = 0x40000000u;  // 32bits, msb = 1;

while (number > -1)
{ printf("Enter a number from 0 to %d  (-1 to exit) :", INT32_MAX);

// read in a 64 bit value
scanf("%lld", number);

// get rid of extraneous newline character
getchar();

// range check
if ((number > -1) && (number <= INT32_MAX)
{ printf("\n\n");
break; } }

// exit on any negative number
if (number < 0)
exit(0);

printf("Your number as binary is :   ");

// burst to binary
while (filter)
{ if (number & filter)
printf("1");
else
printf("0");
filter = filter >> 1; }

printf("\n\n");

return 0; }```

4. Oke,

I hope my book (C book) will explain that later.
It's not something chapter 1 has explained.
They only talked about char and int.

But thanks for the explanation.

Roelof

5. Originally Posted by roelof
Oke,

So I can't know if I have to have to array of 32 of 64 bit.

Roelof
The C standard requires your compiler to tell you which it is in the manual/other documentation that came with the compiler. So assuming you know where you put the manual/other documentation, there will be a section about "implementation limits" or some such that says whether "int" is 32 or 64 bits.

6. Oke,

And if I understand it right
It can be that on my system int us 64 bit and on another system int = 32 bit.
So as Commentator have said it better that I say if a int is 32 or 64 bit.
Otherwise wierd things can happen.

Roelof

7. Originally Posted by roelof
Oke,

I hope my book (C book) will explain that later.
It's not something chapter 1 has explained.
They only talked about char and int.

But thanks for the explanation.

Roelof
No worries... In your place I'd make a note to check into it a bit later in the learning process... When I was learning C++ (a recent undertaking) I made lots of notes about stuff I have to review and investigate later. It's all part of wanting to learn....

8. Originally Posted by roelof
Oke,

And if I understand it right
It can be that on my system int us 64 bit and on another system int = 32 bit.
So as Commentator have said it better that I say if a int is 32 or 64 bit.
Otherwise wierd things can happen.

Roelof
It doesn't always matter... what do you care when simply counting to 10? But when doing binary manipulations or dealing with really big numbers, yes, it is better.

FWIW... there are some cases where an int = 16 bits, too.

Although I doubt your textbook has covered it yet, you can always do this:
Code:
`int x = sizeof(int):`
The value of x is in characters (bytes) so...

2 = 16 bit integers
4 = 32 bit integers
8 = 64 bit integers

9. Oke,

I will try to make the exercise and post the code here for checking.
Both thanks for the help and patience.

Roelof

10. Hello,

I need your help one more time.
I now have this code:
Code:
```/*
* =====================================================================================
*
*       Filename:  test2.c
*
*    Description:  conventer from dec to 0101
*
*        Version:  1.0
*        Created:  14-05-11 10:03:25
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  Dr. Fritz Mehner (mn), mehner@fh-swf.de
*        Company:  FH Südwestfalen, Iserlohn
*
* =====================================================================================
*/

#include        <stdio.h>
#include        <stdlib.h>
int
main ( int argc, char *argv[] )
{
int nummer, uitkomst, teller, remainder ;
char uitkomst2[10] ;
nummer = 4 ;
uitkomst = 4 ;
while (uitkomst <0)
{
uitkomst = nummer/2;
printf ("uitkomst : %d", uitkomst);
remainder = nummer%2;
uitkomst2[teller]= remainder;
teller = teller +1 ;
nummer = uitkomst;
printf ("teller:%d  nummer:%d",teller, nummer);
}
while (teller !=0)
{
printf("%d", uitkomst2[teller]);
teller = teller -1 ;
}

return EXIT_SUCCESS;
}                               /* ----------  end of function main  ---------- */```
I now get no output except a cryptic command terminated message.

Anyone see what I do wrong here ?

Roelof

11. It's making me a little dizzy the way you're flipping numbers around in there...
For example...
Code:
``` remainder = nummer%2;
uitkomst2[teller]= remainder;
teller = teller +1 ;```
Can be combined as...

Code:
`uitkomst2[teller++]= nummer%2;`
with exactly the same result.

It's always a good idea to go through and minmize your code, eliminating any unnecessary variables and combining statements when you can.

12. Oke,

I changed the code to this :
Code:
```/*
*  * =====================================================================================
*
*          Filename:  test2.c
*
*          Description:  conventer from dec to 0101
*
*          Version:  1.0
*          Created:  14-05-11 10:03:25
*          Revision:  none
*          Compiler:  gcc
*
*          Author:  Dr. Fritz Mehner (mn), mehner@fh-swf.de
*          Company:  FH Südwestfalen, Iserlohn
*
* =====================================================================================
*/

#include        <stdio.h>
#include        <stdlib.h>

int
main ( int argc, char *argv[] )
{
int nummer, uitkomst, teller ;
char uitkomst2[10];
nummer = 4 ;
uitkomst = 4 ;
while (uitkomst <1)
{
uitkomst = nummer/2;
uitkomst2[teller++]= nummer%2;
printf ("teller:%d  nummer:%d",teller, nummer);
}
while (teller !=0)
{
printf("%d", uitkomst2[teller]);
teller = teller -1 ;
}
return EXIT_SUCCESS;
}                              /* ----------  end of function main  ---------- */```
But still no output and a cryptic message command terminated.

Anyone a idea waht happens.

Roelof

13. Oke,

I found it.
I forget to set teller to 0

But the problem of no output is still here

Anyone who has tips for that.

Roelof

14. Three guesses as to how many times this loop runs:
Code:
```            uitkomst = 4 ;
while (uitkomst <1)
{
uitkomst = nummer/2;
uitkomst2[teller++]= nummer%2;
printf ("teller:%d  nummer:%d",teller, nummer);
}```

15. Originally Posted by roelof
Oke,

I found it.
I forget to set teller to 0

But the problem of no output is still here

Anyone who has tips for that.

Roelof
Ok... look at the values of each variable as you come into the while loops...

nummer = 4, uitkomst = 4, teller = 0;
then you enter the loop... guarded by while (uitkomst less than 1)
uitkomst = 4
the loop never executes.
Now it falls through to the second loop... while (teller != 0)
Teller was unchanged by the first loop so teller = 0;
The second loop never executes.

Hense... no output.