Double meaning of ~

BlitzMax Forums/BlitzMax Programming/Double meaning of ~

ImaginaryHuman(Posted 2006) [#1]
I am a little confused with how to properly use the tilde ~

"Variable1 ~ Variable2" is the same as saying Variable 1 XOR Variable 2?

...while...

"~ Variable 1" is the same as saying NOT Variable 1 (do a bitwise NOT?)

Does `complement` mean the same as `NOT`?

So if you only have one variable in the expression, it's a NOT, otherwise it's an XOR?


Koriolis(Posted 2006) [#2]
It does seem weird indeed.
On the other hand, +5 does make sense, and is just the same as 0+5.
Similarly in BlitzMax ~5 makes sense, and is just the same as -1 ~ 5 ( = 'Bitwise not' 5 ).

That's a good way to pretend it's not so weird.


FlameDuck(Posted 2006) [#3]
Does `complement` mean the same as `NOT`?
Not really, but they're related.

So if you only have one variable in the expression, it's a NOT, otherwise it's an XOR?
It's an XOR in both cases. In the second case it's an XOR with either all bits set, or reset, which is functionally the same as an inversion.

Wether the compiler does any optimization with regards to the assembly output I wouldn't know, and can't be bothered to test. :o>


Michael Reitzenstein(Posted 2006) [#4]
Well, ~value isn't xor with an implicit variable in Blitz, it's explicitly bitwise complement (see the docs). And that doesn't reduce to XOr in x86 or PPC - they both have bitwise not ops. And an XOr with all bits reset is a nop, not a not!

Also, 'complement' is indeed the same as 'not' (but not any specific implementation of not, which may be bitwise or logical), and it isn't the same as 'two's complement'.


ImaginaryHuman(Posted 2006) [#5]
*head spins*. lol okay thanks guys, that helped.


Will(Posted 2006) [#6]
what does xor mean? What does the tilde do?


Helios(Posted 2006) [#7]
xor = Bitwise eXclusive OR
~ = Bitwise complement (basically this reverses each bit in an integer so 0 would become 1 and vis-versa, you can get an explanation on it here - http://docs.hp.com/en/B3901-90007/ch05s04.html)


Tom Darby(Posted 2006) [#8]
Bitwise exclusive or (A v B):

if A = B, (A v B) = 0
if A != B, (A v B) = 1

(EDIT: used ~ when I should technically have used v; while ~ is used to denote an XOR in BlitzMax, a more 'universal' symbol is v. Sorry for the confusion.)


Michael Reitzenstein(Posted 2006) [#9]
Oops, was wrong there...


ImaginaryHuman(Posted 2006) [#10]
Never seen that symbol for XOR before.

So what we're saying then, is that:

A=~A

Inverses A, like doing a NOT, all 1's become 0's, all 0's become 1's

and

A=A~B

Does an XOR between A and B, whereby if you did a second XOR it would restore the data back to its previous value.

Right?


Helios(Posted 2006) [#11]
EDIT: Nevermind, miss read a post.


ImaginaryHuman(Posted 2006) [#12]
I see this is going from confusion to confusion, lol

It would've been nice to keep ! as the `not` operation, separate from ~ which should always mean XOR.

The only way I can figure out what these things are doing is to write little test programs and look at the printout.


Yan(Posted 2006) [#13]
So what we're saying then, is that:

A=~A

Inverses A, like doing a NOT, all 1's become 0's, all 0's become 1's

and

A=A~B

Does an XOR between A and B, whereby if you did a second XOR it would restore the data back to its previous value.

Right?
Yes!


ImaginaryHuman(Posted 2006) [#14]
Hurray!