# 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 bits**of 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.**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
/*C Program#161 - To divide and multiply a number using bitwise shift operators. Using Bitwise Shift Operators - 1. Shifting 1 bit to the left results in multiplication by 2. 2. Shifting 1 bit to the right results in divide by 2.(ignoring remainder) 3. Division and Multiply occurs in multiples of 2 |Source : www.kshitijdivakar.com | Editor : Code::Blocks 16.01 mingW (Windows)*/ #include<stdio.h> int main() { int number,result; printf("\n\n Enter Number : "); scanf("%d",&number); result=number>>1; //divide by two printf("\n\n Right Shift << by 1 : %d",result); //Shifting 1 bit to the right = number will be divided by 2 //Remainder is ignored during division result=number<<1; //multiply by two printf("\n\n Left Shift >> by 1 : %d",result); //Shifting 1 bit left = number will be multiplied by 2 result=number>>2; //divide by four printf("\n\n Right Shift << by 2 : %d",result); //Shifting 2 bit to the right = number will be divided by 4 //Remainder is ignored during division result=number<<2; //multiply by four printf("\n\n Left Shift >> by 2 : %d",result); //Shifting 2 bit left = number will be multiplied by 4 result=number>>3; //divide by six printf("\n\n Right Shift >> by 3 : %d",result); //Shifting 3 bit to the right = number will be divided by 6 //Remainder is ignored during division result=number<<3; //multiply by six printf("\n\n Left Shift << by 3 : %d",result); //Shifting 3 bit left = number will be multiplied by 6 printf("\n\n"); return 0; } |

**Below Here are Output of the program.**

**Output of the Program :**