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

How To Remove LayerMask (Bit Operator)

Discussion in 'Scripting' started by renman3000, Aug 6, 2014.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Hi there,
    I was recently introduced to bit operators and specifically how to add a layer mask after run time to a variable.

    Code (csharp):
    1.  
    2. faceLayerMask = faceLayerMask | (1<<10);
    3.  
    but I can not for the life of me, figure out how to remove the added layer. Various attempts have failed.



    Thanks
     
  2. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    try

    faceLayerMask ^= (1<<10);


    the ^= operator returns true only if one and only one of both bits being compared is equal to 1
     
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    That did it.
    Thanks.
    !!!::)()(
     
    DoomSamurai likes this.
  4. Estecka

    Estecka

    Joined:
    Oct 11, 2013
    Posts:
    62
    Doesn't that mean that you could accidentally add the layer to the mask if doens't contain the layer already ?
    My guess for strictly removing a layer from a mask would have been:
    faceLayerMask &= ~(1<<10);

    (Though I'm also new to bit operators so correct me if I'm wrong, I literally just discovered the ~ operator while checking whether ! would do what I wanted).
     
    lordofduct likes this.
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It's an old post, but I believe the &= ~(1 << shift) is correct. :)
     
    Estecka and lordofduct like this.