Article directory
write in front
The AND operation is a commonly used binary operation, and its truth table is as follows
x | and | x&y |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
(0 is 0, all 1 is 1)
Common applications of AND operation
Judging parity
Odd numbers must be obtained by adding one to a multiple of two, so the end of the binary representation must be 1 (the last digit in binary corresponds to decimal is 2 0 2^020 ), the end of the binary of even numbers must be 0 for the same reason. So using this property we can use it to determine the odd and even numbers
bool check(int n) {
return n&1;
}
take out some
With the property that a binary digit must be the original number after adding 1, and a number must be 0 after adding 0, certain bits of a number can be extracted. And how many bits can be used to eliminate certain digits using the largest digit in a given range of numbers
Power of 2 determination
If a number is a power of 2, then its binary representation must be 00...0100...00. Because the meaning of each bit in binary is the nth power of two, if there is only one 1 in the binary representation, then it must be a power of 2. Because of the carry relationship, the binary representation of a power of 2 minus one must be all 1. The result of this sum is 0.
bool check(int n) {
return n&(n-1);
}
Take out the number consisting of a 1 in the lowest digit of the binary and the following 0
The binary of a negative number is the inversion of the positive number and the addition of one. By adding a positive number and its corresponding negative number together, you can get a number consisting of a 1 in the lowest digit of the binary and a 0 behind it. This is the very common lowbit function
x | if(x) |
---|---|
10 | 1010 |
-10 | 0110 |
lowbit | 10 |
int lowbit(int n) {
return n&-n;
}
homework
191. Number of bit 1s
Shift right all the time to determine whether each binary bit is 1
class Solution {
public:
int hammingWeight(uint32_t n) {
int cnt=0;
while(n) {
if(n&1) cnt++;
n>>=1;
}
return cnt;
}
};
1356. Sort according to the number of 1's under the digital binary
Build a two-dimensional array, tu[i][j] stores the actual number, i represents the number of 1s under the binary number of the number, j represents the current storage position (each line uses an array to simulate a queue, the first dimension coordinates is the index of the queue)
class Solution {
public:
vector<int> sortByBits(vector<int>& arr) {
int tu[35][510]={
0},cnt[35]={
0};
vector<int>res;
for(auto t:arr) {
int g=t,ct=0;
while(g) {
if(g&1) ct++;
g>>=1;
}
tu[ct][cnt[ct]++]=t;
}
for(int i=0;i<32;i++) {
sort(tu[i],tu[i]+cnt[i]);
for(int j=0;j<cnt[i];j++) {
res.push_back(tu[i][j]);
}
}
return res;
}
};
762. Prime Number Calculated Set in Binary Representation
A function of violently judging prime numbers, a function that counts the number of binary 1s in a loop, win
class Solution {
public:
bool is_prime(int n) {
if(n==1) return false;
for(int i=2;i<=n/i;i++) {
if(n%i==0) return false;
}
return true;
}
int cul(int n) {
int cnt=0;
while(n) {
if(n&1) cnt++;
n>>=1;
}
return cnt;
}
int countPrimeSetBits(int left, int right) {
int ans=0;
for(int i=left;i<=right;i++) {
if(is_prime(cul(i))) ans++;
}
return ans;
}
};
231. Powers of 2
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n<=0) return false;
return !(n&(n-1));
}
};