What is cullingMask ^ 0x8000000 doing for?

I saw this two line in a tutorial, but it doesn’t explain this.

camera1.cullingMask = cullingMask ^ 0x8000000;
camera2.cullingMask = cullingMask2 | 0x8000000;

The binary form of 0x8000000 should be 1000 …000, right?
Then why use exclusive OR operator to compute these two bitwise?

One is toggling the bit #27, the other is forcing bit #27 on.

If they were forcing the bit off, they’d do something like:

camera1.cullingMask = cullingMask & ~0x8000000;

I don’t like using hex like that, especially if it were a tutorial. I’d much rather express it as:

camera1.cullingMask = cullingMask ^ (1 << 27);
camera2.cullingMask = cullingMask2 | (1 << 27);

Personal preference though.

As to why the XOR, you’d have to go ask whoever wrote the tutorial; code doesn’t explain the why.

3 Likes

Thanks! Your code is more readable. I’d like to use yours.
And, it’s “cullingMask2 | (1 << 27);” ,right? not “cullingMask2 | (1 < 27);”

1 Like

Go ahead but if you’re following a tutorial, I’d follow it exactly. Totally up to you.

Yes, just a typo in the forum.

1 Like