Comparison operators for std::bitset ?

639 views
Skip to first unread message

Vincent Reverdy

unread,
Jan 19, 2014, 11:06:25 AM1/19/14
to std-dis...@isocpp.org
Hello.

I would like to know whether there is a reason why comparison operators are not available in std::bitset?
For bitsets with size lower than 64 bits, we can convert them to unsigned long long int and compare the values, but for 128 bits bitsets it is no longer possible in a standard way.
Why such a restriction?

Vincent

Daniel Krügler

unread,
Jan 19, 2014, 11:17:38 AM1/19/14
to std-dis...@isocpp.org
2014/1/19 Vincent Reverdy <vinc...@gmail.com>:
> Hello.
>
> I would like to know whether there is a reason why comparison operators are
> not available in std::bitset?

I cannot tell you about the historic background for this, sorry. I
*guess* that there are similar to those for not providing operator<
for std::complex.

> For bitsets with size lower than 64 bits, we can convert them to unsigned
> long long int and compare the values, but for 128 bits bitsets it is no
> longer possible in a standard way.
> Why such a restriction?

A good question - I see no good reasons for that restrictions. I would
at least like to see std::less provided for bitset to allow usage in
associative containers, but I also wouldn't mind to provide the
comparison functions for it.

- Daniel

Vincent Reverdy

unread,
Jan 19, 2014, 11:42:27 AM1/19/14
to std-dis...@isocpp.org
Thanks. One important thing, is that comparison operators for std::bitset are not equivalent to lexicographically compare of standard containers (like std::vector<bool>) because comparison start from the most significant bit and not from the least significant bit. But appart from that, I do not see any particular problem...

Gabriel Dos Reis

unread,
Jan 19, 2014, 1:13:43 PM1/19/14
to std-dis...@isocpp.org
Daniel Krügler <daniel....@gmail.com> writes:

| 2014/1/19 Vincent Reverdy <vinc...@gmail.com>:
| > Hello.
| >
| > I would like to know whether there is a reason why comparison operators are
| > not available in std::bitset?
|
| I cannot tell you about the historic background for this, sorry. I
| *guess* that there are similar to those for not providing operator<
| for std::complex.

I hope they aren't :-)

-- Gaby

Miro Knejp

unread,
Jan 19, 2014, 2:06:56 PM1/19/14
to std-dis...@isocpp.org
> I cannot tell you about the historic background for this, sorry. I
> *guess* that there are similar to those for not providing operator<
> for std::complex.
For complex numbers the answer is quite simple: they are not ordered.
There exists not mathematical definiton for complex c1 < c1. I assume
the issue with bitsets is to prevent ambiguities and/or inefficiencies.
Some people might expect (int)bs1 < (int)bs2, others the bits to be
compared lexicographically from index 0 upwards, and for architectures
that cannot do bitscans one would have to loop through all bits and
compare their bool values, involving lots of shifting and masking on
every comparison. But again, I'm just guesstimating.

Reply all
Reply to author
Forward
0 new messages