Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitwise Manipulation Functions [duplicate]

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?

like image 373
Connor Black Avatar asked Aug 09 '12 13:08

Connor Black


2 Answers

Set uses or

Unset uses And

Flip uses Xor

this was already answered here: How do you set, clear, and toggle a single bit?

like image 169
Gir Avatar answered Sep 30 '22 17:09

Gir


You are right for the first one, but for Unset() you should use an & with 0 in that bit

like image 24
Dan F Avatar answered Sep 30 '22 18:09

Dan F