如何在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);热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短