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

Here 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 executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Multiply given Number by 4 using Bitwise Operators
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int number, tempnum;
            Console.WriteLine("Enter an integer :");
            number = int.Parse(Console.ReadLine());
            tempnum = number;
            number = number << 2;
            Console.WriteLine("{0},{1}", tempnum, number);
            Console.ReadLine();
        }
    }
}
Program Explanation

In this C# program, we are reading an integer value using ‘number’ variable. Binary Left Shift Operator is used to move the value of left operands by the number of bits specified by the value of right operand. Print the multiplied value of the given number.

advertisement
Runtime Test Cases
 
Enter an integer :
120
120,480

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn