Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Shifty Bits

Author  Topic 

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2003-08-28 : 09:33:57
Say I have a @vb varbinary(12).

Since the bitwise operators only work with INTs, how can I turn the 32 bit on?

Granted, I could select @vb = convert(varbinary(4),0) + convert(varbinary(4),power(2,1)). But I'm looking for a way to do with without the concatenation, since I'd like to be able to turn on bits 2, 5, 33 and 80 all in shot.



Jay White
{0}

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-08-28 : 09:42:45
hmmm.... tough one.

I was actually surprised to find that adding two binary datatypes results in a concatenation, not addition ... maybe it's just me, but I had thought they would work more like numbers than strings.

- Jeff
Go to Top of Page

Onamuji
Aged Yak Warrior

504 Posts

Posted - 2003-08-28 : 11:22:59
Have we forgot our basic binary manipulation lessons?

DECLARE @b VARBINARY(2)

SET @b = 0
SET @b = @b + POWER(2,0) -- turn on the first bit
SET @b = @b + POWER(2,3) -- turn on the fourth bit
SET @b = @b + POWER(2,7) + POWER(2,15) -- turn on the eighth and sixteenth bits

PRINT CAST(POWER(2,0) + POWER(2,3) + POWER(2,7) + POWER(2,15) AS VARBINARY(2))
PRINT @b
PRINT ''

PRINT POWER(2,0) + POWER(2,3) + POWER(2,7) + POWER(2,15)
PRINT CAST(@b AS INT)
PRINT ''

SET @b = 0
SET @b = @b | POWER(2,0) | POWER(2,3) | POWER(2,7) | POWER(2,15)
SET @b = @b - (@b & POWER(2,15))

PRINT @b
PRINT CAST(@b AS INT)
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-08-28 : 11:35:35
but try that going past the 31st bit .... i.e., do what Jay mentioned: set bits 2,5,33 and 80 of a varbinary(12).

- Jeff
Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2003-08-28 : 11:39:20
Exactly, Jeff ... the bitwise operators will only work with Ints, so power(2,30) is the biggest you can go without doing string manipulation on the varbinary. That's why I'm looking for something like the C++ >> operator ...

Jay White
{0}
Go to Top of Page

Onamuji
Aged Yak Warrior

504 Posts

Posted - 2003-08-28 : 11:52:18
Forgive my quick reply. This shouldn't be as large of a problem as it is now in Yukon - I hope.
Go to Top of Page

Lavos
Posting Yak Master

200 Posts

Posted - 2003-09-09 : 19:30:59
Yeah, when I was learning how to use SET CONTEXT_INFO the whole binary data type confused the hell out of me until I picked up that binary was just like the character data types except it used bytes instead of characters.

Now that I've been layed off, I might have to write some genreal purpose functions to do binary manipulation and add it to the t-sql scripts area for fun.


----------------------
"O Theos mou! Echo ten labrida en te mou kephale!"

"Where theres a will, theres a kludge." - Ken Henderson
Go to Top of Page
   

- Advertisement -