C Program to Multiply given Number by 4 using Bitwise Operators

«
»

This is a C Program to multiply given number by 4 using bitwise operators.

Problem Description

This C Program multiplies given number by 4 using bitwise operators.

Problem Solution

The bitwise operators are or, and, xor, not, left shift, right shift. Program uses left shift operator for this.

advertisement
Program/Source Code

Here is source code of the C program to multiply given number by 4 using bitwise operators. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C program to multiply given number by 4 using bitwise operators
 */
#include <stdio.h>
 
void main()
{
    long number, tempnum;
 
    printf("Enter an integer \n");
    scanf("%ld", &number);
    tempnum = number;
    /*  left shift by two bits */
    number = number << 2;
    printf("%ld x 4 = %ld\n", tempnum, number);
}
Program Explanation

In this C program, we are reading the integer using ‘number’ variable, and assign the value of ‘number’ variable to ‘tempnum’ variable. The bitwise operators are, or, and, xor, not, left shift, right shift. The program uses left shift operator to the value of ‘number’ variable by two bits.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
Runtime Test Cases
 
$ cc pgm62.c
$ a.out
Enter an integer
450
450 x 4 = 1800

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics of C, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.