Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««1234»»»

AND & OR Expand / Collapse
Author
Message
Posted Wednesday, October 14, 2009 5:37 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Friday, May 24, 2013 5:20 AM
Points: 660, Visits: 558
Very good question in my book.

All too often you get guys spending hours debugging code and scratching heads when all along it was just someone chuffed up a datatype previously.

The devil is in the details!


--------
I love deadlines. I like the whooshing sound they make as they fly by. -Douglas Adams
Post #802650
Posted Wednesday, October 14, 2009 6:00 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 24, 2013 9:59 PM
Points: 1,354, Visits: 1,299
Rob Goddard (10/14/2009)
Very good question in my book.

All too often you get guys spending hours debugging code and scratching heads when all along it was just someone chuffed up a datatype previously.

The devil is in the details!


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.
Post #802656
Posted Wednesday, October 14, 2009 7:37 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, July 22, 2011 4:13 AM
Points: 1,149, Visits: 603
I agree with dave.farmer.
This wasn't a SQL knowledge question but more of a practical joke
Post #802715
Posted Wednesday, October 14, 2009 7:49 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 7:20 AM
Points: 1,534, Visits: 2,261
Goodness! People are whiny today!

The Redneck DBA
Post #802731
Posted Wednesday, October 14, 2009 8:09 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 10:52 AM
Points: 1,366, Visits: 1,729
Well, this question does provide one good bit of information.

Based on the answer %s at the time of of writing, I estimate than somewhere between 66% and 74%* of the respondents to the question simply copied the code into a query processor and ran it to get their answer.

Cheating, or simply using available resources?

* 74% answered correctly, and I'm willing to accept that ~10% of those actually saw the trick. I rounded up to give them the benefit of the doubt.
Post #802764
Posted Wednesday, October 14, 2009 8:48 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 5:55 AM
Points: 1,813, Visits: 6,586
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

Only I didn't go away "knowing" something, I came here and found out the truth
Post #802807
Posted Wednesday, October 14, 2009 10:04 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, August 1, 2014 5:28 AM
Points: 183, Visits: 215
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.
Post #802900
Posted Wednesday, October 14, 2009 10:59 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, July 17, 2014 10:56 AM
Points: 3,924, Visits: 1,607
Tricky question.

Looks like there is lots of disappointment in air today.


SQL DBA.
Post #802947
Posted Wednesday, October 14, 2009 12:38 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, August 28, 2014 8:53 PM
Points: 1,388, Visits: 3,039
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.
Post #803023
Posted Wednesday, October 14, 2009 3:27 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, February 21, 2013 3:46 PM
Points: 354, Visits: 170
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)!
Post #803142
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse