SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


AND & OR


AND & OR

Author
Message
Rob Goddard
Rob Goddard
SSC Eights!
SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)

Group: General Forum Members
Points: 998 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
cengland0
cengland0
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2256 Visits: 1300
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.
Seth Lynch
Seth Lynch
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1489 Visits: 603
I agree with dave.farmer.
This wasn't a SQL knowledge question but more of a practical joke
TheRedneckDBA
TheRedneckDBA
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4310 Visits: 2614
Goodness! People are whiny today!

The Redneck DBA
sknox
sknox
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4091 Visits: 2934
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.
Toreador
Toreador
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3865 Visits: 8126
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 :-)
Robert J Ryan
Robert J Ryan
SSC-Enthusiastic
SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)

Group: General Forum Members
Points: 183 Visits: 267
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.
SanjayAttray
SanjayAttray
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5771 Visits: 1619
Tricky question.

Looks like there is lots of disappointment in air today.

SQL DBA.
john.arnott
john.arnott
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3652 Visits: 3059
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.
awoodhouse
awoodhouse
Old Hand
Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)

Group: General Forum Members
Points: 360 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)!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search