Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Ankith Reddy

A number can be multiplied by 2 using bitwise operators. This is done by using the left shift operator and shifting the bits left by 1. This results in double the previous number.

A program that demonstrates multiplication of a number by 2 using bitwise operators is given as follows.

Example

Live Demo

using System;
namespace BitwiseDemo {
   class Example {
      static void Main(string[] args) {
         int num = 25, result;
         result = num << 1;
         Console.WriteLine("The original number is: {0}", num);
         Console.WriteLine("The number multiplied by two is: {0}", result);
      }
   }
}

The output of the above program is as follows.

The original number is: 25
The number multiplied by two is: 50

Now let us understand the above program.

First, the number is defined. Then, the left shift operator is used and the bits in num are shifted left by 1. This results in double the previous number which is stored in result. Then, the values of num and result are displayed. The code snippet for this is given as follows −

int num = 25, result;
result = num << 1;
Console.WriteLine("The original number is: {0}", num);
Console.WriteLine("The number multiplied by two is: {0}", result);

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.