C Program to multiply given number by 4 using bitwise operators

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

What is the bitwise operator?

In this C program, we have to think about the Bitwise Operators. They are used for the Operations at Bit-level. There are mainly six Bitwise Operators in C. Refer to the Bitwise Operators tutorial for more information about Bitwise operators. Types of Bitwise Operators are:

  1. Bitwise AND &
  2. Bitwise OR |
  3. Bitwise XOR ^
  4. Bitwise Complement ~
  5. Shift left <<
  6. Shift right >>

What is shift left(<<) bitwise operator?

The symbol of the shift left operator is '<< '. it is used to shift all the bits to the left by a certain number of positions. The vacated positions are filled with zeros. For example, consider a bitwise left shift operation:4<<2. Then the result will be 16. The binary equivalent of 4 is 0100. After the left shift, the number becomes 10000. So it is equivalent to 16.

The main logic of this c program is to accept a number from the user and save it into tempnum. Then use the left shift operator number=number<<2. Now the number is left-shifted to two bits, which means, we have done a multiplication with four on the variable. Then display the tempnum*4 = number.

ALGORITHM

STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Declare the variables number, tempnum as type Long integer.

STEP 3: Read the integer number from the user and save it into a number.

STEP 4: Assign tempnum = number.

STEP 5: number = number<<2.

STEP 6: Display the temnumber * 4 =number.

C Source Code

                                          #include <stdio.h>

void main() {
  long number, tempnum;
  printf("Enter an integer\n");
  scanf("%ld", & number);
  tempnum = number;
  number = number << 2; /*left shift by two bits*/
  printf("%ld x 4 = %ld\n", tempnum, number);
}
                                      

OUTPUT

Enter an integer
15
15 x 4 = 60

RUN2
Enter an integer
262
262 x 4 = 1048