Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Mask (computing)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Toggling bit values=== So far the article has covered how to turn bits on and turn bits off, but not both at once. Sometimes it does not really matter what the value is, but it must be made the opposite of what it currently is. This can be achieved using the [[Exclusive or|<code>XOR</code> (exclusive or)]] operation. <code>XOR</code> returns <code>1</code> [[if and only if]] an [[odd number]] of bits are <code>1</code>. Therefore, if two corresponding bits are <code>1</code>, the result will be a <code>0</code>, but if only one of them is <code>1</code>, the result will be <code>1</code>. Therefore inversion of the values of bits is done by <code>XOR</code>ing them with a <code>1</code>. If the original bit was <code>1</code>, it returns <code>1 XOR 1 = 0</code>. If the original bit was <code>0</code> it returns <code>0 XOR 1 = 1</code>. Also note that <code>XOR</code> masking is bit-safe, meaning that it will not affect unmasked bits because <code>Y XOR 0 = Y</code>, just like an <code>OR</code>. Example: Toggling bit values 10011101 10010101 XOR '''00001111 11111111''' = 10010010 01101010 To write arbitrary 1s and 0s to a subset of bits, first write 0s to that subset, then set the high bits: register = (register & ~bitmask) | value;
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)