Reputation: 485
I have a number as a bool array but I need to do arithmetic operations such as add and subtract and logical such as AND
on it with other numbers similar to it. how can do this operations in C++ without need to handle all boolean-specific calculations, and do it simply.
an example:
bool a[10];
bool b[10];
bool c[10];
c = a + b;
Upvotes: 2
Views: 1662
Reputation: 11181
You can use std::bitset
#include <bitset>
std::bitset<10> a(4);
std::bitset<10> b("0000001000");
std::bitset c = a.to_ulong() + b.to_ulong();
//Etc.
//You can also use
a[0] = 4; a[1] = 5; //to initialize / access
Upvotes: 8
Reputation: 24133
std::transform
can perform a binary operation on pairs of elements from two containers, and put the result into a third container. You can use std::logical_and
, and std::logical_or
to get the results you want:
transform(a, a+10,
b, b+10,
c, logical_and<bool>());
transform(a, a+10,
b, b+10,
c, logical_or<bool>());
Upvotes: 1