As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

Quick (hopefully) Transact SQL Question

YallYall Registered User regular
edited December 2009 in Help / Advice Forum
So can someone enlighten me as to what is happening with this bit of SQL?

(CAST(A.FIELD AS BIGINT) & 2 = 0)

I get that the field is being recast into a bigint, but is the "& 2 =0" part doing some type of base 2 comparison? Making sure it's 2^x or something?

Thanks in advance.

edit - the field in question is of type numeric (10,0)0
edit 2: it's definitely checking to see if the #2 bit is flipped. For example, when casting the field and then doing the "& 2" part, if the number is something like 256 (2^8) the equivalent is '0'. If it's 258, the result would be '2' indication the 2 bit is on.

No idea how this works though, so any clarity would be awesome.

Yall on

Posts

  • Options
    JHunzJHunz Registered User regular
    edited December 2009
    In many languages, T-SQL apparently among them, & means a bitwise AND. x & y means each bit of x is compared with an AND to each bit of y. In your case, it's filtering out the middle two of each group of four numbers, leaving 1,4,5,8,9,12,13,etc.

    Edit: Didn't see your edit when I posted. Here's what's going on, slightly more specifically:
    2 = 00000010
    254 = 11111110
    The rightmost bit has two zeros, so that's zero. The second bit in the two numbers is both 1, so that's a 1. The rest of the them have a one and a zero, leaving zero (since this is an AND operation).
    That leaves 00000010, which is 2.

    JHunz on
    bunny.gif Gamertag: JHunz. R.I.P. Mygamercard.net bunny.gif
  • Options
    YallYall Registered User regular
    edited December 2009
    JHunz wrote: »
    In many languages, T-SQL apparently among them, & means a bitwise AND. x & y means each bit of x is compared with an AND to each bit of y. In your case, it's filtering out the middle two of each group of four numbers, leaving 1,4,5,8,9,12,13,etc.

    Edit: Didn't see your edit when I posted. Here's what's going on, slightly more specifically:
    2 = 00000010
    254 = 11111110
    The rightmost bit has two zeros, so that's zero. The second bit in the two numbers is both 1, so that's a 1. The rest of the them have a one and a zero, leaving zero (since this is an AND operation).
    That leaves 00000010, which is 2.

    Thanks JHunz - I was caught up thinking '&' was the operator for concatenating strings - totally whiffed on the fact that it's a bitwise operator.

    Thanks!!!

    Yall on
Sign In or Register to comment.