Friday, December 26, 2025

Bitwise operator

 DECLARE @phoneInd INT

DECLARE @home BIT

,@homeFax BIT

,@mobile BIT

,@office BIT

,@officeFax BIT

,@tollfreeOffice BIT

,@tollfreeFax BIT


-- turn on indicators 1=on 0=off

SELECT @home = 0

,@homeFax = 0

,@mobile = 1

,@office = 1

,@officeFax = 0

,@tollfreeOffice = 1

,@tollfreeFax = 0


SET @phoneInd = POWER(2 * @home, 1) 

+ POWER(2 * @homeFax, 2) 

+ POWER(2 * @mobile, 3) 

+ POWER(2 * @office, 4) 

+ POWER(2 * @officeFax, 5) 

+ POWER(2 * @tollfreeOffice, 6) 

+ POWER(2 * @tollfreeFax, 7)


SELECT @phoneInd AS phoneInd

,iIF((2 & @phoneInd) = 2, 1, 0) AS 'Has Home'

,iIF((4 & @phoneInd) = 4, 1, 0) AS 'Has Home Fax'

,iIF((8 & @phoneInd) = 8, 1, 0) AS 'Has Mobile'

,iIF((16 & @phoneInd) = 16, 1, 0) AS 'Has Office'

,iIF((32 & @phoneInd) = 32, 1, 0) AS 'Has Office Fax'

,iIF((64 & @phoneInd) = 64, 1, 0) AS 'Has Toll Free Office'

,iIF((128 & @phoneInd) = 128, 1, 0) AS 'Has Toll Free Fax'


No comments:

Post a Comment

Bitwise operator

  DECLARE @phoneInd INT DECLARE @home BIT ,@homeFax BIT ,@mobile BIT ,@office BIT ,@officeFax BIT ,@tollfreeOffice BIT ,@tollfr...