T-SQL SQL 2008

  • Comments posted to this topic are about the item T-SQL SQL 2008

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • This was removed by the editor as SPAM

  • Personally i would rather see them not being used. You dont really gain anything by using them. Only makes the code harder to read. Saving a few keypresses vs loosing readability is in my mind a bad trade.

    Well i would think that you wont gain any performance on it anyway. SQL still have to do the math. But havent been able to test... no SQL2008 🙁

  • += or *= are quite easy, but |= or even ^= wanted a little thinking on bitwise arithmetics.

    Nice equestion.



    See, understand, learn, try, use efficient
    © Dr.Plch

  • stewartc-708166 (6/20/2010)


    These operators ... are definately underused.

    Maybe because they are not compatible with SQL Server 2005 and below?

  • Good question, but the explanation is lacking. I think that the +=, -=, etc operators are generally even better known than the bitwise operators used here (even if they were done in SQL-2005 compatible format such as SET @b-2 = @b-2 & 1).

    The explanation that should have been there is:

    & - bitwise AND. Convert operators to binary, do a bit-by-bit AND operation (result bit is 1 if both input bits are 1), then convert the result back to the result data type. In the example here, 5 AND 1 becomes:

    5 = 0101

    1 = 0001

    ---- &

    0001 = 1

    | - bitwise OR. Convert operators to binary, do a bit-by-bit OR operation (result bit is 1 if either input bit is 1), then convert the result back to the result data type. In the example here, 5 OR 1 becomes:

    5 = 0101

    1 = 0001

    ---- |

    0101 = 5

    ^ - bitwise exclusive OR. Convert operators to binary, do a bit-by-bit exclusive OR operation (result bit is 1 if exactly one bit is 1), then convert the result back to the result data type. In the example here, 5 OR 1 becomes:

    5 = 0101

    1 = 0001

    ---- ^

    0100 = 4

    % - modulo. I assume the readers here to be familiar with this operator.

    (EDIT - attempt to edit formatting of leading spaces)


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Picture didn't appear on the e-mail--had to come here before it showed up. That happen to anyone else?

  • I had to guess because I didn't know the ^= operator but I knew all the others. I was lucky that the other three were specific enough that I guessed correctly.

    So why doesn't the Explanation discuss the ^= operator? What is that?

  • I agree with Hugo - I knew the modulus answer, but had no idea what the others were. So got it correct as only one option had the right value for @e 🙂

  • Toreador (6/21/2010)


    I agree with Hugo - I knew the modulus answer, but had no idea what the others were. So got it correct as only one option had the right value for @e 🙂

    Same here, I was instantly 100% sure about the % and low and behold, I didn't have to think any further :w00t:.

  • Yes, that is how it appeared in my e-mail.

  • Hugo Kornelis (6/21/2010)


    Good question, but the explanation is lacking. I think that the +=, -=, etc operators are generally even better known than the bitwise operators used here (even if they were done in SQL-2005 compatible format such as SET @b-2 = @b-2 & 1).

    The explanation that should have been there is:

    & - bitwise AND. Convert operators to binary, do a bit-by-bit AND operation (result bit is 1 if both input bits are 1), then convert the result back to the result data type. In the example here, 5 AND 1 becomes:

    5 = 0101

    1 = 0001

    ---- &

    0001 = 1

    | - bitwise OR. Convert operators to binary, do a bit-by-bit OR operation (result bit is 1 if either input bit is 1), then convert the result back to the result data type. In the example here, 5 OR 1 becomes:

    5 = 0101

    1 = 0001

    ---- |

    0101 = 5

    ^ - bitwise exclusive OR. Convert operators to binary, do a bit-by-bit exclusive OR operation (result bit is 1 if exactly one bit is 1), then convert the result back to the result data type. In the example here, 5 OR 1 becomes:

    5 = 0101

    1 = 0001

    ---- ^

    0100 = 4

    % - modulo. I assume the readers here to be familiar with this operator.

    (EDIT - attempt to edit formatting of leading spaces)

    thanks! that makes it all so much more clear. the | operator was confusing me since 5|2 and 5|3 both equal 7...

  • I have'nt had to think about bitwise operations since the mid '80s so my brain is mushy now. But the modulo gave it away.

    Thanks for the question.

    Converting oxygen into carbon dioxide, since 1955.
  • Thanks Ron. I learned something more from this question.

    Thanks Hugo for explaining it like that.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • paul.knibbs (6/21/2010)


    Picture didn't appear on the e-mail--had to come here before it showed up. That happen to anyone else?

    Yes, that happened to me, too.

    - webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

Viewing 15 posts - 1 through 15 (of 27 total)

You must be logged in to reply to this topic. Login to reply