- Home /
BitShifting and boolean functions
i currently have around 30 bools in a script on unity. I was told that maybe bit-shifting would be an easier way to do all this. but i am clueless when it comes to bit-shifting or bit anything. could anyone point me in the direction or maybe provide a small example of setting bits and ect.?
im setting bools, when buttons are pressed. which is read by another script that detects when specific bools are true together.. for example, when button 1 is paired with button 4 do this.. and so on and so forth.
if you need any more information please let me know and ill provide as much as i can... thank you for your help!
Your suggestion has helped allot, did some more digging and found out how to utilize bit shifting to eli$$anonymous$$ate 37 Booleans woo!
Good job, maybe post some links or a bit of code for completeness
Answer by meat5000 · Mar 05, 2018 at 08:21 PM
Well an 'Int' has 32 bits. This is getting back to principles somewhat in that the idea is to treat each of those bits as a separate switch akin to something we'd do in electronic circuits. Bitwise commands are fast and straightforward (Bitwise is modifying the bits that make up the integer 'word'). This is similar to the way layers are handled. The idea of a bool... true or false, on or off is the same but its like separating out the bits of the integer and having a separate memory range for them. In this sense bitwise ops can help condense this kind of operation.
A bool has 1 'on/off'; its 1-bit so to speak. An int can contain 32 of them. If you want to get your teeth in to this in a way you can visualise it, look at Layermasks and in particular find the awesome How to use a Layermask answer here on UA.
Hope this helped nudge you in the right direction.
Your answer

Follow this Question
Related Questions
FindWithTag or how to find if there any instances left 1 Answer
How to fix unstable boolean values? [possible bug] 1 Answer
issue with yield.WaitForSeconds(); 1 Answer
GUI.toggle always false 1 Answer
This boolean is messing up. Help? 1 Answer