LoginSignup
1
1

More than 5 years have passed since last update.

[C++] A easy way to check bits of int

Posted at

In c++ we can use bit set to check the binary formation of a int value.

int a = -1;
std::bitset<32> b(a);
cout << b << endl;

The output

11111111111111111111111111111111

We also can loop every bits and output it.

void binary_formation(int x) {
    for (int i = 31; i >= 0; --i) {
        cout << (((1 << i) & x) ? 1 : 0);
    }
    cout << endl;
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1