2

In a lot competitive programming websites and books I've seen questions and solutions in which bitmasking has been used, for eg in many Dynamic Programming and Complete Search problems. I thought they'd be preferred over arrays of bools because they are more lightweight as they take only 1 bit per value as compared to 1 byte per value in array of bools.

However when I got to know about bitsets I wondered why bitmasks are used as bitsets also take same amount of memory, i.e 1 bit per value. But they have the added advantage that they are easier to use and manage. And now I'm confused regarding this.

So TL; DR why are bitmasks preffered over bitsets, especially in the context of Competitive Programming? To give an idea, here is a question that requires dynamic programming and bitmasking, and has a somewhat complicated (at least for me at my level, I'm only a 10th grader) solution that I saw in the editorial. https://www.hackerrank.com/challenges/synchronous-shopping

Thanks for answering my queries in advance!

Edit: Regarding the hateful comments about competitive programming, if you believe it's malpractice to use bitmasks, please try converting the code in the editorial of the problem link above to use bitsets and get an AC verdict.

SinByCos
  • 533
  • 1
  • 6
  • 18
  • 5
    Competitive programming relies on a lot of bad practices. To me it's a exercise to teach inexperienced programmers how not to write good `c++` code. – drescherjm Jan 12 '17 at 17:30
  • 3
    But more to your doubts: it would probably have to do with speed of execution. Bitmasks can be manipulated at pretty low level but bitsets required more overhead. If I recall my c/c++ correctly – efekctive Jan 12 '17 at 17:33
  • @efekctive: [Do they though?](https://softwareengineering.stackexchange.com/questions/284160/is-there-any-advantage-to-c-style-bit-manipulation-over-stdbitset#comment721742_284163) Or is that just more of the sort of [FUD propagated by these "competitive programming websites"](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h/31816096#comment51836350_31950147)? – Lightness Races in Orbit Jan 12 '17 at 17:35
  • I have not even looked at competitive programming. So no clue. A bitset requires finding the bit in the set, set it, etc. A bitmask with a bitwise operator is done. But the idea, I think, is to clarify the meaningful part of the question for the 10th grader. – efekctive Jan 12 '17 at 17:42
  • 2
    This is going to get closed as "primarily opinion based", but the only good reasons to use bitmasks are "that's the idiom around here", and "measurement has shown that we can get a useful performance advantage". The second reason is likely to indicate a poor toolchain. Bitsets don't *need* to be slower than bitmasks. – Martin Bonner supports Monica Jan 12 '17 at 17:42
  • 1
    It is also amazing how long old information can hang around. Compiled correctly a bitset should be as good, but this may not always have been the case. Also, based on the amount of C-style thinking still floating around in C++ code, folks falling back on masking should surprise no one. And the users of the masks might not know that `bitset` even exists. – user4581301 Jan 12 '17 at 17:50
  • 1
    @RawN I don't think it's beneficial to share your opinions about competitive programming, especially in a negative and discouraging fashion. Competitive programming is programming, and therefore within scope of suitable stackoverflow questions (although this particular question is a little fuzzy to be on-topic). – Paul Hankin Jan 12 '17 at 17:54
  • 2
    To the poster: don't marry into one structure over another (in any language or type of structure) try any and keep whatever makes sense in the situation. You will have enough time to get into the language/camp wars later – efekctive Jan 12 '17 at 17:57
  • In many embedded systems, a bitmask can be defined as a constant and be more readable than a bitset. – Thomas Matthews Jan 12 '17 at 20:27
  • Hello. What if I wanted to answer my own question? I think I have found the reason why bitmasks are often preffered over bitsets, and its not opinion based. – SinByCos Feb 03 '17 at 12:00

0 Answers0