Decimal conversion to binary, in mathematics there are many kinds of solution, you can remove the remainder after 2, you can subtract 2 of the n power, N diminishing. But in C, there are simple and ingenious algorithms.
We know that any number in the computer are stored in binary code, in the C language textbooks in the middle of the operation is generally speaking relatively simple, and this example, I saw a bit of manipulation of the ingenious place. Move one bit at a time in binary, equivalent to *2, and there is a bitwise and & this function, so we can think, with a binary left, and then with the 10 binary bitwise AND, this can express the binary form of 10.
Because of the spread of the online replication of other people's achievements, and without analysis, I have a deep sense of pain in this phenomenon. But in view of my limited technology, self-study C + + the next day, so this code is still refer to others, but after their own add changes, they also mastered the skills of the analysis, I believe that the next time I can completely learn the code to come, refueling.
/* Decimal conversion to binary * *
#include "Stdio.h"
#include "Conio.h"
#define N 16/*16 indicates that the int is a 16-bit container * *
void change (int X)
{
int i;
printf ("The result's change is:");
for (i=1;i<=n;i++)
{
if ((1<<n-i) &x)/*1 left N-i, as long as there is a bitwise in the binary and then 1 output 1*/
printf ("1");
Else
printf ("0");
}
GetChar ();
}
int main (void)
{
int x;
printf ("Input number:");
scanf ("%d", &x);
Change (x);
GetChar ();
return 0;
}