I have a piece of code in C with the following:
a = b & ((1<<24) - 1);
If I am not mistaking, this is equivalent to:
a = b & 0xFFFFFF;
What is the benefit in terms of performance to write the first one? For me it is more complicated to read, but I suppose the guy who wrote that had a better C background than I have.
Thanks
In all likelihood, there isn’t any performance difference since the compiler will figure out that
((1<<24) - 1)is a constant expression, and will evaluate it at compile time.We can only speculate about why the original author of the code chose to write it the way they did. Perhaps they thought it better expressed the intent (“mask out all but the 24 least significant bits of
b“).If that was their reasoning, I personally would tend to agree with them.