Performance wise, how fast are Bitwise Operators vs. Normal Modulus?

前端 未结 8 1819
无人及你
无人及你 2020-12-01 04:15

Does using bitwise operations in normal flow or conditional statements like for, if, and so on increase overall performance and would it be better

8条回答
  •  温柔的废话
    2020-12-01 05:00

    Bitwise operations are much faster. This is why the compiler will use bitwise operations for you. Actually, I think it will be faster to implement it as:

    ~i & 1
    

    Similarly, if you look at the assembly code your compiler generates, you may see things like x ^= x instead of x=0. But (I hope) you are not going to use this in your C++ code.

    In summary, do yourself, and whoever will need to maintain your code, a favor. Make your code readable, and let the compiler do these micro optimizations. It will do it better.

提交回复
热议问题