Open In App

Algorithms | Bit Algorithms | Question 2

What does the following C expression do? x = (x<<1) + x + (x>>1);

(A)



Multiplies an integer with 7

(B)



Multiplies an integer with 3.5

(C)

Multiplies an integer with 3

(D)

Multiplies an integer with 8


Answer: (B)
Explanation:

In the expression: x = (x<<1) + x + (x>>1);
(x<< 1) means left shift x by 1, by doing this we get, x*2.
(x>>1) means right shift x by 1, by doing this we get, x/2

After solving the above expression, we get
=x*2 + x + x/2 
= 3*x +x/2
=7x/2
=3.5*x,  Hence x is a multiple of 3.5
The expression multiplies an integer with 3.5.  See for more details: https://www.geeksforgeeks.org/multiply-an-integer-with-3-5/amp/

Hence Option(B) is correct.

Quiz of this Question
Please comment below if you find anything wrong in the above post

Article Tags :