SELECT Number,
1 - SIGN(Number & (Number - 1))
FROM master..spt_values
WHERE Type = 'P'
AND Number > 0
Other way is
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
(
@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
Source Click Here.
Post a Comment