14
votes
Find the binary gap of a number N
Your current code allocates several objects on the heap even though it doesn't need to. A string, a character array and several character objects.
Puzzles like this can often be solved not by ...
14
votes
Linear version of std::bit_ceil that computes the smallest power of 2 that is no smaller than the input integer
#include <type_traits>
#include <cassert>
You are missing some includes. For example, you need <limits> for <...
13
votes
Accepted
Bitwise comparison of any structure
This comparison can fail if there are any padding bytes within T, as those extra bytes are not initialized to any particular value.
Another source of failure can ...
13
votes
Accepted
Fast XOR of multiple integers
Most of the time spent here is likely iteration overhead. If you're concerned about performance, offloading that to C can help.
In this case, functools.reduce ...
11
votes
Accepted
Proper Case Conversion (Performance)
std::vector<std::string> ToProperCase(std::vector<std::string>& array)
{
// ...
return array;
}
There are many styles when it comes to ...
10
votes
Checking if a number is a power of 2 without loops
Use only necessary #includes. The <stdlib.h> is not needed here.
Give your operators some breathing space. ...
10
votes
Accepted
Bouncing map back into its bounds, after user dragged it out
Readability
Mainly looking for readability improvements/getting rid of those double ifs.
Since you are considered about readability, I will focus on that part.
Declare a flags enum on multiple lines ...
10
votes
Accepted
Parsing a sequence of bits out of a bit field
This is not the kind of thing you should loop for. Just calculate a mask:
...
10
votes
Algorithm to find the sequence of bits that makes up to a given number
The function has Undefined Behaviour. Since calloc() can return a null pointer, we must not dereference bits (e.g. using ...
9
votes
Accepted
C Bit Utility Functions: Popcount, Trailing Zeros Count, Reverse All Bits
These look familiar to me, but they are apparently "different enough" that GCC and Clang fail to recognize them as idioms. Well, the bit-reversal comes out about as well as can be expected ...
9
votes
Counting Occurrences of a Specific 3-Bit Pattern in a Byte Array
I see basically two categories of solution that aren't bit-by-bit:
A table-based approach.
A word-oriented approach.
I will get to them, but using this as a hook:
I tried to write it in a generic ...
9
votes
Accepted
Computing the smallest number of the form floor(8^N / 7) that is greater or equal to a number
is there a more direct way?
Look up table
With 32-bit int, level is so limited in range as ...
8
votes
Accepted
How to optimize bitwise get/set/clear of ranges of bits on 8-bit integers in JavaScript?
For any of the following suggestions, please do profile them to test the performance difference. Performance is likely to vary across multiple browser implementations of the JavaScript interpretation ...
8
votes
Algorithm to find the sequence of bits that makes up to a given number
In addition to Toby's excellent advice, I'd like to point out that the code can be simplified without changing the algorithm.
I'll keep your variable names so that the suggested changes can be more ...
8
votes
Accepted
Compute the sum of even numbers between two values
The first implementation mixes integer and floating-point arithmetic. I definitely would avoid that.
The other two versions both use >> on a signed type (<...
8
votes
Fast XOR of multiple integers
As others have noted, your performance is likely limited by iteration overhead. And, as they've also noted, if you really want optimal performance, your best bet is offloading the work to a compiled ...
7
votes
Basic binary number container
I don't see the value in #define standard operators.
I see no reason in limiting bitwidth to powers of 2. A 24-bit integers have an equal right to exist. If you ...
7
votes
Accepted
Basic binary number container
I wanted to expand on @vnp's comment about the #defines, and focus on the use of the preprocessor overall. I would say every use of the preprocessor here, not ...
7
votes
Accepted
Less redundant and clearer implementation of action triggers from two modules
About the constraints
For reasons beyond my control, this code base is constrained to C++98 (so, no =delete, no auto etc., but ...
7
votes
Counting Occurrences of a Specific 3-Bit Pattern in a Byte Array
There's an assumption about what it means for the bit pattern to "span bytes" - we should document which endianness we're working to. This is part of the requirements-gathering which is ...
7
votes
Linear version of std::bit_ceil that computes the smallest power of 2 that is no smaller than the input integer
In addition to @indi's response.
Braces!
You should use braces, even for one-liners.
The lack of braces can lead to very subtle faults, as indentation will no longer match compiler interpretation:
<...
6
votes
Accepted
Take first 12 bits of a number if longer than 12
There is a lot of room for improvement here, but first, let us talk about code style and best practices.
using namespace std; is not a good practice, because it can ...
6
votes
Accepted
Rotating 3D bit board in C
Update to my previous answer!
The original questioner (@Zacariaz) reported that "I'm currently looking into so-called delta swaps." I don't know how Zacariaz got turned onto them, but they are indeed ...
6
votes
Fastest way to clamp an integer to the range 0-255
C++17 introduces std::clamp(), so your function can be implemented as follows:
...
6
votes
Bitwise comparison of any structure
The C++ way isn't necessarily to stay away of such things as bit comparisons, but rather to do it in the most obvious way possible -by that I mean that your code should look dangerous and explicit at ...
6
votes
Largest binary gap
I'm a bit late to the party, but I'll add my two cents nonetheless!
Your code is quite good, even if, as other reviewers pointed, you can still improve it marginally. But I also agree with other ...
6
votes
Hamming Distance in Python
That's a reasonable approach, but converting to strings early and then doing everything on strings leads to a lot of extra code. Note that this construct in your code, ...
6
votes
Accepted
Python binary calculator
General design comment
The fact that a binary number needs to access the application using it looks like a huge design issue.
A quick fix for this is to provide the desired numbers of bits instead - ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
bitwise × 349c++ × 80
c × 74
performance × 59
java × 56
algorithm × 52
python × 46
integer × 43
c# × 36
programming-challenge × 24
javascript × 19
beginner × 18
bitset × 16
python-3.x × 13
array × 13
c++11 × 12
assembly × 11
serialization × 11
comparative-review × 9
strings × 7
.net × 7
image × 7
compression × 7
python-2.x × 6
time-limit-exceeded × 6