isPowerOf2

Last Post 09 Aug 2008 12:47 AM by SwePeso. 0 Replies.
AddThis - Bookmarking and Sharing Button
Author Messages
SwePeso
New Member
New Member

--
09 Aug 2008 12:47 AM
SELECT Number,
LOG(Number) / LOG(2)
FROM master..spt_values
WHERE Type = 'P'
AND Number > 0

CREATE FUNCTION dbo.isPowerOf2
(
@i INT
)
RETURNS BIT
AS
BEGIN
RETURN CASE
WHEN @i IN (1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824) THEN 1
ELSE 0
END
END

CREATE FUNCTION dbo.isPowerOf2
(
@i INT
)
RETURNS BIT
AS
BEGIN
DECLARE @x FLOAT

SET @x = LOG(Number) / LOG(2)

RETURN CASE
WHEN FLOOR(@x) = CEILING(@x) THEN 1
ELSE 0
END
END



Acceptable Use Policy
---