How many bits are there in the binary of two digits for a bitwise operation? different __ Binary

Source: Internet
Author: User
Tags bitwise

Problem Description:

There are 10 kinds of people in the world, one knows the binary system, one does not understand. So you know the binary expression of two int32 integers m and n, how many bits are different?


Enter an example:

1999 2299


Output Example:

7


My Code:

/** * The number of
	 different digits in the binary of two numbers
	 * @param m 
	 * @param n
	 * @return Returns the number of different digits/public
	static int Countbitdiff (int m, int n) {
		//XOR or
		int ans = m^n;
		Find the number of 1 in ans
		int count = 0;
		while (ans!= 0) {
			ans &= (ans-1);
			count++;
		}
		return count;
    }


Pay attention to how to find a number of 1 in a binary system through this problem.


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.