<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # C# 按位和移位運算符 > 原文: [https://www.programiz.com/csharp-programming/bitwise-operators](https://www.programiz.com/csharp-programming/bitwise-operators) #### 在本教程中,我們將詳細學習 C# 中的按位和移位運算符。 C# 提供了 4 個按位和 2 個移位運算符。 按位和移位運算符用于對整數(`int`,`long`等)和布爾數據執行位級運算。 這些運算符在現實生活中并不常用。 如果您有興趣探索更多內容,請訪問[按位運算的實際應用](https://stackoverflow.com/questions/3883384/practical-applications-of-bitwise-operations "Practical applications of bitwise operators")。 下面列出了 C# 中可用的按位和移位運算符。 C# 按位運算符列表 | 運算符 | 運算符名稱 | | --- | --- | | `~` | 按位補碼 | | `&` | 按位與 | | <code>&#124;</code> | 按位或 | | `^` | 按位異或(XOR) | | `<<` | 按位左移 | | `>>` | 按位右移 | * * * ## 按位或 `|`表示按位或運算符。 它對兩個操作數的相應位執行按位或運算。 如果任一位為`1`,則結果為`1`。 否則結果為`0`。 如果操作數的類型為`bool`,則按位或運算等效于它們之間的邏輯或運算。 例如, ```cs 14 = 00001110 (In Binary) 11 = 00001011 (In Binary) ``` 在 14 至 11 之間按位`OR`操作: ```cs 00001110 00001011 -------- 00001111 = 15 (In Decimal) ``` ### 示例 1:按位或 ```cs using System; namespace Operator { class BitWiseOR { public static void Main(string[] args) { int firstNumber = 14, secondNumber = 11, result; result = firstNumber | secondNumber; Console.WriteLine("{0} | {1} = {2}", firstNumber, secondNumber, result); } } } ``` 當我們運行程序時,輸出將是: ```cs 14 | 11 = 15 ``` * * * ## 按位與 `&`表示按位與運算符。 它對兩個操作數的相應位執行按位與運算。 如果任一位為`0`,則結果為`0`。 否則結果為`1`。 如果操作數的類型為`bool`,則按位與運算等效于它們之間的邏輯與運算。 For Example, ```cs 14 = 00001110 (In Binary) 11 = 00001011 (In Binary) ``` 14 至 11 之間的按位與運算: ```cs 00001110 00001011 -------- 00001010 = 10 (In Decimal) ``` ### 示例 2:按位與 ```cs using System; namespace Operator { class BitWiseAND { public static void Main(string[] args) { int firstNumber = 14, secondNumber = 11, result; result = firstNumber & secondNumber; Console.WriteLine("{0} & {1} = {2}", firstNumber, secondNumber, result); } } } ``` 當我們運行程序時,輸出將是: ```cs 14 & 11 = 10 ``` * * * ## 按位異或 按位異或運算符由`^`表示。 它對兩個操作數的相應位執行按位異或操作。 如果相應位**相同**,則結果為`0`。 如果相應位不同,則結果為`1`。 如果操作數的類型為`bool`,則按位異或運算等效于它們之間的邏輯異或運算。 For Example, ```cs 14 = 00001110 (In Binary) 11 = 00001011 (In Binary) ``` 14 至 11 之間的按位異或運算: ```cs 00001110 00001011 -------- 00000101 = 5 (In Decimal) ``` 如果您想進一步了解按位異或的用法,請訪問[異或的魔力](https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/xor.html "The Magic of XOR operator") ### 示例 3:按位異或 ```cs using System; namespace Operator { class BitWiseXOR { public static void Main(string[] args) { int firstNumber = 14, secondNumber = 11, result; result = firstNumber^secondNumber; Console.WriteLine("{0} ^ {1} = {2}", firstNumber, secondNumber, result); } } } ``` 當我們運行程序時,輸出將是: ```cs 14 ^ 11 = 5 ``` * * * ## 按位補碼 按位補碼運算符由`~`表示。 它是一元運算符,即僅對一個操作數進行運算。`~`運算符**將每個位**取反,即將 1 更改為 0,將 0 更改為 1。 For Example, ```cs 26 = 00011010 (In Binary) ``` 26 的按位補碼運算: ```cs ~ 00011010 = 11100101 = 229 (In Decimal) ``` ### 示例 4:按位補碼 ```cs using System; namespace Operator { class BitWiseComplement { public static void Main(string[] args) { int number = 26, result; result = ~number; Console.WriteLine("~{0} = {1}", number, result); } } } ``` 當我們運行程序時,輸出將是: ```cs ~26 = -27 ``` 當我們期望`229`時,得到`-27`作為輸出。 **為什么會發生這種情況?** 發生這種情況是因為我們期望是`229`的二進制值`11100101`實際上是`-27`的 2 的補碼表示。 計算機中的負數以 2 的補碼表示形式表示。 對于任何整數`n`,`n`的 2 的補碼將為`-(n+1)`。 2 的補碼 | 小數 | 二進制 | 2 的補碼 | | --- | --- | --- | | 0 | 00000000 | `-(11111111 +1) = -00000000 = -0`(十進制) | | 1 | 00000001 | `-(11111110 +1) = -11111111 = -256`(十進制) | | 229 | 11100101 | `-(00011010 + 1) = -00011011 = -27` | 溢出值在 2 的補碼中被忽略。 `26`的按位補碼為 229(十進制),`229`的 2 補碼為`-27`。 因此,輸出為`-27`而不是`229`。 * * * ## 按位左移 按位左移運算符由`<<`表示。`<<`運算符將數字左移指定的位數。 零添加到最低有效位。 以十進制表示,相當于 ```cs num * 2bits ``` For Example, ```cs 42 = 101010 (In Binary) ``` 42 的按位提升移位操作: ```cs 42 << 1 = 84 (In binary 1010100) 42 << 2 = 168 (In binary 10101000) 42 << 4 = 672 (In binary 1010100000) ``` ### 示例 5:按位左移 ```cs using System; namespace Operator { class LeftShift { public static void Main(string[] args) { int number = 42; Console.WriteLine("{0}<<1 = {1}", number, number<<1); Console.WriteLine("{0}<<2 = {1}", number, number<<2); Console.WriteLine("{0}<<4 = {1}", number, number<<4); } } } ``` 當我們運行程序時,輸出將是: ```cs 42<<1 = 84 42<<2 = 168 42<<4 = 672 ``` * * * ## 按位右移 按位左移運算符由`>>`表示。`>>`運算符將數字向右移動指定的位數。 第一個操作數向右移動第二個操作數指定的位數。 In decimal, it is equivalent to ```cs floor(num / 2bits) ``` For Example, ```cs 42 = 101010 (In Binary) ``` Bitwise Lift Shift operation on 42: ```cs 42 >> 1 = 21 (In binary 010101) 42 >> 2 = 10 (In binary 001010) 42 >> 4 = 2 (In binary 000010) ``` ### 示例 6:按位右移 ```cs using System; namespace Operator { class LeftShift { public static void Main(string[] args) { int number = 42; Console.WriteLine("{0}>>1 = {1}", number, number>>1); Console.WriteLine("{0}>>2 = {1}", number, number>>2); Console.WriteLine("{0}>>4 = {1}", number, number>>4); } } } ``` 當我們運行程序時,輸出將是: ```cs 42>>1 = 21 42>>2 = 10 42>>4 = 2 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看