
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to multiply a given number by 2 using Bitwise Operators in C#?
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
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); } } }
Output
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);
- Related Articles
- Multiply a number by 15 without using * and / operators in C++
- Multiply any Number with using Bitwise Operator in C++
- Multiply the given number by 2 such that it is divisible by 10
- Bitwise Operators in C
- Bitwise Operators in C++
- C++ Program to Perform Addition Operation Using Bitwise Operators
- Python Bitwise Operators
- Java Bitwise Operators
- Perl Bitwise Operators
- Bitwise operators in Dart Programming
- How to use Java's bitwise operators in Kotlin?
- What are bitwise operators in C#?
- Bitwise right shift operators in C#
- Explain about bitwise operators in JavaScript?
- How to multiply all values in a list by a number in R?

Advertisements