Bitwise XOR

Unity doesn’t seem to recognize the Javascript bitwise XOR (^) despite recognizing other bitwise operators. Does anyone else have this problem? Are there known workarounds for it?

javascript operators have no importance commonly.

That beeing said, ^ is also the C# XOR operator, so the question is: how does your code look like?

And XOR is nothing else than “set in one but not the other” which means:

a = b ^ c = (b ~c) | (~b c)

I am attempting to toggle a single bit in my camera’s culling mask without altering any other bits

target.camera.cullingMask = target.camera.cullingMask ^ (1 << (level + 12));

I’ve also tried implementing it the way you suggested:

var mask = target.camera.cullingMask;
var tog = 1 << (level + 8);
target.camera.cullingMask = (mask ~tog) | (~mask tog);

This seems to work.

Oh, thanks for the help.

I just ran into this exact same problem today while implementing a custom random number generator in JavaScript. What a coincidence :). I am filling it as a bug so that it gets fixed in a future release. For now I’m just going to rewrite the class in C#.

You can also toggle bits without XOR in a visually readable fashion easily

if (flags&bit) flags = ~bit;
else flags |= bit;

I do recall reading somewhere that javascript sometimes uses a slightly syntactically convoluted version of xor though.

Please delete if this is an invalid necromancy of this thread but I recently had this same issue and I wanted to share my solution.

Pretty much, there isn’t a XOR from what I’ve read but you can make your own by returning

(a&&b || !(a||b))

true if both are equal or both are false

Yeah it’s a necro of a 13 years old thread asking questions about a programming language that doesn’t exist anymore.

Also ^ is the C# xor operator, and it works as expected.

2 Likes