Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Kleptomaniac · Apr 22, 2012 at 02:36 PM · binaryoperatorshiftbitwise

Bitwise Operators

Hey there,

I'm not too savvy on the whole binary ... but I'm learning. I'm beginning to learn about logical bitwise operators like AND operators, OR operators, etc. I know that these are used for boolean expressions with two operands (&, |, &&, ||, etc.), however I'd love to know how, in terms of binary, they actually work. For example, this explanation of the AND operator (here) goes right over my head because of my still limited knowledge of binary.

I know what these operators do, I just want to know how they work! :) So basically, could someone please explain how bitwise operators like OR, AND, XOR, etc. work?

Also, however, I would love if someone could give me an explanation of bitwise shift operators. And also a context in which it would actually ever be used, if ever. :)

Thanks very much,

Klep

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

3 Replies

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

Answer by fafase · Apr 22, 2012 at 03:26 PM

In assembly programming :

 ANL  A, #00001111B clears four most significant bits.
 
         11001100  Value
     AND    00001111  Byte used to set the desired value
         00001100

See the first four bits (MSB) are cleared while the last four remains the same, you need 1 AND 1 to get 1

 ORL  A, #00001111B sets four least significant bits.
 
         11001100  Value
     OR    00001111  byte used to set to the desired value
         11001111

The four last bit (LSB) are set to 1 because you need only one 1 to get 1... 1 OR 0 =>1
0 OR 0 => 0

 XRL  A, #00001111B inverts four least significant bits.
 
         11001100  Value
     XOR    00001111  Byte used to set to the desired value
         11000011

XOR is the most confusing

 0 XOR 0 =>0
 1 XOR 1 =>0
 0 XOR 1 =>1
 1 XOR 0 =>1

It is one or the other gives 1 but if both then 0.

The big idea being that at processor level, a lot of computation are based on flags, meaning that the processor checks a particular bit inside a byte to define the state.

Consider a byte of a router for communication, 00000001 could represent the system is on. 00000011 the system is on and sending information, 10000000 the system is down and so on. You would use AND OR XOR to define the state of the device.

For shift operator I think you meant, when a byte is shift to the right or the left. That is used for example in multiplication and division by two.

 00000001B = 1D 
 shift to the left
 00000010B = 2D
 00000100B = 4D
 00001000B = 8D
 By shifting I multiplied by 2

 



 
Comment
Add comment · Show 1 · 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 Kleptomaniac · Apr 23, 2012 at 01:21 PM 0
Share

Wow, thank you for that! That was really helpful. :)

avatar image
1

Answer by AchillesWF · Apr 22, 2012 at 02:45 PM

Well, I could write up a big paragraph here. But, it has already been done:

http://en.wikipedia.org/wiki/Bitwise_operation

Essentially, the computer represents a number with a sequence of bits and bitwise operators manipulate individual bits rather than the entire number to change the number in controlled ways.

Bitwise shift operators are quick ways to multiple numbers by powers of 2 (among other things). Bit operations are also used to compare numbers containing a number of "flag" values with just the flag value of interest.

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 raoz · Apr 22, 2012 at 02:54 PM 0
Share

Also, you can find out if a number is even with AND'ing it with 1. If the result is 1, than it is even, if the result is 0, than it is odd

avatar image Kleptomaniac · Apr 22, 2012 at 03:09 PM 0
Share

Thanks a lot, doing a bit more research now. :)

avatar image
0

Answer by Owen-Reynolds · Apr 22, 2012 at 04:52 PM

A bit string can be an efficient way to represent a lot of bools in a row. Unity uses it mostly for layerMasks. Instead of setting: layer1=true; layer2=false, layer3=true; you set the layerMask to 101.

You can store up to 32 layer yes/no's in a single int. Even better, the computer's built in hardware can check if the layers match in only 1 step (binary and's, or's ... are built into the chip) instead of using 32 if's.

In the old days, you used it everywhere, to save space. Sometimes, if you have two values that never went past 0-15, you used bit-shifting to have them share one 8-bit word. It took longer to read them, but back then 32K was a big computer, so every byte counted.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

What is the difference between these ? 1 Answer

Trouble Setting/Combining LayerMask with "Builtin" Layer 0 Answers

Where is the >>>> bitwise zerofill shift? 1 Answer

Bitwise & Operator in Shaders? 1 Answer

How do I compare two binary values in Javascript? 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