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, ...
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, <...
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 ...
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:
...
16
votes
Accepted
Calculating int next power of two
Java has Integer.highestOneBit that can make this a bit simpler:
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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
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", ...
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, ...
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, ...
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 ...
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 ...
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
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 ...
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 ...
8
votes
UTF-8 Validation
Bugs
I'm surprised that the code was accepted by the online judge. This part of the code makes no sense to me:
...
8
votes
Optimizing special cases of modulo
I started writing this as a comment, but as I was exploring things got worse and worse.
Your benchmarks (in the comments) do not at all line up with my experience. In fact, they're just the opposite.
...
8
votes
Accepted
Fast bitswap in assembly
I'm still a bit fuzzy on how to compute latency, but using iaca v2.2, it reports your code 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 (<...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
bitwise × 340c++ × 79
c × 73
performance × 59
java × 51
algorithm × 48
python × 45
integer × 42
c# × 36
programming-challenge × 24
javascript × 19
beginner × 17
bitset × 15
python-3.x × 13
array × 13
c++11 × 12
assembly × 11
serialization × 11
comparative-review × 9
strings × 7
.net × 7
image × 7
python-2.x × 6
time-limit-exceeded × 6
interview-questions × 6