#include <stdio.h>
int main()
{
int i = 1024;
for (; i; i >>= 1)
printf("Hi");
return 0;
}
Why does the for loop print Hi 11 times? I don't understand.
CodePudding user response:
The expression i >>= 1
is equivalent to i = i / 2
So if initially i
is equal to 1024
then the body of the loop will be executed for the values
1024 512 256 128 64 32 16 8 4 2 1
that is 11 times. The value of the expression 1 / 2
is equal to 0
due to the integer arithmetic.
You could check that by changing the call of printf like
#include <stdio.h>
int main( void )
{
int i = 1024;
for (; i; i >>= 1) printf( "%d ", i );
putchar( '\n' );
}
CodePudding user response:
Example of bitwise right shift operator:
int x = 1024
int z = x >>=1 # right shift operator
// then printing z will print 512, i.e. x/2
So, now if you keep on updating the value of z using a for loop as you are updating the value of 'i' in your example you will see that 'i' or 'z' will go as low as 1 (because 'i' is int type)
# include <stdio.h>
# include <iostream>
int main()
{
int i = 1024;
for (; i; i >>= 1)
std::cout << "\n" << i;
return 0;
}
In your code, because i = 1024 (2^10) can be divided maximum of 11 times by 2 hence the loop prints "Hi" 11 times where i = [1024, 512, ....2, 1].
Check this link for more details: https://en.cppreference.com/w/cpp/language/operator_assignment
CodePudding user response:
The >>=
operator shifts the bits of the binary number to the right by the value on its right side, and assigns the result back to the variable. Here i
is shifted to the right by 1
. The least significant (rightmost) bit is discarded.
The loop condition in the middle of the for
is just i
, which is equivalent to i != 0
due to non-zero values being considered "truthy" and zero "falsey" in C.
1024 is 0b10000000000
in binary, i.e., a 1 followed by 10 zeroes. It will take 11 shifts to the right until the 1 "falls off" and leaves only zeroes, which causes the loop condition to no longer be true.
(As noted in other answers, shifting to the right by one bit is equivalent to integer division by two since the value of each binary digit further to the left is indeed one power of two greater than the previous (ones, twos, fours, eights, etc.), just like in decimal the values are powers of ten (ones, tens, hundreds, etc.). However, this is not really relevant to the question, since we can remain in the world of bits, as described above. Likewise even though integer division discards the fractional part, that is not why 1 >> 1
is zero…)
CodePudding user response:
#include <stdio.h>
int main()
{
for (int i = 1024; i>0; i >>= 1) {
printf("%d ", i);
}
// proccess above as follows:
// 1024 (decimal) equal with 00000100 00000000 (binary)
//
// 1024 >>= 1 mean rotate 00000100 00000000 to right one bit (put 0 bit in left positin, values will be shift to right one bit each loop)
// 00000100 00000000 = 1024
// 00000010 00000000 = 512
// 00000001 00000000 = 256
// 00000000 10000000 = 128
// 00000000 01000000 = 64
// 00000000 00100000 = 32
// 00000000 00010000 = 16
// 00000000 00001000 = 8
// 00000000 00000100 = 4
// 00000000 00000010 = 2
// 00000000 00000001 = 1
}