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

move a bit with <<

Discussion in 'Scripting' started by chucky831, Aug 1, 2018.

  1. chucky831

    chucky831

    Joined:
    Aug 24, 2012
    Posts:
    265
    I do not understand how moving a bit of the value 3 becomes 8.

    3 = 0101
    1 << 3 = 1000
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,002
    First of all 3 = 0011 and not 0101

    and 1 = 0001

    so

    1 << 3 = 1000

    But 3 << 3 would be 1000 as well if we're working with 4bit, since there are no other bits to shift to :)
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,579
    Mathematically speaking,
    x = a << b
    So
    x = 2^a * b

    If
    a = 1, b = 3
    x = 2^1 * 3 = 2 * 3 = 6
    6 = 1 << 3
    110

    if
    a = 3, b = 1
    x = 2^3 * 1 = 8
    8 = 3 << 1
    1000

    if
    a = 3, b = 4
    x = 2^3 * 4 = 8 * 4 = 32
    32 = 3 << 4
    100000
     
    Last edited: Aug 1, 2018
  4. oLDo

    oLDo

    Joined:
    Mar 14, 2017
    Posts:
    55
    1 << 3 means move 0001 (binary) to left by three positions, result is 1000 (int: 8)
    if you have 3 << 3, 3 in binary is 0011, that means moving this binary value to left by three positions: 0011000 (int: 24)

    You need to understand binary numbers. Also is good to check binary operators.
     
    chucky831 likes this.
  5. chucky831

    chucky831

    Joined:
    Aug 24, 2012
    Posts:
    265
    ah ok I understood. But then when I use the levels in the raycasst what sense does this have?

    Code (csharp):
    1.  
    2.         int layerMask = 1 << (int)layer;
    3.         ray = viewCamera.ScreenPointToRay(Input.mousePosition);
    4.         RaycastHit hit;
    5.         bool hasHit = Physics.Raycast(ray, out hit, distanceToBackground, layerMask);
    6.  
    7.  
     
  6. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Layers in unity are denoted by a number from 0 to 31. Integers in unity are a 32 bit value. That looks like this in binary:

    Code (raw):
    1. 00000000 00000000 00000000 00000000
    For brevity, I'm going to abbreviate it from now on to just be the last four bits of the number:

    Code (raw):
    1. 0000
    But remember that all 32 bits are still there.

    In order to create a layer mask from an individual layer number, you must convert it into a bit mask by shifting 1 over by the value of the layer.

    If your layer number is 3, then you take a value of 1:

    Code (raw):
    1. 0001
    and you shift it to the left 3 bits (1 << 3):

    Code (raw):
    1. 1000
    This gives you a final layer mask value of
    1000
    .

    If your layer number is 2, then you take a value of 1:

    Code (raw):
    1. 0001
    and you shift it to the left 2 bits (1 << 2):

    Code (raw):
    1. 0100
    This gives you a final layermask value of
    0100
    .

    You can then OR these two together to create a layermask that uniquely identifies both layer 2 and layer 3 as being part of the mask at once, encoded within one number:

    Code (raw):
    1. 1000 OR
    2. 0100
    3. ____
    4. 1100
    What unity does internally is do an AND of the layermask you pass into
    Physics.Raycast
    against the layermask of the object its checking. Here's an example of the layermask we created above being checked against a layermask for layer 1, and then against layer 2.

    Code (raw):
    1. 1100 (1 << 3) + (1 << 2) AND
    2. 0010 (1 << 1)
    3. ____
    4. 0000
    5.  
    6. 1100 (1 << 3) + (1 << 2) AND
    7. 0100 (1 << 2)
    8. ____
    9. 0100
    If the result of the operation equals zero, then we know that the layermask does not contain the layer we're checking against, and the collision is ignored. If the result of the operation is greater than zero, we know that the layermask does contain the layer we're checking against, and the collision can proceed.
     
  7. oLDo

    oLDo

    Joined:
    Mar 14, 2017
    Posts:
    55
    LayerMask contains information which layers you want to check. To achieve that you can imagine binary positions as switches (true/false -> 1/0).

    upload_2018-8-3_7-36-40.png

    1 << 8 means moving 1 by 8 positions to left. That means turn on Layer 8. It's like switch. Don't think about it as numbers. Storing binary as int is just a way how to store it.
     
    Madgvox likes this.