Application of HDU 3711 binary number bit (^ and &)

Source: Internet
Author: User

The two numbers A and B are different or the result C is that their binary values are 0 and different are 1, so this question is to count the number of 1 in the binary of C, there are many ways to calculate the number of 1 in a binary number. The following describes several methods:

1. number C gets the remainder of 2, and determines whether the remainder is 1. If it is recorded once, then number C is divided by 2, and the previous description is carried out to know that number C is 0, and the code is as follows:

int getBits(int n) {int res;res = 0;while (n) {if (n % 2 == 1) {res++;}n /= 2;}return res;}

2. Same as above, but remove the remainder from 2, Use &, divided by 2, and shift right. The Code is as follows:

int getBits(int n) {int res;res = 0;while (n) {if (n & 2 == 1) {res++;}n >>= 2;}return res;}

3. If an integer is not 0, at least one of the integers is 1. If we subtract 1 from this integer, the first 1 on the rightmost side of the integer will become 0, and the first 0 after 1 will become 1. All other bits will not be affected. For example, if a binary number is 1100, the third digit from the right is 1 on the rightmost side. After 1 is subtracted, the third digit becomes 0, and the two digits after it become 1, while the first one remains unchanged. Therefore, the result is 1011.

We found that the result of 1 reduction is to reverse all bits starting from 1 on the rightmost side. At this time, if we calculate the original integer and the result after subtracting 1, all the bits will change from the first digit on the rightmost side of the original integer to 0. For example, 1100 & 1011 = 1000. That is to say, subtract 1 from an integer and then

int getBits(int n) {int res;res = 0;while (n) {res++;n = n & (n - 1);}return res;}

PS: N & (n-1) = 0; // If the binary number has only one bit of 1, the number is an integer power of 2.

 

/*
* Hdu3711.c
*
* Created on: 2011-8-27
* Author: Wang Zhu
*/

# Include <stdio. h>
# Include <stdlib. h>
# Define Nmax 101
# Define INF 0x7fffffff
Int NUMA [Nmax], numb [Nmax];
Int CMP (const void * a, const void * B ){
Return * (int *) A-* (int *) B;
}
Int getnum0 (int n ){
Int res;
Res = 0;
While (n ){
Res ++;
N = N & (n-1 );
}
Return res;
}

Void solve (int m, int N ){
Int I, J, K, Te, temp, Nmin;
For (I = 0; I <n; I ++ ){
Nmin = inf, K =-1;
For (j = 0; j <m; j ++ ){
Te = numb [I] ^ NUMA [J];
Temp = getnum0 (TE );
If (Nmin> temp ){
Nmin = temp;
K = J;
}
}
Printf ("% d \ n", numa [k]);
}
}
Int main (){
# Ifndef online_judge
Freopen ("in. Data", "r", stdin );
# Endif
Int T, I, m, n;
While (scanf ("% d", & T )! = EOF ){
While (t --){
Scanf ("% d", & M, & N );
For (I = 0; I <m; I ++ ){
Scanf ("% d", numa + I );
}
For (I = 0; I <n; I ++ ){
Scanf ("% d", numb + I );
}
Qsort (NUMA, M, sizeof (NUMA [0]), CMP );
Solve (m, n );
}
}
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.