0

How can I compare bitset with an integer ? Or more generally work with integer operator: something like this :

#include <iostream> 
#include <iomanip>
#include <bitset>
using namespace std;

int main()
{
bitset<4> _b1 = 3 ; 


if(_b1>=2 )
    cout<<_b1;



system("pause");
return 0;

}

Shahb
  • 47
  • 1
  • 6
  • 2
    You shouldn't use variable names that start with `_`; they are reserved for the implementation. – Alex Chamberlain Mar 11 '13 at 21:16
  • 2
    @AlexChamberlain [No, they're not](http://stackoverflow.com/a/228797/241631); that only applies to global namespaces. For local variables the name must begin with an underscore and be followed by an uppercase character (or another underscore) for it to be reserved by the implementation. – Praetorian Mar 11 '13 at 21:25
  • 1
    @Praetorian I usually don't like being wrong, but in this case, I'll give in to the fact that I'll forget those intricacies by tomorrow and go back to not using any variable that starts with `_`. – Alex Chamberlain Mar 11 '13 at 21:28
  • @Praetorian: I like the 'no _' rule better, regardless of whether or not it's true. It's simpler and easier to remember and doesn't cause me to wonder if a name is in a global namespace or not. And code conforming to it will not run afoul of the more complex rule, nor will it cause people to wonder if the more complex rule is being violated or if they're remembering it right. – Omnifarious Mar 11 '13 at 21:35
  • The second answer to [this question](http://stackoverflow.com/q/9702315/1084416) has a nice implementation for comparing `bitset`s. – Peter Wood Mar 11 '13 at 22:37
  • @Praetorian - names that contain two consecutive underscores **anywhere**, not just at the beginning, are reserved. – Pete Becker Mar 12 '13 at 04:17
  • @PeteBecker Yes, of course. The answer I linked to says as much, but I didn't notice my wording was kinda ambiguous. – Praetorian Mar 12 '13 at 05:19

4 Answers4

6

Use std::bitset<N>::to_ulong():

if(_b1.to_ulong() >= 2)
Zeta
  • 103,620
  • 13
  • 194
  • 236
  • 1
    This only works for small enough bitsets, otherwise you get an exception. – Peter Wood Mar 11 '13 at 22:38
  • @PeterWood: Which is also mentioned in the linked reference ;). An exception safe version which uses `to_ulong()` or `to_ullong()` or an additional `bitset` (depending on the right hand side and `N`, using `std::enable_if`) isn't that hard to write, I leave that as an exercise to the OP. – Zeta Mar 12 '13 at 07:53
  • I know it's in the reference, I'm just trying to avoid cache misses (c: – Peter Wood Mar 12 '13 at 08:01
1

There is a method to_ulong of bitset that returns the value of a bitset as unsigned long.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
1

You can use to_ulong to get the unsigned int value of the bitset:

 _b1.to_ulong() 

Here is the ref. In your case it would:

if(_b1.to_ulong()>=2 )
   cout<<_b1;

Also you should avoid system("pause").

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
0

You could use the to_ulong(), and compare to a long, or possibly convert the ulong to an int.

Silas
  • 406
  • 2
  • 11