Implement C function function with code--decimal conversion to binary __ function

Source: Internet
Author: User
Tags bitwise

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.