T-SQL: Bitwise Arithmetic

Please see my other Database Development articles.

Using bits as flags.

Recording user settings, often as “categories” represents significant persistence within many applications.
Storing all these values within separate variables would waste valuable memory as well violate the Don’t Repeat Yourself (DRY) principle, producing hard-to-maintain code as well as tedious to understand.
We’ll begin our examples by simply creating two values and setting their values.


Bitwise AND


Bitwise OR

bitwise_or_code bitwise_or_results

Bitwise XOR


Bitwise NOT


Reversing the bits for ‘1’ produce ‘2’ and doing the same for ‘3’ product ‘4’.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s