如何在C++中进行布尔运算

如何在C++中进行布尔运算,c++,c++11,stdvector,boolean-operations,C++,C++11,Stdvector,Boolean Operations,我想添加两个bolean向量 vector<bool> v1= {0,0,1} vector<bool> v2= {1,0,1} vector<bool> resultedVector = v1+v2 有人知道如何使用c++/c++11吗? 我想把每次给定的布尔向量增加1。只想使用二进制操作。或者可以创建给定数量变量的bolean真值表 >在C++中执行二进制加法,可以使用这里描述的函数: 我从该链接实现了该功能,以符合您的规范,如下所示: std::v

我想添加两个bolean向量

vector<bool> v1= {0,0,1}
vector<bool> v2= {1,0,1}
vector<bool> resultedVector = v1+v2 
有人知道如何使用c++/c++11吗?
我想把每次给定的布尔向量增加1。只想使用二进制操作。或者可以创建给定数量变量的bolean真值表

>在C++中执行二进制加法,可以使用这里描述的函数:

我从该链接实现了该功能,以符合您的规范,如下所示:

std::vector<bool> add(const std::vector<bool>& a, const std::vector<bool>& b)
{
        bool c;
        std::vector<bool> result;
        for(int i = 0; i < a.size() ; i++){
                result.push_back(false);
                result[i] = ((a[i] ^ b[i]) ^ c); // c is carry
                c = ((a[i] & b[i]) | (a[i] & c)) | (b[i] & c);
        }
        return result;
}

此函数获取布尔的两个向量,并假设它们的大小相同,然后返回结果向量。显然,此函数不处理溢出或不同大小的数字。如果需要这些功能,您可以自己修改它。此外,您似乎正在讨论布尔向量的重载运算符,您可以通过检查运算符重载来实现这一点,但此逻辑将允许您添加存储在向量中的两个布尔数

以下是如何使用有状态函子:

struct BitAdder {
    bool carry_ = 0x0;  // Range is [0, 1].

    // Only accepts single bit values for a and b.
    bool operator()(bool a, bool b) {
        assert(a == (a & 0x1) && b == (b & 0x1));
        char sum = a + b + carry_;
        carry_ = (sum & 0x2) >> 1;  // Keep in range.
        return sum & 0x1;
    }
};

// Code is more straightforward when bits are stored in reverse.
std::vector<bool> v = {0, 1, 1, 1, 0};  // To be interpreted as:  1110 (14).
std::vector<bool> w = {1, 0, 1, 1, 0};  // To be interpreted as:  1101 (13).
std::vector<bool> result = {0, 0, 0, 0, 0};  //     Will become: 11011 (27).

assert(v.size() <= w.size());  // v and w can be iterated over together.
assert(v.size() <= result.size());  // There is enough space to store the bits.
assert(v[v.size() - 1] + w[v.size() - 1] < 2);  // No overflow can happen.
std::transform(v.cbegin(), v.cend(), w.cbegin(), result.begin(), BitAdder());

std::cout << "want: 11011, got: ";
std::copy(result.crbegin(), result.crend(), std::ostream_iterator<bool>(std::cout));
std::cout << '\n';

我不确定我是否理解你的问题。因为这看起来像是家庭作业,问题的重点似乎是运算符重载,所以这里有一个想法,而不是完整的答案:

#include <vector>

std::vector< bool > operator+( const std::vector<bool>& a, const std::vector<bool>& b )
{
  std::vector< bool > r;
  // your code goes here
  return r;
}

int main()
{
  std::vector< bool > a, b, c;
  c = a + b;
  return 0;
}
编辑-一天后

以下是增量问题的解决方案:


你想附加向量吗?你看过std::bitset:?@Luqman听起来你应该只使用int进行数学运算,然后在最后取出你关心的位。加法实际上不是一个布尔运算。您似乎在寻找任意精度的int?您提供的与布尔运算无关。相反,你想要实现二进制加法。与嵌入式加法相比,这种方法会非常慢。是的,显然最好的方法是使用现有的类型,如int,并通过整数加法执行布尔运算,但是我回答了这个问题,假设我们需要使用一个向量。如果操作数的大小不同呢?a+b+进位不是布尔算术。
#include <vector>

std::vector< bool > operator+( const std::vector<bool>& a, const std::vector<bool>& b )
{
  std::vector< bool > r;
  // your code goes here
  return r;
}

int main()
{
  std::vector< bool > a, b, c;
  c = a + b;
  return 0;
}
#include <iostream>
#include <vector>

// preinc - no grow on overflow
std::vector< bool >& operator++( std::vector<bool>& v )
{
  for ( auto e : v )
    if ( e = !e )
      break;
  return v;
}

// postinc - no grow on overflow
std::vector< bool > operator++( std::vector<bool>& v, int )
{
  auto t { v };
  operator++( v );
  return t;
}

// insert
std::ostream& operator<<( std::ostream& os, const std::vector< bool > v )
{
  for ( std::vector< bool >::const_reverse_iterator ci = v.rbegin(); ci != v.rend(); ++ci )
    os << *ci ? '1' : '0';
  return os;
}

int main()
{
  std::vector< bool > b {0,0,0,0};
  for ( int i = 0; i < 16; ++i )
  {
    std::cout << b << std::endl;
    ++b;
  }

  return 0;
}