Invert integer. How?

Vader

Well-Known Member
Licensed User
Longtime User
My app needs to invert the value of a given integer (8 bit).
I looked for NOT, but the signature expects an Integer: NOT(Value As Boolean) As Boolean

I also looked for INV() or Inverse(), but no joy.

I want to do the following:

Dim Command as Integer
Dim ExpectedResponse as Integer

Command = 20 ' Arbitary number assigned
ExpectedResponse = INV(Command) ' Should be 235

Any suggestions?
Maybe just:

B4X:
ExpectedResponse = 255 - Command
 

Vader

Well-Known Member
Licensed User
Longtime User
Sorry, no that won't work.

2's complement of FF (255) is -256
Likewise, the 2's complement of EF (239) is -240

It's a problem I see everywhere when you want to invert all the bits, rather than perform the two's complement.

FF = 11111111
Inverted is 00000000
2's complement is 00000000 (but ONLY if you look at the last 8 bits). 16 bit is 1111111100000000
 
Upvote 0

Vader

Well-Known Member
Licensed User
Longtime User
I think that this is what you are looking after:
B4X:
Sub INV(c As Int) As Int
   Return Bit.AND(0x000000FF, Bit.Not(c))
End Sub

Perfect! Thanks very much.

This is what I implemented:
B4X:
Private Sub Inv(Value As Int) As Int
   Return Bit.AND(0x000000FF, Bit.Not(Value))
End Sub
 
Upvote 0
Top