RSS

SQL NAND, XNOR

14 Oct

In SQL there are some build in bitwise operators like you can see in the following table

& AND
| OR
^ XOR
~ NOT

But what sometimes really is missing is NAND and XNOR. But you can easily build them by your own.
Those two missing NAND and XNOR can be build as follows:

DECLARE @a bit = 0;
DECLARE @b bit = 1;

--NAND
SELECT @a & @b ^ 1;

--XNOR
SELECT  @a ^ @b ^ 1

In order to make it a bit easier you can create two User Defined Functions.

--NAND
CREATE FUNCTION NAND (@a bit, @b bit) RETURNS bit AS 
BEGIN
    RETURN @a & @b ^ 1
END

--XNOR
CREATE FUNCTION XNOR (@a bit, @b bit) RETURNS bit AS 
BEGIN
    RETURN @a ^ @b ^ 1
END
Advertisements
 
2 Comments

Posted by on October 14, 2011 in SQL

 

Tags:

2 responses to “SQL NAND, XNOR

  1. lukaseder

    October 19, 2011 at 19:00

    Nice addition to SQL. True, too bad they’re not frequently available from modern RDBMS…

     

Leave a Reply

Please log in using one of these methods to post your comment:

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 )

Google+ photo

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

Connecting to %s

 
%d bloggers like this: