c++ - Checking if number is even by looking at the last bit - are there any other "tricks" like this one? -


I recently found out that if I need to see if the variable is also (or weird), then I You could see that the last bit of the variable is equal to 0. This search is being implemented, replacing some modulo 2 calculations and so on, the whole function runs faster.

Is there any other "trick" like this, where working with bits can replace other calculations, which can lead to better performance execution time?

I suspect that the use of modulo-2 calculation of the speed of execution time by equal bitwise action be produced. The value of any C ++ compiler will be equal to its salt, n% 2 and n & amp; 1 for similar machine instructions.

Beware of using bit-tilling hacks as optimization. First of all, it is not always clear that the work you are customizing is an obstacle, secondly, as a result, the code is usually more likely to be wrong and there are more subtle worms. What does it mean in the famous quote Nunth, "We should forget about the lesser capacity, 97% of the time say: Adaptation of time is the root of all evils." Save your effort.

If you actually pursue this topic, then there is a good list of interesting hacks.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -