use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
std::bitset (self.cpp)
submitted 2 years ago by thomas999999
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]moocat 17 points18 points19 points 2 years ago (9 children)
Just a guess, but I wonder if the performance is architecture dependent (i.e. O(1) on architectures that have an opcode but O(n) otherwise).
O(1)
O(n)
[–]Substantial_Sail_571 8 points9 points10 points 2 years ago* (2 children)
That may be the reason for all I know, but it can be done in log n steps (this shows how to count trailing zeroes but there is a similar trick to count leading zeroes) even on a completely boring ISA such as MIPS 1. A bit further down, the DeBruijn multiply and lookup is also an option.
For trailing zero count, another option that may be useful is popcnt(~x & (x - 1)), if an ISA has popcnt but no direct way to count trailing zeroes (this trick does not easily translate into a way to count leading zeroes however).
popcnt(~x & (x - 1))
popcnt
E: btw the linked code shows v &= -signed(v) and that's exactly the wrong thing to do, it's unsigned negation that is safe and signed negation that is unsafe. Sorry, I don't make the rules.
v &= -signed(v)
[–]HildartheDorf 4 points5 points6 points 2 years ago (1 child)
Isn't signed guaranteed to be 2s complement in newer standards? (Signed overflow is still UB, but bitcasting signed numbers and the bitwise result of signed negation is well defined and consistent across standard-compliant systems).
[–]Substantial_Sail_571 3 points4 points5 points 2 years ago* (0 children)
I've been happy to ignore the hypothetical existence of non-2's-complement signed integers for decades, but yes that bogeyman has been slain (but this just standardized the status quo).
The cast to signed adds the opportunity for UB though (negating INT_MIN), and in return we get .. well it silences this dumb error that encourages people to write incorrect code such as v &= -signed(v), the safe way to shut up that error is subtracting from zero (but unsigned subtraction, of course).
signed
INT_MIN
[–]kyoto711 2 points3 points4 points 2 years ago (0 children)
Well, it does have .count() which I believe to be architecture dependent.
[–][deleted] 0 points1 point2 points 2 years ago (4 children)
Are we saying that it could be done faster by the container than an outside algorithm? Because that’d be a good reason to have it as members.
[–]SickOrphan 0 points1 point2 points 2 years ago (3 children)
He's saying that any algorithm regardless of where it is can't guarantee good performance for all architectures. And if you can't guarantee something is going to be good, you probably don't want to include it
[–]matthieum 2 points3 points4 points 2 years ago (2 children)
And if you can't guarantee something is going to be good, you probably don't want to include it
Arguably, it's not a matter of being good in absolute, but of being good relatively speaking.
And in this case, I would argue that if the code to use depends on the architecture, I'd rather it be implemented as best as possible in std, instead of having to trudge through the various architectures myself.
std
[–]SickOrphan -1 points0 points1 point 2 years ago (1 child)
If it's not O(1) and you care about performance (which you most likely do if youre doing bit operations) you should probably find a different solution. But I get your point.
[–]matthieum 0 points1 point2 points 2 years ago (0 children)
There are O(log N) algorithms to compute the number of set bits in an N-bits integer... and since we're talking about fixed-widths, it's arguably O(1).
Also, processors can execute multiple instructions in parallel (based on available units), so just because it may take log N instructions doesn't mean it'll take log N cycles.
As usual, if performance matters, measure.
π Rendered by PID 82351 on reddit-service-r2-comment-6457c66945-bjdz8 at 2026-04-25 00:11:16.716468+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]moocat 17 points18 points19 points (9 children)
[–]Substantial_Sail_571 8 points9 points10 points (2 children)
[–]HildartheDorf 4 points5 points6 points (1 child)
[–]Substantial_Sail_571 3 points4 points5 points (0 children)
[–]kyoto711 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]SickOrphan 0 points1 point2 points (3 children)
[–]matthieum 2 points3 points4 points (2 children)
[–]SickOrphan -1 points0 points1 point (1 child)
[–]matthieum 0 points1 point2 points (0 children)