Consider the following C language code:
#include<stdio.h> int main() { int x=64; int i=0; while (i++<3) x=(((x<<2)+(x>>1))>>1); printf("%d", x); return 0; }
What is the output of the above code?
welcome @Magma
@akash.dinkar12
In left shift, we add 0 , at right end
and in right shift, do we not add 1 in left end? or we add 0 at left end?
@Ayush Upadhyaya
in ... x = ( (x<<2) + (x>>1) ) >>1 ;
how it will figure that x<<2 should be executed first or x>>1 first
If x<<2 executed first then x>>1 will use updated value of x ??
correct me if wrong sir,
@jatin khachane 1-Actually I also had the same doubt.But right shift and left shift operators don't change the variable value untill it is reassigned to it.
Like x++ changes the variable value.
But if only you do x>>2 or x<<1, then untill this is assigned back to x, x won't change.
x<<2; //x won't change.
x=x<<2; //now x would change.
64.3k questions
77.9k answers
244k comments
80.0k users