如何在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 幼儿节日祝福语大全简短
18 高考毕业祝福语简短励志