Flip that BIT

Another day another “how have I never come across this problem before”.  So todays challenge is to essentially execute a stored procedure which will allow us to alternate a BIT value. So if the value is 0 make it 1, if it is 1 make it 0.

After a little goggling it seems utilising the Bitwise XOR operator is the best method.

Script:

   1: DECLARE @Bit BIT= 1

   2: SELECT CAST(@Bit^1 AS bit)

While i’m not the best person to preach about the benefits of using this operator it got the job done in this instance, for more information start here:

https://msdn.microsoft.com/en-us/library/ms190277.aspx

Advertisements

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 )

Google+ photo

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

Connecting to %s