Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Chimera3D · Jul 11, 2015 at 01:47 AM · shaderintegeroperatorbitwise

Bitwise & Operator in Shaders?

My question is how can I emulate a bitwise & operator in a shader? I have a line of code that is basically:

int a = i & 255;

(where i can become a relatively large number) in a C# script and I need it to work identically in a shader. How can I emulate this operator in a shader?

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 _Gkxd · Jul 11, 2015 at 06:07 AM

The last time I checked, bitwise operators aren't available in CG. See the description of table 3-2 on this page:

Table 3-2 presents the complete list of operators, along with their precedence, associativity, and usage. Operators marked with a reverse highlight are currently reserved. However, no existing Cg profiles support these reserved operators because current graphics hardware does not support bitwise integer operations.

The bitwise and (&) with 255 is equivalent to modding (%) with 256 though, so you can use that instead.

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 Chimera3D · Jul 11, 2015 at 11:39 AM 0
Share

Well I did say "emulate" - I knew they weren't available so I was wondering how I could replicate their functionality... thanks!

avatar image Chimera3D · Jul 11, 2015 at 09:43 PM 0
Share

After quickly testing this I realized that if "a" (in the example in the OP) is negative then modding and the bitwise & are no longer returning the same number... how can I fix that?

avatar image _Gkxd · Jul 12, 2015 at 02:08 PM 1
Share

Ah, I didn't think that you'd have negatives.

x = ((x % 256) + 256) % 256 would work with negatives. If you know that your number doesn't go below -256, then you can use x = (x + 256) % 256 ins$$anonymous$$d.

You might also be able to make the number unsigned... it seems that they exist in Cg: http://http.developer.nvidia.com/Cg/Cg_language.html

Actually, now that I think about it, you can probably just use unsigned chars and completely ignore bitwise operators/modding, since integer overflow for unsigned chars would keep the value between 0 and 255.

avatar image Chimera3D · Jul 12, 2015 at 09:57 PM 0
Share

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.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

What is the difference between these ? 1 Answer

Shader setting integer 1 Answer

Can't use "Integer" type in Shader property? 0 Answers

Bitwise Operators 3 Answers

Bitwise operations in hlsl, is there an alternative method? 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