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_begin_codebitwise_begin_results

Bitwise AND

bitwise_and_codebitwise_and_results

Bitwise OR

bitwise_or_code bitwise_or_results

Bitwise XOR

bitwise_xor_codebitwise_xor_results

Bitwise NOT

bitwise_not_codebitwise_not_results

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

bitwise_not_example1bitwise_not_example3

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