loading...

re: What The For Loop? VIEW POST

FULL DISCUSSION
 
if(i & 1)
{
    // ODD
}
else
{
    // EVEN
}

I much prefer bit checks for that sort of thing, and I think it is valuable for beginners to at least know of this option because it opens the door to the concept of bit oriented shortcuts (there are a few worth knowing). Just wanted to add my two cents. But overall really good post!

 

Why do you much prefer bit hacking for that sort of thing? Is there a performance benefit or something?

For 99.9% of applications the modulus operator would be a better choice because people understand modulus immediately, and the performance trade-off (if there is one) is negligible compared to the benefit of immediate readability. There's a good chance most people will look at your bit hack and have to Google it or at least pause and think about it for a minute to understand.

That being said, it is a neat trick!

 

💯

In general, I would say optimize for developer time over processor time. Developer time is so much more expensive these days... 😂

 

Yes, from what I understand the performance is better, though I think in the vast majority of cases it wouldn't matter. Here is a fun read about a real life problem solved with bitwise operations (not simple even odd stuff though...). I primarily mentioned it for the reasons I already stated, it is probably the simplest intro to bitwise operations that I know of, which opens the door for learning more about the language for a beginner. Also, if you have a "use a for loop to find all even odd values" interview problem, and you whip out that solution... Just sayin'

As for the real world webdev readability, I never really considered the &1 thing to be less readable than %2. In my case I had to google both the first time I saw them. AND I'll go as far to say, I have a hard time thinking outside of myself. What I mean by that, is I love seeing new stuff in code and learning what it is/does, so it is hard for me to think in terms of that bothering people (perhaps a weakness on my part). But I do understand the value of human readability over being clever (you can always just minify your code to make it smaller afterwards). But to me both the bitwise and modulus are an abstract symbol followed by a number, and you kinda have to learn what they do when you see them, just part of coding (a part that I enjoy).

But for the most part, I just threw it in the comments because it was a post for beginners and I thought that the target audience might enjoy it. Though I'm glad you commented on the immediate readability aspect, that is worth considering.

If you're talking optimization, having the if statement (branching) is the most expensive thing, because it requires the processor to dump and reload its entire register state, and kills off the ability for it to pipeline nicely. Getting rid of the conditional is by far the most optimal solution to this code.

 

I assumed that a modern compiler would take care of this anyway. So I wrote a meaningless test program in C:

#include <stdio.h>

void main() {
    int i = 3;

    if(i % 2 == 0) {
      printf("Odd\n");
    } else {
      printf("Even\n");
    }
}

I then compiled it with gcc test.c -o test and used gdb to disassemble it. And indeed, the modulo operation got replaced with a bitwise and (that's ARM assembly but I'd assume it's the same on other architectures):

and r3, r3, #1
cmp r3, #0

So definitely optimize for readability (whatever that means for you) and let your tools deal with these micro-optimizations.

oh that's cool to see, thanks for sharing that.
I found this reply post in a stackoverflow thread on the matter

Performance. jsperf.com/modulo-vs-bitwise/8 Bitwise is faster across most browsers (or, it was at the time the answer was posted, in recent Chrome's there is not much difference tbf – danwellman Jan 15 at 9:05

Sure, the example I showed uses native code, not JS. Though as the post you quoted indicates it seems JS engines are always getting smarter too, and the most recent version of the same test shows the following for newer Chrome versions:

imgur.com/a/QcxZigt

Blue is bitwise and red is modulo, which means in 2 versions there's no speed difference at all and in the 3rd modulo was actually faster.

yeah, well, I posted it to support what you were showing, not as an argument. It was more just meant to be extra information than anything else.

Oh, sorry if there was any misunderstanding, I never construed this as an argument, I just added this since the quote in your comment said "in recent Chrome's there is not much difference tbf" and I had nothing better to do so I looked at the newest version of the benchmark.

Code of Conduct Report abuse