Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.
Options

Physics layer value and bitmask value follow up question

So a couple of follow up questions to https://forums.hololens.com/discussion/2270/shift-operator-used-in-spatialmapping-cs#latest

So I now understand the shift (<<) and what it is doing, my question now is why is the value 31 being used for the layer value, and 1 << 31 evaluates to int.MinValue, that seems odd to me, is that an actual layer or are you defaulting a value here?

Tagged:

Best Answers

Answers

  • Options

    @cyris90,
    After your shift operation (1 << 31), you should be seeing int.MaxValue, which corresponds to the left-most bit being set in a 32 bit binary value (a 1 followed by 31 0s). The following image may help explain it better than words :)

    The image shows the bitmask for the Spatial Mapping layer in Hexadecimal, Decimal, Octal and Binary.

    Hope this helps!
    David

  • Options
    cyris90cyris90
    edited September 2016

    I don't mean to argue, so I am assuming I am wrong, I am getting int.MinValue when I do the shift. What am I missing? I do understand what you are saying about how the shift works though in theory, I am just wondering about why I get int.MinValue and you are saying I should get int.MaxValue.

    I used LinqPad5 to run these lines of code:

    var bitShift = 1 << 31;
    int.MinValue.Dump();
    int.MaxValue.Dump();
    bitShift.Dump();

    and got this output:
    -2147483648
    2147483647
    -2147483648

  • Options

    @cyris90,
    You are correct. My mistake was to use the calculator to display the value. My view was actually displaying the decimal value as a uint instead of a (signed) int.

    The rest of the discussion is accurate, but setting the left-most bit in an int will indeed result in int.MinValue as opposed to my originally stated int.MaxValue.

    Sorry about that!
    David

  • Options

    I do understand, my question is What is the purpose of doing this shift operation, what is the point of it?

  • Options
    PatrickPatrick mod
    edited September 2016

    Why are we doing this bit shift? Good question. While I don't have access to the unity source code, I can imagine that somewhere in the physics system they will want to see if a collision between two objects should be actually considered as a 'collision'. They could use ints for this and have code that looks something like:

    if (currentLayer == ObjectLayer) // currentLayer is the active physics layer, like the mask passed into Physics.Raycast
    {
         // Use this collision
    }
    

    which seems okay, but what if the currentLayer or the ObjectLayer could belong to two groups. Now we need to have lists, and iterate through the lists. Like:

    foreach(int layer in currentLayers)
    {
        if (ObjectLayers.contains(layer))
        { 
            // use the collision
            break;
        }
    }
    

    now we're getting into trouble. loops in loops raise red flags for performance.

    With the masks, we can do:

    if ((currentLayer & ObjectLayer) != 0)
    {
       // use the collision
    }
    

    the disadvantage here is that we can only have as many layers as there are bits in the type we are using for the mask, but we can add and remove layers from either side of the mask without changing performance.

    For more reading, here's a Wikipedia article.

    ===
    This post provided as-is with no warranties and confers no rights. Using information provided is done at own risk.

    (Daddy, what does 'now formatting drive C:' mean?)

  • Options
    cyris90cyris90
    edited September 2016

    @Patrick Very clear and I understand what you are saying. But how does it become a "Mask" (this is from the SpatialMapping.cs)

    For example:

       public static int PhysicsRaycastMask;
       private int PhysicsLayer = 31;
    
        // Convert the layer into a mask so it can be used to Raycast against.
        PhysicsRaycastMask = 1 << PhysicsLayer;
    

    So if I am reading this right... Its putting -2147483648 (integer) into PhysicsRaycastMask <-- Makes sense so far.. Now what I do not understand is the term "Mask" in this case. To me this is just minimum value for an int, where is this mask coming from?

Sign In or Register to comment.