Bitset Operators [C++ Reference]
Keywords: stl,bitset,bitset_operators
Date: 2010-04-08T14:19:55-0700

[[Bitset Operators]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- cz
- de
- es
- fr
- it
- jp
- nl
- pl
- ro
- ru
- sk
- tr
- tw

## Bitset Operators

Syntax:

        #include <bitset>
        !=, ==, &=, ^=, |=, ~, <<=, >>=, [], =

These operators all work with bitsets. They can be described as follows:

Operator | Description
--- | ---
`!=` | returns true if the two bitsets are not equal
`==` | returns true if the two bitsets are equal
`&=` | performs the AND operation on the two bitsets
`&` | performs the AND operation on the two bitsets
`^=` | performs the XOR operation on the two bitsets
`^` | performs the XOR operation on the two bitsets
`|=` | performs the OR operation on the two bitsets
`|` | performs the OR operation on the two bitsets
`<<=` | shifts the bitset to the left
`<<` | creates a copy of the bitset shifted to the left
`>>=` | shifts the bitset to the right
`>>` | creates a copy of the bitset shifted to the right
`~` | creates a reversed copy of the bitset (same as calling flip() on a copy)
`b[x]` | returns a reference to the xth bit in the bitset
`=` | take an unsigned long and put the corresponding bits in the bitset

For example, the following code creates a bitset and shifts it to the left 4 places:

       // create a bitset out of a number
       bitset<8> bs2( 131ul );
       // you can do bitset<8> bs2; bs2 = 131ul;

       cout << "bs2 is " << bs2 << endl;
       // shift the bitset to the left by 4 digits
       bs2 <<= 4;
       cout << "now bs2 is " << bs2 << endl;

When the above code is run, it displays:

     bs2 is 10000011
     now bs2 is 00110000

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •