如何在C#中使用按位运算符将给定数乘以2?
可以使用按位运算符将数字乘以2。这是通过使用左移位运算符并左移1位来完成的。这将导致前一个数字加倍。
给出了一个演示使用位运算符将数字乘以2的程序,如下所示。
示例
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 original number is: 25 The number multiplied by two is: 50
现在让我们了解上面的程序。
首先,定义数字。然后,使用左移运算符,将num中的位左移1。这将导致存储在结果中的前一个数字加倍。然后,显示num和result的值。为此的代码片段如下-
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);热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练