Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question What is cullingMask ^ 0x8000000 doing for?

Discussion in 'Scripting' started by WayneJP, Sep 30, 2022.

  1. WayneJP

    WayneJP

    Joined:
    Jun 28, 2019
    Posts:
    44
    I saw this two line in a tutorial, but it doesn't explain this.

    Code (CSharp):
    1. camera1.cullingMask = cullingMask ^ 0x8000000;
    2. 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?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,512
    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:
    Code (CSharp):
    1. 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:
    Code (CSharp):
    1. camera1.cullingMask = cullingMask ^ (1 << 27);
    2. 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. WayneJP

    WayneJP

    Joined:
    Jun 28, 2019
    Posts:
    44
    Thanks! Your code is more readable. I'd like to use yours.
    And, it's "cullingMask2 | (1 << 27);" ,right? not "cullingMask2 | (1 < 27);"
     
    Bunny83 likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,512
    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.
     
    Bunny83 likes this.