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.
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?
Best Answers
-
OptionsPatrick mod
The term "mask" implies that there is a field of bits.
If you are thinking about this correctly, you shouldn't think that you are putting -21474...etc into the variable but rather putting 0b1000 0000 0000 0000 0000 0000 0000 0000 into the variable. That the mask value becomes the minimum int value is a coincidence. If the physics layer were 30 instead then we would get 0b0100 0000 0000 0000 0000 0000 0000 0000 as the mask or 1073741824 in decimal.
===
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?)
5 -
Optionscyris90 ✭
So, the datatype in this instance is not for the data itself but the mask of bits? So in this case its a 32 bit mask with 1 significant digital at the beginning. I understand now! thank you so much!
5
Answers
@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
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
@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
I do understand, my question is What is the purpose of doing this shift operation, what is the point of it?
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:
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:
now we're getting into trouble. loops in loops raise red flags for performance.
With the masks, we can do:
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?)
@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:
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?
The term "mask" implies that there is a field of bits.
If you are thinking about this correctly, you shouldn't think that you are putting -21474...etc into the variable but rather putting 0b1000 0000 0000 0000 0000 0000 0000 0000 into the variable. That the mask value becomes the minimum int value is a coincidence. If the physics layer were 30 instead then we would get 0b0100 0000 0000 0000 0000 0000 0000 0000 as the mask or 1073741824 in decimal.
===
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?)
So, the datatype in this instance is not for the data itself but the mask of bits? So in this case its a 32 bit mask with 1 significant digital at the beginning. I understand now! thank you so much!