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
 General SQL Server Forums
 New to SQL Server Programming
 bitshifting

Author  Topic 

polob4385
Starting Member

1 Post

Posted - 2012-11-30 : 13:24:16
I have a long for example 144936903694469 and in java i have (int)(id>> 32) & 0xFFFFFFFF. I should get returned 3375 . so I wrote this..

declare @something bigint;
set @something=144936903694469
select (@something / power(2,32)) & 0xFFFFFFFF

That seems wrong but not sure what I need to do or if I am missing something. I did Google it and thats as much as i came up with.

Ideas? Thank you.

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-11-30 : 13:33:37
http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/bitmask-handling-part-4-left-shift-and-right-shift.aspx

You're better off avoiding bit shifting in SQL Server. If you absolutely have to support it your best bet is to create a CLR function for it.
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-11-30 : 13:35:48
the power function can only handle up 2 ^ 30 since it can only return an int (2 ^ 31)-1. So maybe

declare @something bigint;
set @something=144936903694469
select (@something / power(2,30))/4 & 0xFFFFFFFF


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-11-30 : 13:49:28
If you cast it as bigint, you can do:
select POWER(cast(2 as bigint),62)
However the bitwise operators only work up to 32 bits.
Go to Top of Page
   

- Advertisement -