亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#運(yùn)算符之與,或,異或及移位運(yùn)算小結(jié)

 更新時(shí)間:2013年10月29日 09:32:10   作者:  
本文是對(duì)C#中的與,或,異或及移位運(yùn)算進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助

1.剖析異或運(yùn)算(^)

二元 ^ 運(yùn)算符是為整型和 bool 類型預(yù)定義的。對(duì)于整型,^ 將計(jì)算操作數(shù)的按位“異或”。對(duì)于 bool 操作數(shù),^ 將計(jì)算操作數(shù)的邏輯“異或”;也就是說,當(dāng)且僅當(dāng)只有一個(gè)操作數(shù)為 true 時(shí),結(jié)果才為 true。

數(shù)值運(yùn)算舉例

按位異或的3個(gè)特點(diǎn):
(1) 0^0=0,0^1=1  0異或任何數(shù)=任何數(shù)
(2) 1^0=1,1^1=0  1異或任何數(shù)-任何數(shù)取反
(3) 1^1=0,0^0=0  任何數(shù)異或自己=把自己置0

例如:10100001^00010001=10110000

按位異或的幾個(gè)常見用途:
(1) 使某些特定的位翻轉(zhuǎn)
例如對(duì)數(shù)10100001的第2位和第3位翻轉(zhuǎn),則可以將該數(shù)與00000110進(jìn)行按位異或運(yùn)算。
  0100001^00000110 = 10100111

(2) 實(shí)現(xiàn)兩個(gè)值的交換,而不必使用臨時(shí)變量。
例如交換兩個(gè)整數(shù)a=10100001,b=00000110的值,可通過下列語句實(shí)現(xiàn):
    a = a^b;   //a=10100111
    b = b^a;   //b=10100001
    a = a^b;   //a=00000110

(3) 在匯編語言中經(jīng)常用于將變量置零:
    xor   a,a

(4) 快速判斷兩個(gè)值是否相等
舉例1: 判斷兩個(gè)整數(shù)a,b是否相等,則可通過下列語句實(shí)現(xiàn):
        return ((a ^ b) == 0)

舉例2: Linux中最初的ipv6_addr_equal()函數(shù)的實(shí)現(xiàn)如下:

復(fù)制代碼 代碼如下:

    static inline int ipv6_addr_equal(const struct in6_addr *a1, const struct in6_addr *a2)
    {
        return (a1->s6_addr32[0] == a2->s6_addr32[0] &&
            a1->s6_addr32[1] == a2->s6_addr32[1] &&
            a1->s6_addr32[2] == a2->s6_addr32[2] &&
            a1->s6_addr32[3] == a2->s6_addr32[3]);
    }

可以利用按位異或?qū)崿F(xiàn)快速比較, 最新的實(shí)現(xiàn)已經(jīng)修改為:
復(fù)制代碼 代碼如下:

    static inline int ipv6_addr_equal(const struct in6_addr *a1, const struct in6_addr *a2)
    {
    return (((a1->s6_addr32[0] ^ a2->s6_addr32[0]) |
        (a1->s6_addr32[1] ^ a2->s6_addr32[1]) |
        (a1->s6_addr32[2] ^ a2->s6_addr32[2]) |
        (a1->s6_addr32[3] ^ a2->s6_addr32[3])) == 0);
    }

2 & 運(yùn)算符(與)

1 & 0 為0
0 & 0 為0
1 & 1 為1

3 | 運(yùn)算符(或)

1 & 0 為1
0 & 0 為0
1 & 1 為1

------------------

C#移位運(yùn)算(左移和右移)

C#是用<<(左移) 和 >>(右移) 運(yùn)算符是用來執(zhí)行移位運(yùn)算。

左移 (<<)

將第一個(gè)操作數(shù)向左移動(dòng)第二個(gè)操作數(shù)指定的位數(shù),空出的位置補(bǔ)0。
左移相當(dāng)于乘. 左移一位相當(dāng)于乘2;左移兩位相當(dāng)于乘4;左移三位相當(dāng)于乘8。

  x<<1= x*2
  x<<2= x*4
  x<<3= x*8
  x<<4= x*16

同理, 右移即相反:

右移 (>>)
將第一個(gè)操作數(shù)向右移動(dòng)第二個(gè)操作數(shù)所指定的位數(shù),空出的位置補(bǔ)0。

右移相當(dāng)于整除. 右移一位相當(dāng)于除以2;右移兩位相當(dāng)于除以4;右移三位相當(dāng)于除以8。

  x>>1= x/2
  x>>2= x/4
  x>>3= x/8
  x>>4=x/16


 int i = 7;
 int j = 2;
 Console.WriteLine(i >> j);   //輸出結(jié)果為1

當(dāng)聲明重載C#移位運(yùn)算符時(shí),第一個(gè)操作數(shù)的類型必須總是包含運(yùn)算符聲明的類或結(jié)構(gòu),并且第二個(gè)操作數(shù)的類型必須總是 int,如:

復(fù)制代碼 代碼如下:

class Program   
{       
 static void Main(string[] args)       
 {           
  ShiftClass shift1 = new ShiftClass(5, 10);           
  ShiftClass shift2 = shift1 << 2;           
  ShiftClass shift3 = shift1 >> 2;           
  Console.WriteLine("{0} << 2 結(jié)果是:{1}", shift1.valA,  shift2.valA);           
  Console.WriteLine("{0} << 2 結(jié)果是:{1}", shift1.valB,shift2.valB);           
  Console.WriteLine("{0} >> 2 結(jié)果是:{1}", shift1.valA,  shift3.valA);           
  Console.WriteLine("{0} >> 2 結(jié)果是:{1}", shift1.valB, shift3.valB);           
  Console.ReadLine();       
 }       
 public class ShiftClass       
 {          
  public int valA;          
  public int valB;           
  public ShiftClass(int valA, int valB)           
  {               
   this.valA = valA;               
   this.valB = valB;           
  }           
  public static ShiftClass operator <<(ShiftClass shift, int count)           
  {               
   int a = shift.valA << count;               
   int b = shift.valB << count;               
   return new ShiftClass(a, b);           
  }           
  public static ShiftClass operator >>(ShiftClass shift, int count)           
  {               
   int a = shift.valA >> count;               
   int b = shift.valB >> count;               
   return new ShiftClass(a, b);           
  }       
 }   
}

因?yàn)槲灰票瘸顺俣瓤?對(duì)效率要求高,而且滿足2的冪次方的乘除運(yùn)方,可以采用位移的方式進(jìn)行。

相關(guān)文章

最新評(píng)論