Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

Hot answers tagged

121 votes

Efficiently checking if a number is divisible by 3

Computers aren't humans. A human might find it easiest to apply a divisibility rule. A computer, though, doesn't care. The simplest code would be to check ...
user avatar
60 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, ...
user avatar
  • 4,429
41 votes

Efficiently checking if a number is divisible by 3

Division instructions are often slow, because they have to do a conditional subtract once for each bit of the quotient. It's possible to write code which may perform better than this, but a loop ...
user avatar
  • 1,188
33 votes

Efficiently checking if a number is divisible by 3

I guess, a really fast solution on a modern CPU looks like this: ...
user avatar
  • 13.4k
19 votes

Efficiently checking if a number is divisible by 3

Given how fast it should be I'd tend toward advocating a brute-force test, something like: ...
user avatar
  • 33.2k
19 votes
Accepted

Converting an IP

I wouldn't both prefixing 'internal' functions with an underscore. Instead, just put this in a module, and only export the functions you want to export. If you care about following the node style ...
user avatar
  • 10.5k
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, <...
user avatar
  • 4,429
18 votes

Fastest way to clamp an integer to the range 0-255

Conclusion 2011-12-05: I tried all of the suggestions again with VS 2010 Express. The generated code didn't change much, but the register assignments did which affected the overall results. A slight ...
user avatar
  • 1,796
17 votes

Efficiently checking if a number is divisible by 3

As your original implementation doesn't produce the correct results for 21, 42, 69, 81, 84, 87, and 93 (that's just from 0 to 100 !), I wanted to provide a more "common" way to handle the question of ...
user avatar
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: ...
user avatar
  • 17.1k
16 votes

Efficiently checking if a number is divisible by 3

I ran some benchmarks. I included @JavaDeveloper's original code for comparison, even though it produces erroneous results. ...
16 votes

String reversal, capitalize vowels, lowercase consonants

We have a namespace called StringOperation, a class called Program, and methods called Main, ...
user avatar
  • 107k
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. ...
user avatar
  • 3,106
16 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 ...
user avatar
  • 3,493
15 votes

Diamond in the rough! Finding Gems in the rocks

You have added public GemCounter() { // default constructor. Does nothing. } which is not needed for the program to work because, The compiler ...
user avatar
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 ...
user avatar
  • 64.1k
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. ...
user avatar
  • 3,106
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 ...
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 ...
user avatar
  • 28.4k
14 votes
Accepted

Calculating int next power of two

Java has Integer.highestOneBit that can make this a bit simpler: ...
user avatar
  • 107k
13 votes
Accepted

Setting and getting bits in C

I do, however insist on making everything explicit. I'm not a huge fan of implicit anything... Maybe that's something I need to get over? No, that is very good practice and a good habit. Particularly,...
user avatar
  • 4,415
13 votes

Why are these functions slower than BigInteger's included methods?

Your question comes in multiple parts: General code review, adding algorithms, and then performance relative to standard BigInteger General Review Let's focus on ...
user avatar
  • 96.7k
13 votes
Accepted

Validating UTF-8 byte array

Logic Never omit the optional braces like that. Think of yourself as a contributing factor to a future coding accident. If you really want to omit braces, then put the statement on the same line, ...
user avatar
13 votes
Accepted

Diamond in the rough! Finding Gems in the rocks

Not much to say. Logical operators used a bit inconsistently: gotElement |= 1 << element; gemsSoFar = gemsSoFar & gotElement; better use <...
user avatar
  • 53.1k
13 votes
Accepted

Gray codes addition

Loop invariants code motion This is the name of an optimization performed by most compilers: when they detect code that actually does not depend on the state of the loop, they move it out of the loop....
user avatar
  • 19.8k
13 votes

Reversed Binary Numbers

Division and multiplication are relatively computationally costly operations compared to shifts and AND operations. Try to reformulate your answer in terms of <...
user avatar
  • 64.1k
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 ...
user avatar
12 votes

Efficiently checking if a number is divisible by 3

In the spirit of premature optimisation, here's a fast method based on modular arithmetic: ...
user avatar
  • 271
12 votes

Converting an IP

Your bitwise operations are fine, though the >>> right-shifts are unnecessary. @Lucien and @Corbin are right! The right-shift operators prevent ...
user avatar
12 votes

Diamond in the rough! Finding Gems in the rocks

It's not a bad solution. It's not immediately obvious what processRock() function does, so I would just make some minor adjustments in the way the problem is ...
user avatar

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