Skip to main content
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 ...
Roland Illig's user avatar
  • 21.9k
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 <...
indi's user avatar
  • 16.6k
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 ...
1201ProgramAlarm's user avatar
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 ...
STerliakov's user avatar
  • 2,142
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 ...
Snowhawk's user avatar
  • 6,770
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. ...
vnp's user avatar
  • 58.7k
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 ...
dfhwze's user avatar
  • 14.2k
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: ...
Reinderien's user avatar
  • 71.2k
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 ...
Toby Speight's user avatar
  • 88.7k
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 ...
user555045's user avatar
  • 12.6k
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 ...
user555045's user avatar
  • 12.6k
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 ...
chux's user avatar
  • 36.5k
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 ...
Reinderien's user avatar
  • 71.2k
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 ...
Martin R's user avatar
  • 24.2k
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 (<...
Toby Speight's user avatar
  • 88.7k
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 ...
Sean Werkema's user avatar
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 ...
vnp's user avatar
  • 58.7k
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 ...
indi's user avatar
  • 16.6k
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 ...
G. Sliepen's user avatar
  • 69.5k
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 ...
Toby Speight's user avatar
  • 88.7k
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: <...
Matthieu M.'s user avatar
  • 6,399
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 ...
Ben Steffan's user avatar
  • 5,333
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 ...
Quuxplusone's user avatar
  • 19.7k
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: ...
G. Sliepen's user avatar
  • 69.5k
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 ...
papagaga's user avatar
  • 5,817
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 ...
papagaga's user avatar
  • 5,817
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, ...
user555045's user avatar
  • 12.6k
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 - ...
SylvainD's user avatar
  • 29.8k

Only top scored, non community-wiki answers of a minimum length are eligible