October 8, 2007 at 2:12 am
Comments posted to this topic are about the item Convert integers from decimal to binary display.
May 18, 2011 at 7:48 am
Here is the result of my Convert-Loop-To-Tally exercise:
/* Convert signed integer to binary notation: */
DECLARE @BINval CHAR(39) = ''-- Format: 'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx'
,@INTval INT = 2147483647-- Positive limit
--,@INTval INT = -2147483648-- Negative limit
SELECT @BINval = CASE WHEN @INTval & power(2, N) > 0 THEN '1' ELSE '0' END/* Determine bit value */
+ CASE WHEN N%4 = 0 THEN ' ' ELSE '' END/* Blks of 4 bits each */
+ @BINval
FROM [dbo].[Tally] WITH (NOLOCK)
WHERE N BETWEEN 0 AND 30 /* Set all but sign bit */
--ORDER BY N /* My tally tbl PK is N */
-- Add sign bit
SELECT @BINval = CASE WHEN @INTval >= 0 THEN '0' ELSE '1' END
+ @BINval
-- Voila...
SELECT @BINval
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy