Then why do people use the version (1 <7)?
This is a form of documentation, this is not a magic number, but 2^7 (from two to seventh degrees), which makes sense for someone who wrote the code. The modern optimizing compiler should generate the same code for both examples, so there is no need to use this form, and there is the advantage of adding context.
Using godbolt , we can make sure that this is true, at least for several versions of gcc , clang and icc . Using a simple example with side effects to make sure the code is not fully optimized:
#include <stdio.h> void forLoopShift() { for(int i = 0; i < (1 << 7); i++) { printf("%d ", i ) ; } } void forLoopNoShift() { for(int i = 0; i < 128; i++) { printf("%d ", i ) ; } }
For the relevant part of the code, we see that they generate the following to see it live :
cmpl $128, %ebx
What we have is an integer constant expression defined in the draft standard section C11 6.6 Constant expressions that state:
An integer constant expression117) must have an integer type and have only operands that are integer constants, enumeration constants, symbolic constants, sizeof expressions, the results of which are integer constants, [...]
and
Constant expressions must not contain assignment, increment, decrement, function call, or commas, except when they are contained in a subexpression that is not evaluated. 115)
and we can see that the constant expression is allowed to evaluate during translation:
A constant expression can be evaluated at translation time and not at run time, and accordingly, can be used anywhere where a constant can be.