C#中的位运算

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace 集合
  7. {
  8.     class 位运算
  9.     {
  10.         public static void Main()
  11.         {
  12.             //1、位逻辑非运算
  13.             //逻辑非运算是目的,只有一个运算象。位逻辑非运算按位运算象的值进行非运算,即:如果某一位等于0,就将其转变为1;如果某一位等于1,就将其转变为0
  14.             //比如,制的10010001行位逻辑非运算,果等于01101110,用十制表示就是:
  15.             //~145等于110制的01010101行位逻辑非运算,果等于10101010。用十制表示就是~85等于170
  16.             //下面我是用的有符号sbyte,果就是-86
  17.             sbyte num1 = 85 ;
  18.             sbyte renum = (sbyte)~num1 ;
  19.             Console.WriteLine( "85位非运算:{0}" , renum ) ;
  20.             //如果我用无符号型来存果就是170
  21.             byte num2 = 85 ;
  22.             byte renum2 = (byte)~num2 ;
  23.             Console.WriteLine( "85位非运算:{0}" , renum2 ) ;
  24.  
  25.             //2、位逻辑与运算
  26.             //逻辑与运算将两个运算象按位行与运算。与运算的规则11等于110等于00等于
  27.             //比如:10010001(二制)&11110000等于10010000(二制)。
  28.  
  29.             // 99的二制表示: 01100011
  30.             // 85的二制表示: 01010101
  31.             // 与运算的 : 01000001 ( 制:65)
  32.  
  33.             int num3 = 85 ; 
  34.             int num4 = 99 ;
  35.             int renum3 =  num3 & num4 ;
  36.             Console.WriteLine( "85 & 99 位与运算:{0}" , renum3 ) ;
  37.             
  38.            
  39.             //3、位逻辑或运算
  40.             //逻辑或运算将两个运算象按位行或运算。或运算的规则是:11110等于1
  41.             //00等于0。比如10010001(二制)| 11110000(二制)等于11110001(二制)。
  42.  
  43.             // 99的二制表示: 01100011
  44.             // 85的二制表示: 01010101
  45.             // 运算的 : 01110111 ( 制:119)
  46.  
  47.             int num5 = 85 ;
  48.             int num6 = 99 ;
  49.             int renum4 = num5 | num6 ;
  50.             Console.WriteLine( "85 | 99 位或的果:{0}" , renum4 ) ;
  51.            
  52.             //4、位逻辑异或运算
  53.             //逻辑异或运算将两个运算象按位行异或运算。异或运算的规则是:1异或1等于0
  54.             //1异或0等于10异或0等于0。即:相同得0,相异得1
  55.             //比如:10010001(二制)^11110000(二制)等于01100001(二制)。
  56.  
  57.             // 99的二制表示: 01100011
  58.             // 85的二制表示: 01010101
  59.             // 运算的 : 00110110 ( 制:54)
  60.  
  61.             int num7 = 85 ;
  62.             int num8 = 99 ;
  63.             int renum5 = num7 ^ num8 ;
  64.             Console.WriteLine( "85 ^ 99 位异或的果:{0}" , renum5 ) ;
  65.             
  66.             //5、位左移运算
  67.             //位左移运算将整个数按位左移若干位,左移后空出的部分0。比如:8位的byte
  68.             //byte a=0x65(即二制的01100101),将其左移3位:a<<3果是0x27(即二制的00101000)
  69.             
  70.  
  71.             // 85的二制表示: 01010101 
  72.             // 左移3位后成了: 01010101000
  73.  
  74.  
  75.             int num9 = 85 ;
  76.             int rein6 = num9 << 3 ;
  77.             Console.WriteLine( "85左移3位后的:{0}" , rein6 ) ;
  78.  
  79.             
  80.             //6、位右移运算
  81.             //位右移运算将整个数按位右移若干位,右移后空出的部分填0。比如:8位的byte
  82.             //Byte a=0x65(既(二制的01100101)将其右移3位:a>>3果是0x0c(00001100)
  83.            
  84.             // 85的二制表示: 01010101 
  85.             // 右移3位后成了:00001010
  86.  
  87.             byte num10 = 85 ;
  88.             byte rein7 = (byte)(num10 >> 3) ;
  89.             Console.WriteLine( "85右移3位后的:{0}" , rein7 ) ;
  90.  
  91.             //行位与、或、异或运算,如果两个运算象的型一致,运算果的型就是运算象的型。比如两个intab做与运算,运算果的int型。如果两个运算
  92.             //象的型不一致,C#不一致的转换成一致的型,然后行运算。
  93.             //转换规则同算运算中整型量的转换则一致。
  94.  
  95.             Console.ReadLine() ;
  96.         }
  97.     }
  98. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值