4 votes 4 votes 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? Programming and DS go-mockgate-1 numerical-answers programming-in-c programming output + – Ruturaj Mohanty asked Dec 27, 2018 • recategorized Jan 7, 2020 Ruturaj Mohanty 1.8k views answer comment Share Follow See all 15 Comments See all 15 15 Comments reply Show 12 previous comments akash.dinkar12 commented Dec 31, 2018 reply Follow Share welcome @Magma 0 votes 0 votes srestha commented Apr 18, 2019 reply Follow Share @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? 0 votes 0 votes rsansiya111 commented Mar 7, 2022 reply Follow Share 729 0 votes 0 votes Please log in or register to add a comment.
7 votes 7 votes replace x as $x=\frac{(x \times 2^2)+(\frac{x}{2})}{2}$ (Only if when X is even) When X is odd right shifting makes division but with some loss of precision.So, in case of Odd data do bitwise calculation only. and code would work same. Ayush Upadhyaya answered Jan 1, 2019 • edited Jan 3, 2019 by Ayush Upadhyaya Ayush Upadhyaya comment Share Follow See all 6 Comments See all 6 6 Comments reply Show 3 previous comments jatin khachane 1 commented Jan 10, 2019 reply Follow Share @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, 0 votes 0 votes Ayush Upadhyaya commented Jan 10, 2019 reply Follow Share @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. 2 votes 2 votes jatin khachane 1 commented Jan 10, 2019 reply Follow Share ok that means x<<2 + x>>1 here x<<2 will be a temp result not assigned back to x yet but in x++ + x++ , first x++ may assign x as x+1 and second x++ may use it or may not both may use intital x ? 1 votes 1 votes Please log in or register to add a comment.