Stephen
Stephen

Reputation:

SQL bitwise operations

I'm hooked on bitwise for a new security paradigm I'm creating called VMAC. Variable Matrix Access Control. I want to do logical implication on to bit strings. Just trying to avoid echoeureka (reinventing the wheel).

  1. Before I reinvent the wheel, is there an established shortcut to emulate ⇒ (logical implication) using AND and OR and NOT or other basic SQL binary operators?

  2. XNOR would allow me to cut ⇒ emulation down to four operations: NOT, XOR, OR, AND. But it is not widely available. Any known shortcuts to XNOR? I was thinking of something like AND operation on operands plus NOTted operands off the top of my head.

  3. Any comments on efficiency achieved by implementation of bitwise data structures on a 64-bit platform, or on the speed of multi-threaded apps using concurrent threads operating on word-size segments of larger data object?

(Sorry, I'm not a computer scientist)

Upvotes: 2

Views: 2249

Answers (4)

Fernando Cuadrado
Fernando Cuadrado

Reputation: 11

The definition of XNOR is A*B + !A*!B

Therefore in SQL this could be implemented like this:

DECLARE @A AS BIT;
DECLARE @B AS BIT;

(@A & @B) | (~@A & ~@B)

An alternate notation can be found here: http://michaelmairegger.wordpress.com/2011/10/14/sql-nand-xnor/

Upvotes: 1

Tom Ritter
Tom Ritter

Reputation: 101330

Any comments (3) on efficiency achieved by implementation of bitwise data structures 0n 64-bit platform? Speed on multi-threaded apps using concurrent threads operating on word-size segments of larger data object?

I personally haven't given it much thought, but Raymond Chen has some comments.

These are just a few things to take into account when considering whether you should change your fields to bitfields. Sure, bitfields save data memory, but you have to balance it against the cost in code size, debuggability, and reduced multithreading. If your class is going to be instantiated only a few times (and by "a few" I'm thinking less than a few thousand times), then these costs most likely exceed the savings.

Upvotes: 1

Eclipse
Eclipse

Reputation: 45493

If you're manipulating a bit string then

x -> y 

can be expressed in C/C++ (or SQL) as:

~x | y

As far as speed goes, bitwise operators on a single machine word are incredibly fast since they are implemented in a single arithmetic CPU instruction. The performance hit of doing the math should be nearly neglegible in relation to the work of actually retrieving the data.

Upvotes: 1

ttarchala
ttarchala

Reputation: 4557

if A then B is logically equivalent to (not A) or B

Upvotes: 3

Related Questions