在C#中可以对整型运算对象按位进行逻辑运算。按位进行逻辑运算的意义是:依次取被运算对象的每个位,进行逻辑运算,每个位的逻辑运算结果是结果值的每个位。C#支持的位逻辑运算符如下表所示。
<!-- /* Font Definitions */ @font-face {font-family:"MS 明朝"; panose-1:2 2 6 9 4 2 5 8 3 4; mso-font-alt:"MS Mincho"; mso-font-charset:128; mso-generic-font-family:roman; mso-font-pitch:fixed; mso-font-signature:-536870145 1791491579 18 0 131231 0;} @font-face {font-family:SimSun; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:宋体; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:Century; panose-1:2 4 6 4 5 5 5 2 3 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-pitch:variable; mso-font-signature:647 0 0 0 159 0;} @font-face {font-family:"MS Pゴシック"; panose-1:2 11 6 0 7 2 5 8 2 4; mso-font-charset:128; mso-generic-font-family:modern; mso-font-pitch:variable; mso-font-signature:-536870145 1791491579 18 0 131231 0;} @font-face {font-family:"/@MS Pゴシック"; panose-1:2 11 6 0 7 2 5 8 2 4; mso-font-charset:128; mso-generic-font-family:modern; mso-font-pitch:variable; mso-font-signature:-536870145 1791491579 18 0 131231 0;} @font-face {font-family:"/@MS 明朝"; panose-1:2 2 6 9 4 2 5 8 3 4; mso-font-charset:128; mso-generic-font-family:roman; mso-font-pitch:fixed; mso-font-signature:-536870145 1791491579 18 0 131231 0;} @font-face {font-family:"/@SimSun"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0mm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:Century; mso-fareast-font-family:"MS 明朝"; mso-bidi-font-family:"Times New Roman"; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:99.25pt 30.0mm 30.0mm 30.0mm; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:563226557; mso-list-template-ids:2085121002;} ol {margin-bottom:0mm;} ul {margin-bottom:0mm;} -->
运算符号 | 意义 | 运算对象类型 | 运算结果类型 | 对象数 | 实例 |
~ | 位逻辑非运算 | 整型,字符型 | 整型 | 1 | ~a |
& | 位逻辑与运算 | 2 | a & b | ||
| | 位逻辑或运算 | 2 | a | b | ||
^ | 位逻辑异或运算 | 2 | a ^ b | ||
<< | 位左移运算 | 2 | a<<4 | ||
>> | 位右移运算 | 2 | a>>2 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace 集合
- {
- class 位运算
- {
- public static void Main()
- {
- //1、位逻辑非运算
- //位逻辑非运算是单目的,只有一个运算对象。位逻辑非运算按位对运算对象的值进行非运算,即:如果某一位等于0,就将其转变为1;如果某一位等于1,就将其转变为0。
- //比如,对二进制的10010001进行位逻辑非运算,结果等于01101110,用十进制表示就是:
- //~145等于110;对二进制的01010101进行位逻辑非运算,结果等于10101010。用十进制表示就是~85等于170。
- //下面我们是用的有符号类型sbyte,结果就是-86
- sbyte num1 = 85 ;
- sbyte renum = (sbyte)~num1 ;
- Console.WriteLine( "85位非运算结果:{0}" , renum ) ;
- //如果我们用无符号类型来存储,结果就是170
- byte num2 = 85 ;
- byte renum2 = (byte)~num2 ;
- Console.WriteLine( "85位非运算结果:{0}" , renum2 ) ;
- //2、位逻辑与运算
- //位逻辑与运算将两个运算对象按位进行与运算。与运算的规则:1与1等于1,1与0等于0 ,0与0等于0 。
- //比如:10010001(二进制)&11110000等于10010000(二进制)。
- // 99的二进制表示: 01100011
- // 85的二进制表示: 01010101
- // 与运算的结果 : 01000001 ( 十进制:65)
- int num3 = 85 ;
- int num4 = 99 ;
- int renum3 = num3 & num4 ;
- Console.WriteLine( "85 & 99 位与运算结果:{0}" , renum3 ) ;
- //3、位逻辑或运算
- //位逻辑或运算将两个运算对象按位进行或运算。或运算的规则是:1或1等1,1或0等于1,
- //0或0等于0。比如10010001(二进制)| 11110000(二进制)等于11110001(二进制)。
- // 99的二进制表示: 01100011
- // 85的二进制表示: 01010101
- // 运算的结果 : 01110111 ( 十进制:119)
- int num5 = 85 ;
- int num6 = 99 ;
- int renum4 = num5 | num6 ;
- Console.WriteLine( "85 | 99 位或的结果:{0}" , renum4 ) ;
- //4、位逻辑异或运算
- //位逻辑异或运算将两个运算对象按位进行异或运算。异或运算的规则是:1异或1等于0,
- //1异或0等于1,0异或0等于0。即:相同得0,相异得1。
- //比如:10010001(二进制)^11110000(二进制)等于01100001(二进制)。
- // 99的二进制表示: 01100011
- // 85的二进制表示: 01010101
- // 运算的结果 : 00110110 ( 十进制:54)
- int num7 = 85 ;
- int num8 = 99 ;
- int renum5 = num7 ^ num8 ;
- Console.WriteLine( "85 ^ 99 位异或的结果:{0}" , renum5 ) ;
- //5、位左移运算
- //位左移运算将整个数按位左移若干位,左移后空出的部分0。比如:8位的byte型变量
- //byte a=0x65(即二进制的01100101),将其左移3位:a<<3的结果是0x27(即二进制的00101000)。
- // 85的二进制表示: 01010101
- // 左移3位后变成了: 01010101000
- int num9 = 85 ;
- int rein6 = num9 << 3 ;
- Console.WriteLine( "将85左移3位后的结果:{0}" , rein6 ) ;
- //6、位右移运算
- //位右移运算将整个数按位右移若干位,右移后空出的部分填0。比如:8位的byte型变量
- //Byte a=0x65(既(二进制的01100101))将其右移3位:a>>3的结果是0x0c(二进制00001100)。
- // 85的二进制表示: 01010101
- // 右移3位后变成了:00001010
- byte num10 = 85 ;
- byte rein7 = (byte)(num10 >> 3) ;
- Console.WriteLine( "将85右移3位后的结果:{0}" , rein7 ) ;
- //在进行位与、或、异或运算时,如果两个运算对象的类型一致,则运算结果的类型就是运算对象的类型。比如对两个int变量a和b做与运算,运算结果的类型还是int型。如果两个运算
- //对象的类型不一致,则C#要对不一致的类型进行类型转换,变成一致的类型,然后进行运算。
- //类型转换的规则同算术运算中整型量的转换则一致。
- Console.ReadLine() ;
- }
- }
- }