C Program#161 – To divide and multiply a number using bitwise shift operators.

Program to divide and multiply a number using bitwise shift operators.:

Program Logic :

  • Shift Operators (Left [<<] & Right [>>] Bitwise) used to move bitsof a number to left or right.
  • The vacant positions are always filled by zeros (0) by default.
  • Shifting of bits are used for multiplication and division of a number by powers of two. (multiples of 2).
  • Means, if we shift’s 1 bit of a number from right then, it results in the Division by 2, by 2 bits shift division by 4 and so on…
  • Example : Suppose x = 10 which is 0000 1010 in binary. If we shifts right by 1 using x>>1 results in 0000 0101 which is 5 (division by 2).
  • Similarly, if we shift’s 1 bit of a number from left then, it results in the Multiplication by 2, by 2 bits shift multiliplication by 4 and so on…
  • Example : Suppose x = 10 which is 0000 1010 in binary. If we shifts left by 1 using x<<1 results in 0010 100 which is 20 (multiply by 2).
  • Still not understood ? See the proper working example in action below.




Full Well Commented, Self-Explanatory Example Program of the same given below.

Below Here are Output of the program.



Output of the Program :