Add bits to SQL Server when



  • While discussing some methods of storing information in SQL Server 2000, I came upon this little gem in the explanation for "Bitwise Operators"

    For example, you want to add 150 and 75 together, but you're interested in not only the decimal value of 225, but want to use binary arithmetic (addition of 0s and 1s). Use the bitwise AND operator (&) for this purpose.

    Ummm.... where to begin? 



  • 1 & 1 = 1

    1 + 1 = 1

    Where's the problem?

     



  • Well,

     

    150 + 75 = 225

    150 & 75 = 2

    150 | 75 = 223

    (or did I miss your dripping sarcasm?) 

    You would (almost) never be interested in both the sum and any binary operation of two numbers.  And at any rate, OR (|) is closer to + (the same if the addends are powers of 2) than AND (&).  So why the author thinks that using & to add numbers is a publishable statement is beyond me.

    Whoever wrote this clearly had no idea of what they were talking about.

    Also, why isn't left-shift and right-shift supported?
     



  • The only problem I see is the statement "addition of 0s and 1s".  This isn't technically wrong, it's just phrased poorly.  You're not actually adding the numbers together, but AND'ing them is often phrased that way.

    10010110  (150)
    01001011  (75)

    AND those and you get 10 = 2.
    OR them and you get 11011111 = 223

    what's the problem? 



  • "Use the bitwise AND operator (&) for this purpose."

     

    He wasn't actually wrong. He didn't say to *only* use the bitwise AND,  this could just be his first step.

    First he takes the AND, then he takes the OR, then he adds those together!

    He's just one of those guys who likes to take the scenic route I guess.



  • First he takes the AND, then he takes the OR, then he adds those together!

     add those together... hmm, wasn't that the whole idea in the first place?



  • Which he can do by taking the sum of the and and the or, recursively!

    So what if he gets stack errors! 



  • [quote user="cwolves"]The only problem I see is the statement "addition of 0s and 1s".  This isn't technically wrong, it's just phrased poorly.  You're not actually adding the numbers together, but AND'ing them is often phrased that way.[/quote]Actually, that is technically wrong.  "Addition" means something entirely distinct from "and" and "or" operations.


Log in to reply