位操作是一种用于各种问题的技术,可以以最佳方式获得解决方案。从竞争编程的角度来看,此技术非常有效。关于位运算符的全部内容都直接作用于二进制数字或数字位,从而有助于快速实现。下面是使用的按位运算符:
- 按位与(&)
- 按位或(|)
- 按位XOR(^)
- 按位非(!)
在本文中了解有关按位运算符的更多信息。
以下是编程中常用的一些常见位操作:
按位运算:
下表说明了使用按位运算符执行操作时的结果。分别为0或1此处的0或1的装置序列。
Operators | Operations | Result |
---|---|---|
XOR | X ^ 0s | X |
XOR | X ^ 1s | ~X |
XOR | X ^ X | 0 |
AND | X & 0s | 0 |
AND | X & 1s | X |
AND | X & X | X |
OR | X | 0s | X |
OR | X | 1s | 1s |
OR | X | X | X |
获取位:
此方法用于查找给定数字N的特定位置(例如i )的位。这个想法是找到给定数字和2 i的按位与,可以将其表示为(1 << i) 。如果返回值是1,则第i位的位置1 。否则,它不会被设置。
下面是相同的伪代码:
C++
// Function to get the bit at the
// ith position
boolean getBit(int num, int i)
{
// Return true if the bit is
// set. Otherwise return false
return ((num & (1 << i)) != 0);
}
C++
// Function to set the ith bit of the
// given number num
int setBit(int num, int i)
{
// Sets the ith bit and return
// the updated value
return num | (1 << i);
}
C++
// Function to clear the ith bit of
// the given number N
int clearBit(int num, int i)
{
// Create the mask for the ith
// bit unset
int mask = ~(1 << i);
// Return the update value
return num & mask;
}
C++
// C++ program to implement all the
// above functionalities
#include "bits/stdc++.h"
using namespace std;
// Function to get the bit at the
// ith position
bool getBit(int num, int i)
{
// Return true if the ith bit is
// set. Otherwise return false
return ((num & (1 << i)) != 0);
}
// Function to set the ith bit of the
// given number num
int setBit(int num, int i)
{
// Sets the ith bit and return
// the updated value
return num | (1 << i);
}
// Function to clear the ith bit of
// the given number N
int clearBit(int num, int i)
{
// Create the mask for the ith
// bit unset
int mask = ~(1 << i);
// Return the update value
return num & mask;
}
// Driver Code
int main()
{
// Given number N
int N = 70;
cout << "The bit at the 3rd position is: "
<< (getBit(N, 3) ? '1' : '0')
<< endl;
cout << "The value of the given number "
<< " after setting the bit at "
<< " MSB is: "
<< setBit(N, 0) << endl;
cout << "The value of the given number "
<< " after clearing the bit at "
<< " MSB is: "
<< clearBit(N, 0) << endl;
return 0;
}
设置位:
此方法用于将位设置为给定数字N的特定位置(例如i )。这个想法是将给定数字N的值更新为给定数字N和2 i的按位或,可以将其表示为(1 << i) 。如果返回值是1,则第i位的位置1 。否则,它不会被设置。
下面是相同的伪代码:
C++
// Function to set the ith bit of the
// given number num
int setBit(int num, int i)
{
// Sets the ith bit and return
// the updated value
return num | (1 << i);
}
清除位:
此方法用于清除给定数字N的特定位置(例如i )的位。我们的想法是,以更新给定数目N的位与所述给定数目N的值和的2 i中的称赞的是可被表示为〜(1 << i)中。如果返回值是1,则第i位的位置1 。否则,它不会被设置。
下面是相同的伪代码:
C++
// Function to clear the ith bit of
// the given number N
int clearBit(int num, int i)
{
// Create the mask for the ith
// bit unset
int mask = ~(1 << i);
// Return the update value
return num & mask;
}
下面是实现上述功能的程序:
C++
// C++ program to implement all the
// above functionalities
#include "bits/stdc++.h"
using namespace std;
// Function to get the bit at the
// ith position
bool getBit(int num, int i)
{
// Return true if the ith bit is
// set. Otherwise return false
return ((num & (1 << i)) != 0);
}
// Function to set the ith bit of the
// given number num
int setBit(int num, int i)
{
// Sets the ith bit and return
// the updated value
return num | (1 << i);
}
// Function to clear the ith bit of
// the given number N
int clearBit(int num, int i)
{
// Create the mask for the ith
// bit unset
int mask = ~(1 << i);
// Return the update value
return num & mask;
}
// Driver Code
int main()
{
// Given number N
int N = 70;
cout << "The bit at the 3rd position is: "
<< (getBit(N, 3) ? '1' : '0')
<< endl;
cout << "The value of the given number "
<< " after setting the bit at "
<< " MSB is: "
<< setBit(N, 0) << endl;
cout << "The value of the given number "
<< " after clearing the bit at "
<< " MSB is: "
<< clearBit(N, 0) << endl;
return 0;
}
输出:
The bit at the 3rd position is: 0
The value of the given number after setting the bit at MSB is: 71
The value of the given number after clearing the bit at MSB is: 70