Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How do you set, clear, and toggle a single bit?

How do you set, clear, and toggle a bit?
by

2 Answers

kshitijrana14
Using the Standard C++ Library: std::bitset<N>.
Or the Boost version: boost::dynamic_bitset.
There is no need to roll your own:
#include <bitset>
#include <iostream>

int main()
{
std::bitset<5> x;

x[1] = 1;
x[2] = 0;
// Note x[0-4] valid

std::cout << x << std::endl;
}

[Alpha:] > ./a.out
00010

The Boost version allows a runtime sized bitset compared with a standard library compile-time sized bitset.
MounikaDasa
Using the Standard C++ Library: std::bitset<N>.

Or the Boost version: boost::dynamic_bitset.

There is no need to roll your own:

#include <bitset>
#include <iostream>

int main()
{
std::bitset<5> x;

x[1] = 1;
x[2] = 0;
// Note x[0-4] valid

std::cout << x << std::endl;
}
[Alpha:] > ./a.out
00010
The Boost version allows a runtime sized bitset compared with a standard library compile-time sized bitset.

Login / Signup to Answer the Question.