First, find the number of different bits in the binary number two
ideas:
- M and n is first bitwise exclusive or, when the same binary bits m and n are cleared, different bits are binary 1
- Statistics XOR result after the completion of the binary bits in the number of 1
void Dif_bit(int a, int b,int c)
{
c = a^b;
int i = 0;
int count = 0;
for (i = 0; i < 32;i++)
if (c >> i & 1 == 1)
count++;
printf("%d\n", count);
}
int main()
{
int a = 0;
int b = 0;
int c = a^b;
scanf("%d%d", &a, &b);
Dif_bit(a, b, c);
return 0;
}
Second, obtaining a binary integer all the even bit and odd bit sequences, respectively, to print out the binary sequence
(1) thinking:
- Extract all odd bit, if the bit is 1, output 1, output is 0 0
- In the same way even positions extraction
(2) detecting num is a bit in the mode 0 or 1:
- The mobile num i bits to the right
- The result after the completion of a shift and bitwise AND, if:
the result is 0, the i-th bit is 0
the result is non-zero, the i-th bit is 1
void Printbit(int num)
{
for (int i = 31; i >= 1; i -= 2)
{
printf("%d ", (num >> i) & 1);
}
printf("奇数位序列\n");
for (int i = 30; i >= 0; i -= 2)
{
printf("%d ", (num >> i) & 1);
}
printf("偶数位序列\n");
}
int main()
{
int num = 11;
Printbit(num);
return 0;
}
Third, the number of binary 1's count
(1) Method a:
cycle following until n is reduced to 0:
- The mold 2 with the data, it can be detected whether or not divisible by 2
- It can be: the binary data bits corresponding to the least significant bit must be 0, otherwise it is 1, if the count is incremented to 1 1
- If n is not equal to 0, 1 continued
//方法1
#include <stdio.h>
int main()
{
int num = 10;
int count= 0;//计数
while(num)
{
if(num%2 == 1)
count++;
num = num/2;
}
printf("二进制中1的个数 = %d\n", count);
return 0;
The method of the above-described defects: a lot and modulo division, division and modulo efficiency was relatively low.
(2) Method II:
a type of int data, corresponding to a total of 32 binary bits, the azimuth calculation can be employed
//方法2:
#include <stdio.h>
int main()
{
int num = -1;
int i = 0;
int count = 0;//计数
for(i=0; i<32; i++)
{
if( ((num>>i)&1) == 1 )
count++;
}
printf("二进制中1的个数 = %d\n",count);
return 0;
}
Method two advantages: a bit operation instead of division and modulus, high efficiency slightly
defects: no matter what the data is to be carried out 32 cycles
(3) Method three:
using two adjacent data is a bitwise
example:
9999: 10011100001111
first cycle: n = 9999 n = n & (n-1) = 9999 & 9998 = 9998
second cycles: n = 9998 n = n & (n-1) = 9998 & 9997 = 9996
third cycle: n = 9996 n = n & (n-1) = 9996 & 9995 = 9992
in the fourth cycle: n = 9992 n = n & ( n-1) = 9992 & 9991 = 9984
in the fifth cycle: n = 9984 n = n & (n-1) = 9984 9983 9728 & =
sixth cycle: n = 9728 n = n & (n-1) = 9728 & 9727 = 9216
VII cycles: n = 9216 n = n & (n-1) = 9216 & 9215 = 8192
eighth cycle: n = 8192 n = n & (n-1) = 8192 & 8191 = 0
The observed: in this way, the binary data bit in a few 1, the loop cycle times, and using the intermediate bit computing, processing them more efficient
#include <stdio.h>
int main()
{
int num = -1;
int i = 0;
int count = 0;//计数
while(num)
{
count++;
num = num&(num-1);
}
printf("二进制中1的个数 = %d\n",count);
return 0;
}
Fourth, the exchange of two variables (without creating temporary variables)
int main()
{
int a = 10;
int b = 11;
a = a^b;
b = a^b;
a = a^b;
printf("a=%d b=%d", a, b);
return 0;
}