Skip to main content
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's user avatar
  • 4,557
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's user avatar
  • 4,557
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,580
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 int next power of two

Java has Integer.highestOneBit that can make this a bit simpler: ...
janos's user avatar
  • 112k
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.6k
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.6k
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.4k
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
  • 112k
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,265
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

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's user avatar
  • 4,557
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.7k
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.2k
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
  • 57.3k
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
  • 65.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
  • 81.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
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
  • 10.7k
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
  • 10.7k
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: ...
200_success's user avatar
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. ...
Der Kommissar's user avatar
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: ...
David Wohlferd's user avatar
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
  • 65.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
  • 23.5k
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
  • 81.7k

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