News for hackers. Stuff that matters.
Re:Low Level Bit Hacks You Absolutely Must Know by ladberg on Friday May 11, 2018 @01:02AM (#17045067)
What did they usually say? I know the bitwise trick, but I would say "x % 2 == 0" if asked because it makes a lot more sense and will (almost) always compile to the exact same code.
Re:Low Level Bit Hacks You Absolutely Must Know by carlmr on Friday May 11, 2018 @04:23AM (#17045731)
Exactly, readability by other engineers + compiler optimizations is usually better than some bithacking that may or may not help in terms of speed and definitely doesn't help with understanding. (In this case maybe simple enough, but still).
Re:Low Level Bit Hacks You Absolutely Must Know by mbrumlow on Friday May 11, 2018 @09:22AM (#17047101)
I am not trying to start a argument but it is a bit sad that these things are called "hacks", in the article, and by others.

Some would call it math.

But I guess that shows you the state of our art.

Re:Low Level Bit Hacks You Absolutely Must Know by blackflame7000 on Friday May 11, 2018 @08:48PM (#17051805)
We live in the era were you can get a Billion Bytes for a dollar. We also live in an era where finding someone who can manipulate bits to improve performance is either 1)A Micro-Op 2) Extremely fringe work, or 3) prohibitively expensive to be worth it.
Re:Low Level Bit Hacks You Absolutely Must Know by carlmr on Monday May 14, 2018 @02:18AM (#17063143)
Don't get me wrong. I find most of these "bit hacks" to be quite simple individually. But they add a layer of necessary comprehension.

In most cases you won't see these alone. They will be surrounded by tens of thousands of other lines of code. And where there is one "hack" there are usually others. And at some point you end up with a bug somewhere because one of these smart little hacks is failing. You don't know which one.

This does create unnecessary problems for maintenance in the future. So use it, but use it only when absolutely necessary. Not because you think the code will run faster.

It's also important to note that many things people think make the code faster, don't. Like vector vs. linked list access. Intuitively the linked list seems much simpler for inserting an element. But in almost all cases the vector is faster, because of cache locality.

So what I'm saying is. Do things that make the code probably faster like using a vector, if they don't cloud understanding. If they do, leave them out until you have a reason to use them. The only reason to use micro-optimizations like these is if you measured execution time and know there is a performance bug in this place in the code which your "hack" could resolve.

No measurement -> no optimization. Because the performance problems are almost always in different places than the average developer assumes.

And then add a comment explaining what you did to a 5 year old.

Re:Low Level Bit Hacks You Absolutely Must Know by DoofusOfDeath on Friday May 11, 2018 @07:19AM (#17046358)
> I know the bitwise trick

Only if you make certain assumptions about how the integer is represented, I reckon.

Re:Low Level Bit Hacks You Absolutely Must Know by projektfu on Friday May 11, 2018 @08:24AM (#17046729)
Indeed, a ones-complement negative number would not have this bit property.
Re:Low Level Bit Hacks You Absolutely Must Know by Hello71 on Friday May 11, 2018 @08:41AM (#17046858)
In fact, newer gcc versions are unable to optimize (x & 1) == 0 at level -O1, and require -O2. I assume that the code emitted at -O2 and -O3 for both constructs is the more efficient version than the literal translation, even though they are the same number of instructions.
Re:Low Level Bit Hacks You Absolutely Must Know by vardump on Friday May 11, 2018 @02:08PM (#17049546)
Apparently not true, at least in a simple function.

https://godbolt.org/g/hfpHz7

  int isEven(int number)
  {
      return number & 1;
  }
gcc 7.3, -O1, -O2 and -O3 (identical codegen for all optimization levels):

  isEven(int):
    mov eax, edi
    and eax, 1
    ret
Re:Low Level Bit Hacks You Absolutely Must Know by Hello71 on Friday May 11, 2018 @06:31PM (#17051194)
that function checks if a number is odd, not even.

https://godbolt.org/g/7Khcg9

Re:Low Level Bit Hacks You Absolutely Must Know by vardump on Friday May 11, 2018 @07:31PM (#17051454)
My bad. Didn't even check it at any point.

Anyways, you still prove my point. All those three are pretty close.

-O1 sete instruction is very slightly unoptimal, but even it is just a fractional clock cycle.

Re:Low Level Bit Hacks You Absolutely Must Know by BearOso on Friday May 11, 2018 @01:33PM (#17049257)
I highly doubt it. That’s 1:1

  test eax, 1
  ; use result
Re:Low Level Bit Hacks You Absolutely Must Know by Hello71 on Friday May 11, 2018 @06:33PM (#17051203)
gcc -O2, clang, and icc all produce the sequence:

    is_even(int):
      mov eax, edi
      not eax
      and eax, 1
      ret
I am far from a x86 expert, but I assume this version avoids contention on the flags registers and increases pipelining.

edit: my mistake, icc and clang produce the equivalent sequence "not edi; and edi, 1; mov eax, edi; ret".

Re:Low Level Bit Hacks You Absolutely Must Know by throwaway2016a on Friday May 11, 2018 @08:42AM (#17046864)
This is a question I give people who were already hired out of curiosity and I explicitly say not to use modulus. I ask a similar question in interviews but don't exclude modulus and most answer using modulus.
Re:Low Level Bit Hacks You Absolutely Must Know by naasking on Friday May 11, 2018 @09:18AM (#17047075)
Well, you need to use modulus if you want to be 100% portable.
Re:Low Level Bit Hacks You Absolutely Must Know by dmkolobov on Friday May 11, 2018 @04:27PM (#17050575)
always been fond of `even x = x == ((x >> 1) << 1)`