Last active
May 2, 2020 11:05
-
-
Save openroomxyz/dd0728347ae48fed3f519ef71a7886f1 to your computer and use it in GitHub Desktop.
Unity C# : How to set a bit in byte to 0 or 1 ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Writing | |
//Set bit to 1 | |
my_byte = my_byte | (1 << pos); // longer version, or | |
my_byte |= 1 << pos; // shorthand | |
//Set bit to 0 | |
my_byte = my_byte & ~(1 << pos); // longer version, or | |
my_byte &= ~(1 << pos); // shorthand | |
//Reading | |
var bit = (b & (1 << bitNumber-1)) != 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment