Earn More by Sharing What You Love
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!

Tim Corey
5m 54s
在C#中,二進位運算對於處理位元操作至關重要,特別是在處理標誌、權限和底層資料處理時。 基本的二進位運算子之一是 OR (|) 運算子。 在他的影片Binary in C#: The Binary OR Operator in 10 minutes or less中,Tim Corey 分解了在 C#中二進位 OR 運算子的工作原理及其實際應用。 讓我們一步步地來了解他的解釋。
Tim 透過他在C#中理解二進位運算系列為本影片揭開序幕。 這是該系列中的第五課,專注於 OR 運算子。 他設置了一個基本情境,其中有兩個整數值 Val1 和 Val2,以二進位格式呈現。 這些值將列印在控制台中,以清楚顯示它們在二進位中的外觀。
在這一部分,Tim 解釋了在二進位比較中 OR 運算子的作用。 他澄清了 OR 運算子在對應的位置比較兩個二進位值的每一位。 如果任一位是 1,那位置上的結果將是 1。 如果兩位都是 0,則結果將是 0。
為了演示這點,他透過比較兩個值的範例來進行解說:
Val1 = 1101 (二進位表示)Val2 = 0010 (二進位表示)Tim 強調在每個位元位置上,如果至少有一個值為 1,則結果位將是 1。 否則,它將保持 0。
Tim 進一步撰寫實際程式碼來執行 OR 運算。 他將 OR 運算的結果分配給使用單一管道 (|) 運算子的變數中:
// Define two binary values as integers
int Val1 = 0b1101; // Binary representation: 1101
int Val2 = 0b0010; // Binary representation: 0010
// Perform the bitwise OR operation
int result = Val1 | Val2;
// Print the result to the console
Console.WriteLine(Convert.ToString(result, toBase: 2)); // Output: 1111 in binary' Define two binary values as integers
Dim Val1 As Integer = &B1101 ' Binary representation: 1101
Dim Val2 As Integer = &B0010 ' Binary representation: 0010
' Perform the bitwise OR operation
Dim result As Integer = Val1 Or Val2
' Print the result to the console
Console.WriteLine(Convert.ToString(result, toBase:=2)) ' Output: 1111 in binary0b 前綴來定義二進位值,這樣可以在 C# 中直接輸入二進位。result 變數中。Convert.ToString(result, toBase: 2) 將整數結果轉換為字串表示其二進位格式,以便顯示。Tim 在這裡做了一個重要區別:在 if 語句中的邏輯表達式中,使用 || for logical OR, whereas in bitwise operations, we use a single | 來進行位元運算。
運行 OR 運算後,Tim 在控制台中印出結果。 輸出顯示了 OR 如何在位元層級運作:
1101 | 0010 = 1111結果中的每個位皆遵循規則:如果 Val1 或 Val2 任一存在 1,則結果為 1。 這提供一個清晰的視覺呈現,顯示 OR 如何結合兩個二進位數。
為了強化概念,Tim 修改 Val2 以觀察 OR 運算子如何反應不同的輸入。 他將 Val2 改為 1010 並再次運行運算。 結果仍然符合理OR邏輯:
1101 | 1010 = 1111Tim 指出,只要某個數值在指定位置擁有 1,則該位置的結果也會是 1。
Tim 簡要地回顧了邏輯 OR (||) and bitwise OR (|) 的不同之處。 邏輯 OR 用於布林條件,比如:
// Example of logical OR in a conditional statement
if (condition1 || condition2)
{
// Execute if either condition1 or condition2 is true
}' Example of logical OR in a conditional statement
If condition1 OrElse condition2 Then
' Execute if either condition1 or condition2 is true
End If而位元 OR 則用於位層級的二進位運算,如這堂課程所示。
Tim 最後討論了一些使用位元 OR 的實際場景。 一個常見的用例是處理基於標誌的設定。 例如,如果您有不同的權限標誌儲存為二進位值,OR 可以有效地結合它們。
例如,在基於標誌的系統中:
READ_PERMISSION = 0001WRITE_PERMISSION = 0010EXECUTE_PERMISSION = 0100如果使用者需要同時具備讀取和寫入權限,可以使用 OR 結合它們:
// Define permission flags as constants
const int READ_PERMISSION = 0b0001;
const int WRITE_PERMISSION = 0b0010;
// Combine read and write permissions using bitwise OR
int userPermissions = READ_PERMISSION | WRITE_PERMISSION; // Result: 0011
// Print the user permissions in binary
Console.WriteLine(Convert.ToString(userPermissions, toBase: 2)); // Output: 0011' Define permission flags as constants
Const READ_PERMISSION As Integer = &B0001
Const WRITE_PERMISSION As Integer = &B0010
' Combine read and write permissions using bitwise OR
Dim userPermissions As Integer = READ_PERMISSION Or WRITE_PERMISSION ' Result: 0011
' Print the user permissions in binary
Console.WriteLine(Convert.ToString(userPermissions, toBase:=2)) ' Output: 0011Tim 強調 OR 特別有用,因為在處理此類標誌組合時不會影響現有值。
在影片結尾,Tim 總結了主要點:
|) 比較兩個數字中的對應位。1,結果就是 1。||),後者用於布林表達式。依循 Tim 的解釋和範例,我們能夠紮實地理解 C# 中二進位 OR 運算子的工作原理以及適合應用的地方。
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!
Join our newsletter, you’ll get exclusive access on article updates. We value your privacy