当前位置:   article > 正文

C#的几种位操作运算,与、或、非、异或、左移、右移_c# 与或非

c# 与或非

C#的常见几种位操作运算,与($)、或(|)、非(~)、异或(^)、左移(<<)、右移(>>)

位操作一般来说比加减乘除计算要快一些

与(&)操作符

与(&)操作符的位都为1时,才为1,其他都为0,因此与(&)操作符的结果范围在[0, Math.Min(x,y)],x,y均为正整数

或(|)操作符

或(|)操作符的位都为0时,才为0,其他都为1,因此或(|)操作符的结果范围在[Math.Max(x,y), x+y],x,y均为正整数

非(~)操作符

非(~)操作符按位取反,1转化为0, 0转化为1,因此非(~)操作符满足[x+~x=-1]一个数与其取反操作之和为-1

异或(^)操作符

异或(^)操作符的位不同时为1, 位相同时为0

左移(<<)操作符

左移(<<)操作符:【最左侧位不要,在最右侧补0】,相当于乘以2个N次方,【移位是除以32后的余数,范围[0,31],即以32为一个周期】

右移(>>)操作符

右移(>>)操作符:【最右侧位不要,在最左侧补符号位。(正数补0,负数补1)】,相当于除以2个N次方,【移位是除以32后的余数,范围[0,31],即以32为一个周期】

C#新建控制台应用程序BitOperationDemo。

