You can do the conversion without using the pow function, instead shift the number right each interation.

See C program to convert decimal to binary number system using bitwise operator[^]

15,795,793 members

I want to make binary cnvert calculator. But I can't make it. Where it the bugs? Please help me to fix it.

**What I have tried:**

#include <stdio.h> #include <string.h> #include <math.h> int main() { char binar[65]; int decimal; int i,n,power,temp; while(1) { printf("Enter the decimal number : "); scanf("%d",&decimal); for(n = 0;pow(2,n) < decimal;n++){ temp = pow(2,n); } n -= 1; if(temp < decimal) { binar[0] = 1; } int j; for(i = n - 1 ,j = 1;j <= 7;i--,j++) { if(temp + pow(2,i) < decimal) { binar[j] = 1; temp = temp + pow(2,i); } else{ binar[j] = 0; } } for(i = 0;i < n;i++) { printf("%d",binar[i]); } printf("\n"); } return 0; }

Comments

You can do the conversion without using the pow function, instead shift the number right each interation.

See C program to convert decimal to binary number system using bitwise operator[^]

See C program to convert decimal to binary number system using bitwise operator[^]

Permalink

Share this answer

C

for (n = 0; pow(2, n) < decimal; n++) { temp = (int)pow(2, n); } n -= 1;

If the number of bits needed should be calculated here, then subtracting one here would not be correct. With negative numbers, n=0 would not work.Since negative numbers are binary based on the two's complement, this approach would not lead to a solution. The number of bits can be calculated without repeatedly calculating pow in a loop, or is the function log not allowed?

C

char binar[65];

Why is the array so big? Are there computers where an int has so many bits?

If you don't know, I would recommend allocating the memory in the necessary size.

C

if (temp < decimal) { binar[0] = 1; }

The query is always fulfilled with some certainty if decimal is not 0. In the most significant position binary[0] there is then (of course) a 1.

After that it just goes wrong, as you would certainly notice in the debugger.

C

for (i = n - 1, j = 1; j <= 7; i--, j++) { if (temp + pow(2, i) < decimal) { binar[j] = 1; temp = temp + (int)pow(2, i); } else { binar[j] = 0; } }

The loop continues even with i=-1 ...

If you simply shifted the decimal value and masked out the lowest digit, you could save yourself all that. And the result would be easier and also more correct.

Permalink

Share this answer

v5

Quote:Where it the bugs? Please help me to fix it.

There about 1 toll to help you discover what is going on by yourself, the

this simple code get input digits 1 by 1:

C++

int Num= 100; while (Num > 0) { int Digit= Num % 10; // get unit digit here Num= Num/ 10; printf("%i", Digit); } printf("\n");

Guess what happen if you replace 10 by 2.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.

The debugger is here to show you what your code is doing and your task is to compare with what it should do.

There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.

To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

Permalink

Share this answer

v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900

Btw. do you know what you are doing? This e.g. what should this part of the code

`for(n = 0;pow(2,n) < decimal;n++){`

why here atemp = pow(2,n);

}

`for`

is needed?10 --> 100 c/a 1010

I used this to get the value of n and temp.

for(n = 0;pow(2,n) < decimal;n++){

temp = pow(2,n);

}

`for`

is needed, think about it or explain me where I'm wrong.