PowerShell provides below list of Bitwise operators and these ONLY works on Integer types. Why only Integer types? Because, these operators works on bits (0s and 1s) and only numeric types can support bitwise operations.
Bitwise AND -band
operator
This operator takes two operands as inputs and apply the bitwise AND operation on each bit position, to produce the result. Considering if both bits have the value 1, it produces the result 1. Otherwise, it produces the result 0. Confused? Let’s take a simple example;
PS C:\> 123 -band 321 65
Here are the steps how it produced the result 65;
- Binary form of 123 is 1111011
- And binary form of 321 is 101000001
- Now apply bitwise AND operation (
-band
) to produce the result. Below table explains in more detail; 1-band
1 produces the result 1. In other bit combinations, it produces the result 0.
123 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 1 | 1 |
321 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 |
-band | |||||||||
65 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 |
Bitwise OR -bor
operator
This operator takes 2 operands as inputs. Produces the result, by comparing each bit value. If both bit values are 1, or any of the bit value is 1, it produces the result 1. Other cases, it produces the value 0.
PS C:\> 123 -bor 321 379
Below table captures the operation;
123 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 1 | 1 |
321 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 |
-bor | |||||||||
379 | 1 | 0 | 1 | 1 | 1 | 1 | 0 | 1 | 1 |
Bitwise XOR (Exclusive OR) -bxor
operator
This operator also takes 2 operands as inputs. Compares each bit value; and produces the result 1, if ANY ONE of the bit value is 1. Yes, any one of the bit value should be 1; if both bit values 1 or 0, it produces the result 0.
PS C:\> 123 -bxor 321 314
Here is the table shows how this operator works;
123 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 1 | 1 |
321 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 |
-bxor | |||||||||
314 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 0 |
Bitwise NOT -bnot
operator
Bitwise NOT operator is an unary operator. That means, it takes a single operand as an input. If you give wrong number of arguments to it; it produces the error.
Below is an example, where it produces an Error; if attempts to apply this with more than 1 operand.
PS C:\> 123 -bnot 321 At line:1 char:5 + 123 -bnot 321 + ~~~~~ Unexpected token '-bnot' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken
Below is another example, where it produces the Error; this operator should appear before the operand or expression.
PS C:\> 123 -bnot At line:1 char:5 + 123 -bnot + ~~~~~ Unexpected token '-bnot' in expression or statement. At line:1 char:10 + 123 -bnot + ~ Missing expression after unary operator '-bnot'. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken
Now let’s see what it does with the input. Observe that, this operator appears before the operand or value or expression.
PS C:\> -bnot 123 -124
This operator, flips each bit; that means, a bit of 1 converts to 0 and vice versa. Hence you are seeing -ve result for +ve numbers and +ve results for -ve numbers. We can say it is a kind of negation operator.
Below table captures the above operation;
123 | 0 | … | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 1 | 1 |
-bnot | |||||||||||
-124 | 1 | … | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
Observer that, it converts all 0s to 1s and vice versa. As sign bit is set to 1, you will see negative number as the result for the above expression.
[..] David
One thought on “PowerShell – Bitwise Logical Operators”