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 MC HALO · May 15, 2011 at 04:00 AM · operatorbitwiseand-operator

What is the difference between these ?

hey all,

i just wanted to know what is the difference between these | and ||

i know the double lines are Or what is the single ?

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

1 Reply

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

Answer by Owen-Reynolds · May 15, 2011 at 05:48 AM

A single | or & is a bitwise operator, for binary-type math. For example 6 in binary is 110 and 12 in binary is 1100. 6|12 means to use all the bits in 6 OR in 12, which is 1110, or 14.

Layers are stored as binary, putting a 1 in the slot for that layer. Layer 0 is 00000001, layer 1 is 00000010 layer 2 is 00000100, and so on. This is why there are layers 0-31. Computer numbers are 32-bits long. A layer is always 31 zeros and a 1 in the slot for that layer.)

If layers were stored as normal numbers, and you wanted to skip 2, 3 and 6 for a raycast, you'd have to write if(layer!=2 && layer!=3 && layer!=6). Instead, you write the layers you want to skip using the binary layer representation:

76543210

A 00001000 <-- I am on layer 3 B 01001100 <-- skip layers are 6, 3 and 2 --------
A&B 00001000 <-- a 1 in slots where both are 1's A|B 01001100 <-- a 1 in slots where either are 1's

Using (layer&skipLayers) very quickly checks whether the current layer is 2, 3 or 6 (a result of not 0 means you are in the skipLayers.)

If you using using 01001100 to represent skipping 6,3 and 2, and want to add layer 5, you can write: skipLayers=skipLayers|(1<<5); to get 01101100. The double-lessThan symbol is a bitshift. If means to turn 1 into 100000 by shifting it five spaces over.

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 MC HALO · May 15, 2011 at 05:56 AM 0
Share

cheers :) you are great :)

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

No one has followed this question yet.

Related Questions

how do i use 'And' and 'Or' in UnityScript 2 Answers

Where did I go wrong (logical operator or tagging) 1 Answer

Bitwise Operators 3 Answers

Bitwise & Operator in Shaders? 1 Answer

Unity/Javascript Operators 2 Answers


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