Programming implementation: Two int (32-bit) integer m and n in the binary expression, how many bits (bit) different? __ Programming

Source: Internet
Author: User

A

#define _crt_secure_no_warnings 1
#include <stdio.h>
#include <math.h>
int main ()
{
    int m = 0;
    int n = 0;
    int i = 0;
    int count = 0;
    printf ("Please enter two number: \ n");
    scanf ("%d%d", &m, &n);
    for (i = 0; i < i++)
    {if ((
        m >> i) & 1) ^ ((n >> i) (& 1))//Let M and N move to the right I bit then respectively and 1 phase again or if is true count++
            count++;
    printf ("count=%d", count);
    System ("pause");
    return 0;
}

Two

#define _crt_secure_no_warnings 1
#include <stdio.h>
#include <math.h>
int main ()
{
    int u = 0;
    int m = 0;
    int n = 0;
    int count = 0;
    printf ("Please enter two number: \ n");
    scanf ("%d%d", &m, &n);
    u = m^n;  M and N xor or while
    (U)   //equivalent to finding a number; the number of 1 in binary
    {
        count++;
        u = u & (u-1);
    }
    printf ("count=%d", count);
    System ("pause");
    return 0;
}

Three

#define _crt_secure_no_warnings 1
#include <stdio.h>
#include <math.h>
int main ()
{
    int i = 0;
    int m = 0;
    int n = 0;
    int count = 0;
    printf ("Please enter two number: \ n");
    scanf ("%d%d", &m, &n);
    for (i = 0; i < i++)
    {
        if ((M & 1)!= (n & 1))  
        {
            count++
        }
        m = M >> 1;
        n = n >> 1;
    }
    printf ("count=%d", count);
    System ("pause");
    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.