AND & OR

  • archie flockhart (10/14/2009)


    Not only a trick question, but one that is possible to get right by getting the reasoning all wrong...

    If you don't know that AND and OR operate bitwise on integers, you might assume that it works like Excel's AND() function which treats nonzero values as TRUE and zeroes as FALSE. Then it would return 1 as the value, even if all the variables were declared as INT.

    So you'd get the right answer, but for the wrong reason - and you'd go away "knowing" something about these operators that "ain't so".

    Exactly what I did :Whistling:

    Only I didn't go away "knowing" something, I came here and found out the truth 🙂

  • I believe this to be a good question with a not so good title.

    I got a chance to review the bitwise operators, and the bit data type and I think that was helpful.

    Also

    msdn.microsoft has the bit data type described as :

    "An integer data type that can take a value of 1, 0, or NULL."

    I think what they mean by "can take" is in question because we see that it will take a 56 and will translate it to a 1. I did a little excercise to see just what values you can use in an expression like "Select @bitVariable = ". It was interesting.

  • Tricky question.

    Looks like there is lots of disappointment in air today.

    SQL DBA.

  • Stop whining about "trick" questions! This one taught me to consider and be careful of automatic conversion of datatypes.

    I got it right, but would have missed it if one of the choices were "0".

    a = 120 = x'01111000'

    b = 30 = x'00011110'

    a & b = x'00011000' = 24

    d = 40 = x'00101000'

    (a&b)|d = x'00111000' = 56

    Since the result is being put to a "BIT" datatype, I mistakenly thought that the binary representation of the integer 56, x'111000', would be truncated to the last bit which is zero.

    But the conversion of any non-zero numeric to type BIT yeilds value '1', so that's the correct answer.

  • If this question has taught us anything, it's how NOT to write declare statements.

    I love binary artithmetic! Even hex to an extent, just for something different. And to have my morning fun ruined by poorly set out SQL... Yes, i know it saves space, but this isnt the 1980's anymore, bytes come by the billion (literally)!

  • I do bitwise operations fairly often, yet was thrown off at first by the & operator. My probelm is I'm also a VB programmer where it is used for string concatenation. My 2nd problem was that I should have read the newsletter earlier in the day.

    Nice QOD. The time spent criticizing trick questions would have been put to better use in just reading the question.

    Tom Garth
    Vertical Solutions[/url]

    "There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers
  • Sorry. I disagree with you about ~10% of people "seeing" the bit datatype and most people cheating.

    I do these questions in order to learn. I don't cheat at them because there is no point. I'm not competing against anyone here. I take a stab at it and when I get it wrong I usually end up reading about why and learning something new!

    Today I took a stab at 1. Why? Because I don't know anything about how bitwise and/or work. I saw the datatype was bit and I knew straight away that this could only be a zero or a one (and a null as the answer reminded me).

    That automatically ruled out all the other answers.

    I have now gone and read about what the bitwise operators do .

    There are lots of questions like that on here where you have to be careful of datatypes. Its very true in the real world too.

    Just because you didn't see the 'bit' and you got it wrong, don't assume that everyone cheats.

  • hodgy (10/14/2009)


    arghh!! I spent a few minutes playing with a spreadsheet to mock up the bitwise AND and OR functions (a happy little diversion) and i missed the data type gotcha!

    ah well...

    That's why I did not play with bytes early in the morning and found the bit trick 🙂

    Kindest Regards,

    Damian Widera
    SQL Server MVP,
    MCT, MCSE Data Platform, MCSD.NET

  • cengland0 (10/14/2009)


    I would agree except the question did not say, Why doesn't this produce the expected result of 56? It was not a troubleshooting question but a question about the operators AND and OR.

    I don't think the question said anything other than "What is the output of the above"?

    I agree that the title could have been changed so that it wasn't misleading but still didn't give the game away. "Bitwise" may have been more appropriate?

    All I'm saying is I can see where the question poser is coming from and I quite liked it as it translates well to real world situations, and it's a lesson that is normally learned the hard way. Put it this way, if a dev came to you and asked what was going on, and the variable declarations were nowhere near the code in question, and they'd been mulling this over for a while and specifically asked you to look at their AND/OR operator logic as they were so far up the wrong tree they couldn't see the way back down, how many people would first say, "right, what are the datatypes you're using here?"

    - I'm pretty sure I would get suckered into the same staring, head scratching routine and it could take quite a while to get to the bottom of it...

    --------
    [font="Tahoma"]I love deadlines. I like the whooshing sound they make as they fly by. -Douglas Adams[/font]

  • OOOOHhhhhh!!! Dang! Ya got me! :laugh:

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • I'm with Rob, I got this one right (for once!) but often it's these little details that let me down in these questions. Different data type can completely change what is going on. The name of the question is very much like the scenario Rob mentioned too. Sometimes you take a look at a problem with preconceptions, and it can throw you for a while because everything in the logic is screaming at you that it's correct and yet... well I thought it was a good question because it is a lesson not just in bit fields but in taking in all the details and not taking anything for granted.

  • IMHO this is a completely fair question. We are after all DBA's and Developers, and these little details matter a lot in our profession. 🙂

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • This is not a trick question but one of fundamentals and/or reading the entire chunk of code. If you knew what a bit was then you would know there was only one logical answer.

  • Personally I can't see the cause for complaint. I didn't understand how the bitwise & and | operators worked so I thought I was toast, then I noticed the datatype and realised there could only be 1 (no pun intended) correct answer. Having got it right I went on to read up on how the & and | operators work. I think this question better suited those with sketchy knowledge of bitwise operations since we had not choice but to look for an alternative method of solving the question straight away. I liked the question, I got it right and it educated me too. Job well done 😀

  • A good question. Maybe even a very good question.

    Maybe the title of the question could be phrased so that it makes it clear that what is being tested is knowledge of how implicit conversion from int to bit works. But that wouldn't be quite as much fun, now would it? and it would have rather given the game away.

    Personally I quite like trick questions, since they are like real life - most of the time all those frustrating hours spent wondering how that code could possibly do this are because we've missed some important bit of information that was right under our noses.

    I dislike questions where the wrong anser is given as right - but this question was not one of those.

    The questions I really hate are those where an answer is given which could be right if the question had mentioned all the constraints that the putter of the question has assumed but not stated, but without those constrains is a dangerous wrong answes that could easily lead to grief if applied in real life. This question was actuallyy just the opposite of that, demonstrating to people that if they assume a constraint (in this case "all variables are typed as int") that is not required by the problem (even worse one which is specifically contradicted by the terms of the problem) they will come to grief. It's a lesson everyone needs to learn. It also demonstrates that looking at all the constraints can give you a shortcut to the solution (and that's another useful lesson), several people commenting that this "this is a bit" constraint led them straight to the right answer.

    It seems from the number of unhappy responses that rather a lot of people have learned neither of those lessons.

    Tom

Viewing 15 posts - 16 through 30 (of 30 total)

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