• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kog513 · Jul 21, 2018 at 02:10 PM · c#scripting beginner

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 .

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by JVene · Jul 21, 2018 at 02:44 PM

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kog513 · Jul 21, 2018 at 03:20 PM 1
Share

@JVene Thank you so much for explaining it to me this much ! I see you are program$$anonymous$$g since the late 70's and that's a great pleasure for me to receive such an explanation from such a person . Perfor$$anonymous$$g on a guitar in a professional band by playing on the "Garage Band" app. this was a hilarious example xD , anyway I'm planning to start learning computer science after university (Preparatory Institute for Engineering Studies) and I'm pretty fascinated by the program$$anonymous$$g logic .. it's just that uni is pretty useless in $$anonymous$$ching us complexe concepts and I'm tryna see whether I'll be able to endure hard program$$anonymous$$g concepts or not , and I didnt know where to start learning program$$anonymous$$g so I decided to code games . and yeah it feels that way , like jumping in the sea without knowing how to float .. anyway u actually motivated me for some reason by saying that ! Thanks a lot again .

avatar image JVene kog513 · Jul 21, 2018 at 05:57 PM 1
Share

I'm delighted you liked that and to meet you online. If you enjoyed trigonometry, or had little difficulty with algebra, writing software will be no difficulty. Indeed, the function in C, C#, C++, Java, etc. is based on the notion of the function from algebra, or the calculus. There are lots of programmers who picked up various languages just poking around scripts. I wouldn't trust the software they make, but the do actually understand a surprising amount. Engineered software, on the other hand, is built by those who actually decided to dedicate time, energy and the discipline of engineering to the craft, no matter how the came to know the material. By the time a university can fashion course material the subject advances, so everyone continues to study all the time. That said, the foundations of this work are quite old by comparison to industrial perception. C# was invented only 17 years ago, while C came from 1969, C++ from 1986. The QSort algorithm, which stands to this day as generally the fastest sort, is from 1956. Don $$anonymous$$nuth's encyclopedia was started in the 60's (still pertinent material). $$anonymous$$y point there is that there are timeless foundational materials for$$anonymous$$g a science behind this, and a great many programmers don't study it at all. Some think all of it is new. For C#, try not to be inducted by Unity's use of it as a scripting language. $$anonymous$$any think of the script as a central program$$anonymous$$g concept, but it isn't. The class is. $$anonymous$$y own favorite language is C++, but it's nasty. It's based on C and has all the same features such you can write a C program in C++ (if you're a bad C++ author). Without discipline you can create havoc. In 1982 in the IB$$anonymous$$ PC with original color monitor, a bug of one specific kind (writing the wrong byte to a register on the primitive 'graphics' card of the era) would actually cause smoke and sparks in the flyback transformer of the monitor and destroy it! C was created to be an assembler independent of any particular CPU, so that AT&T could create UNIX, the DNA parent to Linux. UNIX is the basis for $$anonymous$$AC (iOS), while Linux is the basis for Android, and all modern supercomputers. Windows is the oddball in reality. C# was their creation. Never like it myself ;)

avatar image kog513 JVene · Jul 22, 2018 at 02:07 PM 1
Share

You know a lot of stuff about the history of the science , and that's amazing . I once started to learn C++ just saying that as a beginner they all look similar .. I wish I could talk to you privately , I got a lot of questions to ask and my environment got no answers so far .. I mean I would like to ask someone with such skill and knowledge about the science and about what should I do next. I got 40 days holiday then I'll be taking the most important exam which will decide what engineering path will I take .. I want to be productive since then , but I cant figure out how , I'm just reading the Unity manuel searching for every thing I couldnt understand and write down notes and try it myself I have been doing this for like 20 days now , I just wanna know how great programmers with a long time career started and kept going .. Please if there is any advice u can give ,or any book u recommend , it will be really helpful , and I'm not giving up on this ,I wanna be a great software or game developer , I want to go deeper in the science and I'll . But you know asking for advice from someone skilled will be always helful . and thanks again !

Show more comments
avatar image
1

Answer by Hellium · Jul 21, 2018 at 02:32 PM

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 ) ;
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jul 21, 2018 at 02:42 PM 1
Share

Right, keep in $$anonymous$$d that layer with index 0 does a bit shift 0 times to the left, so it keeps the "1" where it is. So the 1 does end up at the "nth" place but you start counting at 0, like always.

avatar image kog513 · Jul 21, 2018 at 03:23 PM 0
Share

thanks a lot mate ! I'll write this down ! thanks again .

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

534 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

How to implement a simple dialogue system and disable movement/shooting? 1 Answer

How to call child overide function? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges