Bit Operations (Basic)

Foreword
As we all know, bit operation is something that we must learn in computer science. The predecessors used binary and bit operation to give us a computer with simple operation, but we seldom touch bit operation. Today, I will introduce some of the use of bit operations in algorithms.

Bitwise ORing
&
Bitwise AND
If both corresponding binary bits are 1, the result value of that bit is 1, otherwise it is 0
|
Bitwise OR
As long as one of the two corresponding binary bits is 1, the result of that bit is 1 The result value is 1
^
bitwise exclusive OR
If the two binary bits participating in the operation have the same value, it is 0, otherwise it is 1
~
Inversion
~ is a unary operator, which is used to invert a binary number bitwise, that is, 0 becomes 1 , 1
<<
left shift
is used to shift all the binary bits of a number to the left by N bits, and right complement 0
> > >>>>
Shift right Shiftsa
number by N bits to the right, and the lower bits shifted to the right end are discarded. For unsigned numbers, the upper bits are filled with
0s

 x & (x-1)
x = 1100
x-1 = 1011
x & (x-1) = 1000

1.1. Application 1 Use O(1) time to detect whether the integer n is a power of 2.
Idea analysis: If N is a power of 2, then N satisfies two conditions.
There is only one 1 in the binary representation of 1.N>0
2.N, and there is only one 1 in the binary representation of one
-bit N, so use N&(N-1) to eliminate the only one 1.
If N is a power of 2, then N&(N-1) gets a result of 0, which can be judged.
1.2. Apply 2 to calculate how many 1s are in the binary representation of a 32-bit integer.
Idea analysis:
Eliminate the last bit of x by x & (x-1). Use x & (x-1) to eliminate the last 1 in a loop, and calculate how many times it has been eliminated in total.
1.3. How many bits need to be changed to convert an integer A to B?
Ideas Analysis
This application is an extension of the above application.
Think about converting the integer A to B, if A and B are equal in the i-th (0<=i<32) bit, you don't need to change this BIT bit, if they are not equal in the i-th bit, you need to change this BIT bit. So the question turns into how many BIT bits A and B have different. The associative bit operation has an XOR operation, the same is 0, and the difference is 1, so the problem turns into calculating the number of 1s in the number after A XOR B.

Given an integer, write a function to determine if it is a power of 2.
Example 1:
Input: 1
Output: true
Explanation: 2^0 = 1

It can be solved quickly by using bitwise operations.
Because the powers of 2 are all 1, 10, 100, 1000, 10000,
so if it is a power of 2, then n&(n-1)=0 The
code is as follows:

class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n <= 0 ) return false;
        return !(n&(n-1)); 
    }
};

Example 2:
Find the value of the modulo p of a raised to the b power.
Input format
Three integers a, b, p, separated by spaces on the same line.
Output Format
Output an integer representing the value of a^b mod p.
Data range
1≤a,b,p≤109
Input example:

3 2 7

Sample output:

2

Problem-solving idea 1:
(violent enumeration) O(m)O(m)
violent enumeration is just a loop

#include <iostream>
using namespace std; 
#define ll long long //自定义ll为long long类型 
int main()
{
    ll a,b,c,ans=1;
    cin>>a>>b>>c;
    for (ll i=1;i<=b;i++)
        ans=ans*a%c;
    cout<<ans;
}

Problem- solving idea 2:
General idea: We know that any natural number can be written as

  n=2^pi1+2^pi2+2^pi3+……2^pim

Among them, all pi are non-negative integers, so you can use dichotomy to convert the number m of this question.

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring> 
using namespace std;
#define ll long long
ll n,m,k;
ll power(ll n,ll m,ll k)
{
    ll ans=1%k;
    while(m)
    {
        if (m&1)//如果m为奇数
            ans=ans*n%k;
        n=n*n%k;//将上一次的n进行平方
        m>>=1;
    }
    return (ans%k);
}
int main()
{
    cin>>n>>m>>k;
    n%=k;
    cout<<power(n,m,k);
} 

Convert decimal number to binary number

	while(n > 0){
		arr[j] = (n & 1);
		n >>= 1;
		j = j + 1;
	}
	for(int i = j - 1;i >= 0 ;i--){
		cout << arr[i];
	}

Examples of base conversion

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325736562&siteId=291194637