Bit shifting ( layer mask)

So I’m learning to code through programming games , and it’s kinda hard understanding things like bit shifting , masking , accessing files ect … anyway I used this like 20 times and whenever I look at it ,it feels kinda weird using stuff that I dont really understand it’s functionality .

an int number is represented as a 32 bit .

So 1<<8 means something like this right ? : 00000000000000000000000010000000

it positions the 1 in the 8th position and applies a logical && operation with the layers so only the layer with the ID 8 which actually got the same binary representation will return true .

So only if an object is in the layer with the ID 8 will be considered as a collider , please correct me if I’m wrong , I hate using things That I dont understand .

Thanks in advance .
And one more question , if I use this << it means I start counting from left to right , so if Use this >> does it mean the opposite ?

Thanks again .

I know a lot of people are learning to write in C# from the perspective of Unity, but really the subject is so large you can earn PhD’s in the science.

What you’re asking about is part of computer science. You’ll need to consider studying programming in C# (and/or other languages) as part of your own growth, because if you insist on learning programming from a Unity origin, you are going to be deluged with concepts far more advanced than students of programming would have to endure.

It’s like you’re trying to perform on a guitar in a professional band by having played on the “Garage Band” app.

That said, the subject you’ve inquired about is similar to what people learn to do when multiplying and dividing by 10 - remember about moving the decimal point to the right one notch for every mutliplication by 10, or to the left one notch when dividing by 10? That’s what shifting does, but in powers of 2.

What is happening, more fundamentally, is that you’re starting with a 1 (which is your example, but you could start with any number). It has a value of 1 just like in decimal math, but it is represented as a single bit. Then, if you shift it to the left 1, as in 1<<1, you’re moving that bit to the left to become 10, just as you’ve indicated. This is a multiplication, of 1 x 2. If you shift 1<<3, the result is 1000, which is 8 in the binary numbering system. You’re correct that 1>>4 is division by a power of 2, but in this case that 1 is shifted right and out of scope - it falls away, and you get 0 (because 1 / 2^4 is less than 1, it is a fraction, and integers just truncate fractions).

However, if you start with something like 8, and write 8>>2, then 8 is divided by 2^2 (which is 4), so the result will be 2 decimal, or 10 binary.

This has nothing to do with layers specifically. The layers respond to which bits are “on” or “off” using a mask. What you’re doing is fashioning the math.

I should point out that you are incorrect about one point. The <> do not perform any kind of logical operation like “and” or “xor”, or any other form like them. The shift operators <> only shift bits left or right. I think you intended to say that this mask is used in a logical operation to identify layers, but your phrase suggested the <> did that, which it doesn’t.

I should note, too, that this is such a fundamental operation that the CPU does this natively. There are shift instructions, and special circuitry (called barrel shifters) designed to perform this work very quickly, and it comes in more forms than the operators provide. Shifting with <> perform the kind of shift where bits that exceed either end of the “word” (32 bit word in your example, or 64 bit word in others) are simply dropped. However, there are “rolls”, where the bits that are tossed out of one end are brought back end from the other. A “roll right”, or ROR instruction, of 00000001 (an 8 bit value in an 8 bit register), would push the 1 bit out of the right, then back in from the left, which becomes 10000000. It has special use cases beyond the scope of our discussion, but it is useful to understand how large the subject is, and just where it applies.

I know a lot of people like to use the form 1<<8 to put a bit where the want it, but us old timers (I’ve been at this for decades) often prefer HEX representation to do the same. With practice we see hex values for their binary counterparts automatically, so if I needed the pattern 10100101, I’d merely use hex 0xA5. To me, 0xA5 looks like 10100101, and visa versa. Instead of 1<<8, I’d just write 0x0100.

To me, after practice, each “place” of the HEX number is 4 bits. 0x10 (a hex value) means, to me, the right most 4 bits are all zero, in the next group, going left, it’s a 1, so the binary is 10000. After a little practice, this is so natural that we just don’t bother with shifts to place bits.

When you are using bit operations, you always start from the right.

1 in decimal is represented by 1 in binary (I removed the leftmost 0s since they are irrelevant, and pollute the reading)

a << n means you “shift to the left all the bits of a n times”. So 1 << 8 is 100000000 (binary) (not 10000000 as you indicated) = 256 (decimal)

In the opposite, a >> n means you “shift to the right all the bits of a n times”. So 8 >> 2 is 1000 >> 2 = 10 (binary) = 2 (decimal)


When using NameToLayer(string layerName), you get n (the layer index as indicated in the documentation), so if you want to use it in Physics.Raycast, don’t forget to use bitshifting :

int layerIndex = NameToLayer( "MyLayer" ) ;
Physics.Raycast(origin, direction, Mathf.Infinity, 1 << layerIndex ) ;