I am wondering if anyone here knows what the termination condition in the following for loop is supposed to mean.
for (int i = 0; i < 1 << Level; i++) {
...
}
<<
shifts the bits of the first operand n
times to the left, where n
is the second operand.
Therefore 1 << Level
shifts the single 1
bit of the number 1
Level
times to the left, which is equivalent to calculating 2 ^ Level.
So i < 1 << Level
is equivalent to i < Math.pow(2,Level)
.
Simply stated
for (int i = 0; i < 1 << Level; i++) {
...
}
is equal to
for (int i = 0; i < Math.pow(2,Level); i++) {
...
}
So the for loop will run for "Math.pow(2,Level)" times since you are counting from 0 to Math.pow(2,Level)-1.
if Level = 2 then the loop is
for(int i =0;i<4;i++){}
if Level = 3 then the loop is
for(int i =0;i<8;i++){}
if Level = 5 then the loop is
for(int i =0;i<32;i++){}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With