Possible Duplicate:
How do you set, clear and toggle a single bit in C?
I'm studying for an upcoming final and I want to verify some questions from the study guide.
Some context:
The Set()
function sets a bit in a byte to 1
The Unset()
function sets a bit in a byte to 0
The Flip()
function "flips" the bit to the opposite of what it is
So some kid in our class took it upon himself to answer the study guide questions but I've already found some errors, and these answers sound fishy. Here's what he said:
Which operation is used for the Set? the or operator |
Which operation is used for the Unset? Xor operator ^ Done twice
Which operation is used for the Flip? Xor operator ^
Are these the correct bitwise operators to implement in the functions I've described above?
Set uses or
Unset uses And
Flip uses Xor
this was already answered here: How do you set, clear, and toggle a single bit?
You are right for the first one, but for Unset()
you should use an &
with 0
in that bit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With