C: Bitwise operators for AND, OR, XOR and compliment

I'm pretty forgetful, hence the reason for this blog, so I'm keeping this as a little reminder on how to perform these tasks in C.

AND

Result: The result is 1 only if both compared bits are 1.

result = variableA & variableB

OR

Result: The result is 1 if either compared bits are 1.

result = variableA | variableB

XOR (exclusive OR)

Result: The result is 1 only if one or the other bits are 1. If both bits are 1, the result is 0.

result = variableA ^ variableB

Compliment

This flips all the bits in a variable, so all the 1's become 0's and all the 0's become 1's. (It isn't actually an operator, but you'll have to come around to using it one day)

Result: An example would be 1001 becomes 0110.

flippedB = ~variableB

image 
C/C++ bitwise operator skill-point level up! Fuck yeah!

Sources:

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog