Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SSIS runs in BIDS but not with SQL AgentSQL Server 2008 Service Pack 2 Released »
    comments

    This question came up today.

    in c# i have a:

    a = !a
    (if false makes it true, if true makes it false)

    in sql i want to do the same with a BIT variable

    In a language like C# you can use ! for a bool to make it become true if it was false and vice versa

    So if you run this

    1. bool a = true;
    2. a = !a;
    3.  
    4. Console.WriteLine(a.ToString());
    5. a = !a;
    6. Console.WriteLine(a.ToString());
    7. Console.ReadLine();

    The output will be

    False
    True

    How can you do this in SQL Server? It is pretty easy, take a look

    1. select ~ CONVERT(bit,0)
    2. select ~ CONVERT(bit,1)

    The ~ symbol is the Bitwise NOT operator, here is what books on line has to say about the Bitwise NOT operator.

    The ~ bitwise operator performs a bitwise logical NOT for the expression, taking each bit in turn. If expression has a value of 0, the bits in the result set are set to 1; otherwise, the bit in the result is cleared to a value of 0. In other words, ones are changed to zeros and zeros are changed to ones.

    Let's take a look at another example. What do you think will happen here?

    1. select ~ CONVERT(tinyint,0)
    2. select ~ CONVERT(tinyint,1)

    That actually returns
    255
    254

    Why is that? Here is a simple explanation

    With a tinyint possible values are between 0 and 255, when you have 0 all bits are turned off, when you flip it, you turn all bits on and you get 255

    00000000	= 0   ( 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0)
    11111111	= 255 (1 + 2 + 4 + 8 + 16 + 32 + 64 + 128)

    When you have 1 all bits are turned off except for the first bit, when you flip it you turn all bits on except for the first bit

    00000001	=  1 ( 1 + 0 + 0 + 0 + 0 + 0 + 0 + 0)
    11111110	= 254 (0 + 2 + 4 + 8 + 16 + 32 + 64 + 128)

    If you want to use tinyint or int you can use the following code

    1. select  CONVERT(tinyint,0) ^ 1 as [tinyint], 0^1 as [int]
    2. select  CONVERT(tinyint,1) ^ 1 as [tinyint], 1^1 as [int]

    The ^ operator is the Bitwise Exclusive OR operator. Here is what books on line has to say about Bitwise Exclusive OR

    The ^ bitwise operator performs a bitwise logical exclusive OR between the two expressions, taking each corresponding bit for both expressions. The bits in the result are set to 1 if either (but not both) bits (for the current bit being resolved) in the input expressions have a value of 1. If both bits are 0 or both bits are 1, the bit in the result is cleared to a value of 0.

    Let's take a closer look

    The bits in the result are set to 1 if either (but not both) bits (for the current bit being resolved) in the input expressions have a value of 1.

    0 ^ 1
    00000000
    00000001
    --------
    00000001

    If both bits are 0 or both bits are 1, the bit in the result is cleared to a value of 0.

    1^1
    00000001
    00000001
    --------
    00000000

    So in the end if you want to flip a bit use the Bitwise NOT operator

    1. select ~ CONVERT(bit,0)
    2. select ~ CONVERT(bit,1)




    *** Remember, if you have a SQL related question, try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    2296 views
    Instapaper

    5 comments

    Comment from: Erik [Member] Email
    Erik If the output is going right back into a bit column you can just do `1 - BitCol` and it will work fine, too.
    09/29/10 @ 13:44
    Comment from: SQLDenis [Member] Email
    SQLDenis Nice...I was waiting for your comment :-)

    yes duh

    1-1 = 0
    1-0 = 1
    09/29/10 @ 13:46
    Comment from: Erik [Member] Email
    Erik But your way with the bit operator ~ might be faster and is more explicitly correct since the data type isn't converted.
    09/29/10 @ 15:47
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky I should have posted it too as I answered this same question twice (using bitwise XOR operator). The second time I had to dig for my original answer as I forgot how I solved it
    09/29/10 @ 16:06
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky Related thread http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/4e8a6e05-ee5c-4453-9863-2a644a3660c1
    09/29/10 @ 16:37

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)