目录
1.实例目标
了解与/或/异或/左移/右移等位运算
2.编程思路
C#提供了与/或/异或/求补/左移/右移等操作,可重载
3.编程步骤
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PRO15
{
//位运算
class Program
{
public static void Main()
{
int varA = 10;//二进制为 00001010
int varB = 20;//二进制为00010100
//与运算
int andResult = varA & varB;
Console.WriteLine("10 & 20 = {0}", andResult);
//或运算
int orResult = varA | varB;
Console.WriteLine("10|20 = {0}", orResult);
//异或运算
int notorReault = varA ^ varB;
Console.WriteLine("10^20={0}", notorReault);
//求补运算
Console.WriteLine("~{0:x8}={1:x8}", varA, ~varA);
//按位右移
Console.WriteLine("{0:x8}>>3={1}", varA, varA >> 3);
//按位左移
Console.WriteLine("{0:x8}<<3={1}", varA, varA << 3);
Console.ReadKey();
}
}
}