CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

PowerShell – Bitwise Logical Operators

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.
123001111011
321101000001
-band
65001000001

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;

123001111011
321101000001
-bor
379101111011

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;

123001111011
321101000001
-bxor
314100111010

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;

1230001111011
-bnot
-1241110000100

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

PowerShell – Bitwise Logical Operators

One thought on “PowerShell – Bitwise Logical Operators

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top