Skip to main content
OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community. Learn more
61 votes
Accepted

Decide if the sum of three numbers is even or odd

First off, you have nice, clean, well-formatted, easy-to-read code. You have even included comments that explain the goal of each instruction. Too much of the time I review assembly-language code, ...
Cody Gray - on strike's user avatar
19 votes
Accepted

Endianness conversion in C

First off, Jean-François is absolutely right: you cannot assume any particular bit widths for the built-in types, short, int, <...
Cody Gray - on strike's user avatar
17 votes
Accepted

Implementation of C Standard Library Function ntohl()

Unless you wish to optimize the code, with specialized swappers for various hosts orders, you are doing it wrong. I invite you to check The Byte Order Fallacy by Rob Pike. The punch line: the byte ...
Matthieu M.'s user avatar
  • 5,137
17 votes
Accepted

Compressing a 'char' array using bit packing

Conditional jumps are murder on throughput due to branch-misprediction. Consider simply using a lookup-table instead: ...
Deduplicator's user avatar
  • 19.3k
16 votes
Accepted

Calculating the 'binary gap' of a number

I think using Regex will be too much for this little task. A simple split by 1s should suffice. ...
Xiaoy312's user avatar
  • 3,136
16 votes
Accepted

Calculating int next power of two

Java has Integer.highestOneBit that can make this a bit simpler: ...
janos's user avatar
  • 111k
15 votes
Accepted

Program that guesses your number using bitwise operations

Here are some things that may help you improve your code. Understand type implications The code includes a bitFlag array that is declared as ...
Edward's user avatar
  • 66.4k
14 votes
Accepted

Bit shifting and masking

Layout Put your main method on the top, since it tells main purpose of the application. And, C# isn't like C++, you don't have to define the method ahead, before calling them. ...
Xiaoy312's user avatar
  • 3,136
14 votes
Accepted

Return the next power of two

If you really care about performance you may also give a chance to a branchless version. Note that compiler may optimize code with explicit branches (if and ...
Adriano Repetti's user avatar
14 votes
Accepted

Counting the number of "on" bits in an int

The current implementation Your implementation is correct, and takes \$O(n)\$ time, where \$n\$ is the number of bits in an int. One thing you can improve is the ...
JS1's user avatar
  • 28.5k
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.3k
13 votes
Accepted

Start an EventHandler in different Thread

Don't reinvent the wheel: use EventArgs The StateChanged event isn't compliant with established C# conventions for creating events. ...
Mathieu Guindon's user avatar
13 votes

Decide if the sum of three numbers is even or odd

The beginning of your program obviously should be following the "HINT" and using xor instead of add. This is some really wacky ...
Quuxplusone's user avatar
  • 19.4k
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
11 votes
Accepted

Check if a given integer is power of two

Code organization There is nothing in the description and the posted code to justify storing the result of the "is power of 2" calculation in a class. It would be better to simply return it. The code ...
janos's user avatar
  • 111k
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,148
10 votes

Copying 80 bytes as fast as possible

Taking Benefits of The Out-of-Order Execution Engine You can also read about The Out-of-Order Execution Engine in the "Intel® 64 and IA-32 Architectures Optimization Reference Manual", ...
Maxim Masiutin's user avatar
10 votes
Accepted

Calculating the results of bit operations

...
Mathieu Guindon's user avatar
10 votes
Accepted

Integer Log2 implemented using binary search

I was initially skeptical of your benchmarking results because of your testing methodology: I test both of the method by sending 1 << n, n from 0 to 31 to them, and repeat for 1000000 times, ...
Cody Gray - on strike's user avatar
10 votes
Accepted

N-Queens, bitwise approach

Micro-optimizing Python is a bit of a quixotic endeavour — if you wanted the fastest possible performance then you wouldn't be using Python. But so long as we understand the nature of the exercise, ...
Gareth Rees's user avatar
  • 49.6k
10 votes
Accepted

Count number of bits to convert one integer to another integer

cmn_d = n1 & n2 all_d = n1 | n2 diff = all_d - cmn_d There is an operator for that: diff = n1 ^ n2 Having found the ...
Peter Taylor's user avatar
  • 24.1k
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
  • 56.5k
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 ...
dfhwze's user avatar
  • 13.9k
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
  • 62.1k
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
  • 75.7k
9 votes
Accepted

Hamming distance between numbers in JavaScript

A few things could be changed here, non of which I mention are optimisation however. Your solution is almost identical to the example on the wiki page where you can see hardware optimisations if ...
Craig Ayre's user avatar
8 votes

Calculating the 'binary gap' of a number

Another bit shifting solution that is a bit more general than @Xiaoy312's. It does not use a type cast and can therefore be implemented as generic function. Also, the trailing zero handling is simpler....
Rainer P.'s user avatar
  • 2,622
8 votes
Accepted

Test four int of commonality in the lower four bits

The code isn't bad, but it's a little more verbose than it needs to be. Consider that we don't really need to check one bit at a time; we can check four simultaneously. The key here is that we're ...
Edward's user avatar
  • 66.4k
8 votes
Accepted

Packing and unpacking bits

Bug The swap<T>() method isn't doing what you think it does, because the passed in T obj1, T obj2 aren't passed with the <...
Heslacher's user avatar
  • 50.4k

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