April 22, 2015 at 5:36 pm
Comments posted to this topic are about the item Function to count set bits in a signed integer
April 28, 2015 at 12:14 am
Any particular reason for
~CONVERT(bigint, -9223372036854775808)
instead of
CONVERT(bigint, 9223372036854775807)
My Italian keyboard doesn't have this famous tilde characters! :crying:
Oh crap, forgot all about copy/paste. 🙂
April 28, 2015 at 1:49 am
~ is the T-SQL's bitwise NOT operator. It inverts all the bits in an integer.
It's used so the right-most bit, which is set to 1 two's compliment signed integer that has a negative value, can be counted.
In binary -9223372036854775808 = 1000000000000000000000000000000000000000000000000000000000000000 (1 with 63 trailing zeros), when you bitwize NOT this number you get 0111111111111111111111111111111111111111111111111111111111111111 (0 with 63 trailing 1's). When we AND this with the bigint parameter to the function it removes the leading 1 making it a positive number.
April 28, 2015 at 1:59 am
Jonathan AC Roberts (4/28/2015)
~ is the T-SQL's bitwise NOT operator. It inverts all the bits in an integer.It's used so the right-most bit, which is set to 1 two's compliment signed integer that has a negative value, can be counted.
In binary -9223372036854775808 = 1000000000000000000000000000000000000000000000000000000000000000 (1 with 63 trailing zeros), when you bitwize NOT this number you get 0111111111111111111111111111111111111111111111111111111111111111 (0 with 63 trailing 1's). When we AND this with the bigint parameter to the function it removes the leading 1 making it a positive number.
What I was trying to say was that
~CONVERT(bigint, -9223372036854775808)
and
CONVERT(bigint, 9223372036854775807)
return the same value.
April 28, 2015 at 2:58 am
Michael Meierruth (4/28/2015)
Jonathan AC Roberts (4/28/2015)
~ is the T-SQL's bitwise NOT operator. It inverts all the bits in an integer.It's used so the right-most bit, which is set to 1 two's compliment signed integer that has a negative value, can be counted.
In binary -9223372036854775808 = 1000000000000000000000000000000000000000000000000000000000000000 (1 with 63 trailing zeros), when you bitwize NOT this number you get 0111111111111111111111111111111111111111111111111111111111111111 (0 with 63 trailing 1's). When we AND this with the bigint parameter to the function it removes the leading 1 making it a positive number.
What I was trying to say was that
~CONVERT(bigint, -9223372036854775808)
and
CONVERT(bigint, 9223372036854775807)
return the same value.
Yes, both give the answer of (2^63) - 1
April 28, 2015 at 7:37 am
Not sure I will ever use it in my every day work. Interesting nevertheless and I'm sure someone will make use of it. Thanks for the exercise.
April 28, 2015 at 8:27 am
Iwas Bornready (4/28/2015)
Not sure I will ever use it in my every day work. Interesting nevertheless and I'm sure someone will make use of it. Thanks for the exercise.
In fact, this will lead us into the question of what in the real world gets represented as positional bits in an Int or BigInt column and where counting the number of bits turned on becomes meaningful/useful.
April 28, 2015 at 8:48 am
Well it probably is bit of an obscure/esoteric thing to do but one of the most efficient ways of storing items that are switched is in a bit string within an integer. For Quine-McCluskey minimisation (the reason I wrote this function) part of the algorithm involves counting the set bits in an integer (the minterms). This information is then used to group the integers into sets by how many 1's they contain.
April 28, 2015 at 1:29 pm
Unless I typed over the comment by accident there is an error in the comments since BitsIn returns 4 for an input of 15 while the comment lists 3 as the result.
HTH -- Mark D Powell --
April 28, 2015 at 3:08 pm
Mark D Powell (4/28/2015)
Unless I typed over the comment by accident there is an error in the comments since BitsIn returns 4 for an input of 15 while the comment lists 3 as the result.HTH -- Mark D Powell --
Yes, well spotted, that's an error in the comment. Fortunately the error is not in the code as 15 has four bits set as its binary representation is 1111
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply