0
Hey , Can anyone help me in explaining this logic
int n = 2048; while(n){ printf ("sololearn"); n>>=1; }
2 Antworten
+ 3
the operator >> 1 means shift right one place,
the number 2048 = 100000000000 in binary, shift right pushes it to the right each time a 0 goes off and you get another number it's like dividing by 2.
1 0 0 0 0 0 0 0 0 0 0 0
2048 1024 512 256 128 64 32 16 8 4 2 1
first iteration for example :
100000000000 shift right 10000000000|¦0
last zero is gone you get 010000000000
which is 1024.
second iteration 1000000000||0
00100000000 = 512
since there are 12 places you get "sololearn"
12 times.
the loop exits when 1 is shifted right
to 0.
000000000000
+ 1
Great👍
Thankyou for helping 😊