C++ 中 std::bitset 的算术运算
bitset 是一个布尔值数组,但每个布尔值不是单独存储的。相反,bitset 优化了空间,使得每个 bool 只占用 1 位空间,因此 bitset 占用的空间说, bs小于bool bs[N]和vector
重要的提示:
- Take care of integer overflow say if bitset is declared of size 3 and addition results 9, this is the case of integer overflow because 9 cannot be stored in 3 bits.
- Take care for negative results as bitsets are converted to unsigned long integer, so negative numbers cannot be stored.
添加2个bitset:按照以下步骤解决问题:
- 将 bool进位初始化为 false。
- 创建一个位集ans来存储两个位集x和y的总和。
- 遍历位集x和y的长度,并使用 fullAdder函数确定ans 中当前位的值。
- 返回ans 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Utility function to add two bool values and calculate
// carry and sum
bool fullAdder(bool b1, bool b2, bool& carry)
{
bool sum = (b1 ^ b2) ^ carry;
carry = (b1 && b2) || (b1 && carry) || (b2 && carry);
return sum;
}
// Function to add two bitsets
bitset<33> bitsetAdd(bitset<32>& x, bitset<32>& y)
{
bool carry = false;
// bitset to store the sum of the two bitsets
bitset<33> ans;
for (int i = 0; i < 33; i++) {
ans[i] = fullAdder(x[i], y[i], carry);
}
return ans;
}
// Driver Code
int main()
{
// Given Input
bitset<32> a(25);
bitset<32> b(15);
// Store the result of addition
bitset<33> result = bitsetAdd(a, b);
cout << result;
return 0;
}
C++
// C++ program for the above approach
#include
using namespace std;
// Utility function to subtract two bools and calculate diff
// and borrow
bool fullSubtractor(bool b1, bool b2, bool& borrow)
{
bool diff;
if (borrow) {
diff = !(b1 ^ b2);
borrow = !b1 || (b1 && b2);
}
else {
diff = b1 ^ b2;
borrow = !b1 && b2;
}
return diff;
}
// Function to calculate difference between two bitsets
bitset<33> bitsetSubtract(bitset<32> x, bitset<32> y)
{
bool borrow = false;
// bitset to store the sum of the two bitsets
bitset<33> ans;
for (int i = 0; i < 32; i++) {
ans[i] = fullSubtractor(x[i], y[i], borrow);
}
return ans;
}
// Driver Code
int main()
{
// Given Input
bitset<32> a(25);
bitset<32> b(15);
// Store the result of addition
bitset<33> result = bitsetSubtract(a, b);
cout << result;
return 0;
}
输出
000000000000000000000000000101000
时间复杂度: O(N),N 是位集的长度
辅助空间: O(N)
2 bitset 相减:按照以下步骤解决问题:
- 将 bool借用初始化为 false。
- 创建一个位集ans来存储两个位集x和y之间的差异。
- 遍历位集x和y的长度,并使用 fullSubtractor函数确定ans 中当前位的值。
- 返回ans 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Utility function to subtract two bools and calculate diff
// and borrow
bool fullSubtractor(bool b1, bool b2, bool& borrow)
{
bool diff;
if (borrow) {
diff = !(b1 ^ b2);
borrow = !b1 || (b1 && b2);
}
else {
diff = b1 ^ b2;
borrow = !b1 && b2;
}
return diff;
}
// Function to calculate difference between two bitsets
bitset<33> bitsetSubtract(bitset<32> x, bitset<32> y)
{
bool borrow = false;
// bitset to store the sum of the two bitsets
bitset<33> ans;
for (int i = 0; i < 32; i++) {
ans[i] = fullSubtractor(x[i], y[i], borrow);
}
return ans;
}
// Driver Code
int main()
{
// Given Input
bitset<32> a(25);
bitset<32> b(15);
// Store the result of addition
bitset<33> result = bitsetSubtract(a, b);
cout << result;
return 0;
}
输出
000000000000000000000000000001010
时间复杂度: O(N),N 是位集的长度
辅助空间: O(N)
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程。