Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unary "Not" or bit-flip operator in PowerShell?

I have a decimal value 163 that I need to do a unary "not" operation on. In C++ and Java, this would look like ~(163). Essentially, I want every bit to flip.

Is there a way to do this in PowerShell using a unary operator or, if needed, a subroutine? I would also need this to flip all the bits of the entire 32-bit address. In other words, 163 in binary is 0b10100011, but I would want the entire 32-bit container to be flipped including the 163 at the end (e.g. 0b11111......01011100).

like image 368
AlwaysQuestioning Avatar asked Jan 10 '17 21:01

AlwaysQuestioning


People also ask

What is bit flip operator?

In computing, bit flipping may refer to: Bit manipulation, algorithmic manipulation of binary digits (bits) Bitwise operation NOT, performing logical negation to a single bit, or each of several bits, switching state 0 to 1, and vice versa.

What is >> in PowerShell?

That means that the console is waiting for more input. You've probably got a missing } somewhere.

What is XOR bit operation?

A bitwise XOR is a binary operation that takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 1 if only one of the bits is 1, but will be 0 if both are 0 or both are 1.

How do you flip a bit in Java?

Java BitSet flip() methodThe flip() method of Java BitSet class sets the bit set to its complement. For example, if a bit value contains a true value then if you apply flip() operation on it, it will return false. There are two overloaded flip() method available in BitSet class.


1 Answers

As Bill_Stewart commented, the -bnot (binary not or bitwise not) operator does what you want. It works in PowerShell 2.0.

Just be aware that PowerShell's default integers are generally signed, and the result of this operation will be different depending on that.

You may need to cast to one of the unsigned types to get the result you want:

-bnot ([uint32]163)
like image 163
briantist Avatar answered Oct 23 '22 17:10

briantist