8

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).

5
  • 2
    Do you mean -bnot 163? Commented Jan 10, 2017 at 21:17
  • I don't think I am. -bnot 163 gets me -164. Could you provide its usage? I need something backwards compatible with PowerShell v2. Commented Jan 10, 2017 at 21:20
  • 2
    @AlwaysQuestioning -164 is the correct result because it's a signed integer. See my answer. Commented Jan 10, 2017 at 21:24
  • That makes sense. Thank you! Commented Jan 10, 2017 at 21:31
  • 1
    It's -164 if you are talking about a signed value. Commented Jan 10, 2017 at 21:39

1 Answer 1

12

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.