相关测试程序如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace BitOperationDemo
  8. {
  9. class Program
  10. {
  11. /// <summary>
  12. /// 标识符字典
  13. /// </summary>
  14. static readonly Dictionary<OperationSymbol, string> dict = new Dictionary<OperationSymbol, string>()
  15. {
  16. { OperationSymbol.AND,"与(&)"},{ OperationSymbol.OR,"或(|)"},{ OperationSymbol.NOT,"非(~)"},{ OperationSymbol.XOR,"异或(^)"},
  17. { OperationSymbol.SHIFT_LEFT,"左移(<<)"},{ OperationSymbol.SHIFT_RIGHT,"右移(>>)"}
  18. };
  19. static void Main(string[] args)
  20. {
  21. Console.SetWindowSize(120, 50);
  22. Console.WriteLine($"与(&)操作符的位都为1时,才为1,其他都为0,因此与(&)操作符的结果范围在[0, Math.Min(x,y)],x,y均为正整数");
  23. Console.WriteLine($"或(|)操作符的位都为0时,才为0,其他都为1,因此或(|)操作符的结果范围在[Math.Max(x,y), x+y],x,y均为正整数");
  24. Console.WriteLine($"非(~)操作符按位取反,1转化为0, 0转化为1,因此非(~)操作符满足[x+~x=-1]一个数与其取反操作之和为-1");
  25. Console.WriteLine($"异或(^)操作符的位不同时为1, 位相同时为0");
  26. Console.WriteLine($"左移(<<)操作符:【最左侧位不要,在最右侧补0】,相当于乘以2个N次方,【移位是除以32后的余数,范围[0,31],即以32为一个周期】");
  27. Console.WriteLine($"右移(>>)操作符:【最右侧位不要,在最左侧补符号位。(正数补0,负数补1)】,相当于除以2个N次方,【移位是除以32后的余数,范围[0,31],即以32为一个周期】");
  28. Console.WriteLine();
  29. int x = 12345;
  30. int y = 34567;//34567除以32,余数为7,相当于移动7
  31. string binaryX = Convert.ToString(x, 2).PadLeft(32, '0');
  32. string binaryY = Convert.ToString(y, 2).PadLeft(32, '0');
  33. Console.WriteLine($"整数【{x}】对应的32位二进制为【{binaryX}】");
  34. Console.WriteLine($"整数【{y}】对应的32位二进制为【{binaryY}】");
  35. Console.WriteLine("-----------------下面分别用二进制字符串逻辑处理 以及 系统内置的操作符进行操作计算-----------------");
  36. PrintOperatorResult(x, y, binaryX, binaryY, OperationSymbol.AND);
  37. PrintOperatorResult(x, y, binaryX, binaryY, OperationSymbol.OR);
  38. PrintOperatorResult(x, y, binaryX, binaryY, OperationSymbol.NOT);
  39. PrintOperatorResult(x, y, binaryX, binaryY, OperationSymbol.XOR);
  40. PrintOperatorResult(x, y, binaryX, binaryY, OperationSymbol.SHIFT_LEFT);
  41. PrintOperatorResult(x, y, binaryX, binaryY, OperationSymbol.SHIFT_RIGHT);
  42. Console.WriteLine("-----------------下面测试负数进行操作计算-----------------");
  43. x = -12345;
  44. binaryX = Convert.ToString(x, 2).PadLeft(32, '0');
  45. PrintOperatorResult(x, y, binaryX, binaryY, OperationSymbol.AND);
  46. PrintOperatorResult(x, y, binaryX, binaryY, OperationSymbol.OR);
  47. PrintOperatorResult(x, y, binaryX, binaryY, OperationSymbol.NOT);
  48. PrintOperatorResult(x, y, binaryX, binaryY, OperationSymbol.XOR);
  49. PrintOperatorResult(x, y, binaryX, binaryY, OperationSymbol.SHIFT_LEFT);
  50. PrintOperatorResult(x, y, binaryX, binaryY, OperationSymbol.SHIFT_RIGHT);
  51. Console.ReadLine();
  52. }
  53. /// <summary>
  54. /// 打印结果比较
  55. /// </summary>
  56. /// <param name="x"></param>
  57. /// <param name="y"></param>
  58. /// <param name="binaryX"></param>
  59. /// <param name="binaryY"></param>
  60. /// <param name="operationSymbol"></param>
  61. static void PrintOperatorResult(int x, int y, string binaryX, string binaryY, OperationSymbol operationSymbol)
  62. {
  63. string binaryResult = GetOperatorResult(binaryX, binaryY, operationSymbol);
  64. int result = 0;
  65. switch (operationSymbol)
  66. {
  67. case OperationSymbol.AND:
  68. result = x & y;
  69. break;
  70. case OperationSymbol.OR:
  71. result = x | y;
  72. break;
  73. case OperationSymbol.NOT:
  74. result = ~x;
  75. break;
  76. case OperationSymbol.XOR:
  77. result = x ^ y;
  78. break;
  79. case OperationSymbol.SHIFT_LEFT:
  80. result = x << y;
  81. break;
  82. case OperationSymbol.SHIFT_RIGHT:
  83. result = x >> y;
  84. break;
  85. }
  86. Console.WriteLine($"{dict[operationSymbol]}操作符系统内置的操作符运算结果为【{result}】,使用二进制字符串还原后的整数结果为【{Convert.ToInt32(binaryResult, 2)}】");
  87. Console.WriteLine();
  88. }
  89. /// <summary>
  90. /// 获取操作符的结果
  91. /// </summary>
  92. /// <param name="binaryX">32位二进制字符串,第一个数</param>
  93. /// <param name="binaryY">32位二进制字符串,第二个数</param>
  94. /// <param name="operationSymbol">指定的操作符枚举</param>
  95. /// <returns></returns>
  96. static string GetOperatorResult(string binaryX, string binaryY, OperationSymbol operationSymbol)
  97. {
  98. string result = string.Empty;
  99. switch (operationSymbol)
  100. {
  101. case OperationSymbol.AND:
  102. result = CalculateOperatorResult(binaryX, binaryY, (left, right) => (left == '1' && right == '1') ? "1" : "0");
  103. break;
  104. case OperationSymbol.OR:
  105. result = CalculateOperatorResult(binaryX, binaryY, (left, right) => (left == '0' && right == '0') ? "0" : "1");
  106. break;
  107. case OperationSymbol.NOT:
  108. result = CalculateOperatorResult(binaryX, binaryY, (left, right) => (left == '0') ? "1" : "0");
  109. break;
  110. case OperationSymbol.XOR:
  111. result = CalculateOperatorResult(binaryX, binaryY, (left, right) => (left == right) ? "0" : "1");
  112. break;
  113. case OperationSymbol.SHIFT_LEFT:
  114. result = CalculateShiftResult(binaryX, Convert.ToInt32(binaryY, 2), true);
  115. break;
  116. case OperationSymbol.SHIFT_RIGHT:
  117. result = CalculateShiftResult(binaryX, Convert.ToInt32(binaryY, 2), false);
  118. break;
  119. }
  120. Console.WriteLine($"{dict[operationSymbol]}操作符逻辑运算的32位二进制结果为【{result}】");
  121. return result;
  122. }
  123. /// <summary>
  124. /// 计算逻辑运算的结果【与,或,非,异或】
  125. /// </summary>
  126. /// <param name="binaryX">32位二进制字符串,第一个数</param>
  127. /// <param name="binaryY">32位二进制字符串,第二个数</param>
  128. /// <param name="logicOperator">逻辑操作委托</param>
  129. /// <returns></returns>
  130. static string CalculateOperatorResult(string binaryX, string binaryY, Func<char, char, string> logicOperator)
  131. {
  132. string result = string.Empty;
  133. for (int i = 0; i < 32; i++)
  134. {
  135. result += logicOperator(binaryX[i], binaryY[i]);
  136. }
  137. return result;
  138. }
  139. /// <summary>
  140. /// 计算移位操作【左移,右移】
  141. /// </summary>
  142. /// <param name="binaryX"></param>
  143. /// <param name="shiftCount">移动几位,该数需要转化为【除以32的余数】</param>
  144. /// <param name="leftShift">true代表左移,false代表右移</param>
  145. /// <returns></returns>
  146. static string CalculateShiftResult(string binaryX, int shiftCount, bool leftShift)
  147. {
  148. //比如移动32位,就相当于移动0位,移动33位,就相当于移动1位【32位作为一个周期】
  149. shiftCount = (int)((uint)shiftCount % 32U);//确保余数在0~31之间波动
  150. if (leftShift)
  151. {
  152. //左移:【最左侧位不要,在最右侧补0
  153. return binaryX.Remove(0, shiftCount).PadRight(32, '0');
  154. }
  155. else
  156. {
  157. //右移:【最右侧位不要,在最左侧补符号位。(正数补0,负数补1)】
  158. return binaryX.Remove(32 - shiftCount, shiftCount).PadLeft(32, binaryX[0]);
  159. }
  160. }
  161. }
  162. /// <summary>
  163. /// 操作符号,运算符号
  164. /// </summary>
  165. enum OperationSymbol
  166. {
  167. /// <summary>
  168. /// 与 【位都是1时结果为1,否则为0
  169. /// </summary>
  170. AND = 0,
  171. /// <summary>
  172. /// 或 【位都是0时结果为0,否则为1
  173. /// </summary>
  174. OR = 1,
  175. /// <summary>
  176. /// 非 【位取反】,非操作只考虑第一个数
  177. /// </summary>
  178. NOT = 2,
  179. /// <summary>
  180. /// 异或 【位不同是为1,相同为0
  181. /// </summary>
  182. XOR = 3,
  183. /// <summary>
  184. /// 左移,相当于乘以2个N次方【最左侧位不要,在最右侧补0
  185. /// </summary>
  186. SHIFT_LEFT = 4,
  187. /// <summary>
  188. /// 右移,相当于除以2个N次方【最右侧位不要,在最左侧补符号位。(正数补0,负数补1)】
  189. /// </summary>
  190. SHIFT_RIGHT = 5
  191. }
  192. }

程序运行如图:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/112290
推荐阅读
相关标签
  

闽ICP备14008679号