Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unsigned right shift '>>>' Operator in sql server [closed]

How to write unsigned right shift operator in sql server? The expression is like value >>> 0

Here is the e.g. -5381>>>0 = 4294961915

like image 349
Musakkhir Sayyed Avatar asked Jun 26 '15 11:06

Musakkhir Sayyed


1 Answers

T-SQL has no bit-shift operators, so you'd have to implement one yourself. There's an implementation of a bitwise shifts here: http://dataeducation.com/bitmask-handling-part-4-left-shift-and-right-shift/

You'd have to cast your integer to a varbinary, use the bitwise shift function and cast back to integer and (hopefully) hey-presto! There's your result you're expecting.

Implementation and testing is left as an exercise for the reader...

Edit - To try to clarify what I have put in the comments below, executing this SQL will demonstrate the different results given by the various CASTs:

SELECT -5381 AS Signed_Integer,
        cast(-5381 AS varbinary) AS Binary_Representation_of_Signed_Integer,
        cast(cast(-5381 AS bigint) AS varbinary) AS Binary_Representation_of_Signed_Big_Integer, 
        cast(cast(-5381 AS varbinary) AS bigint) AS Signed_Integer_Transposed_onto_Big_Integer, 
        cast(cast(cast(-5381 AS varbinary) AS bigint) AS varbinary) AS Binary_Representation_of_Signed_Integer_Trasposed_onto_Big_Integer

Results:

Signed_Integer Binary_Representation_of_Signed_Integer                        Binary_Representation_of_Signed_Big_Integer                    Signed_Integer_Transposed_onto_Big_Integer Binary_Representation_of_Signed_Integer_Trasposed_onto_Big_Integer
-------------- -------------------------------------------------------------- -------------------------------------------------------------- ------------------------------------------ ------------------------------------------------------------------
-5381          0xFFFFEAFB                                                     0xFFFFFFFFFFFFEAFB                                             4294961915                                 0x00000000FFFFEAFB
like image 75
Ed B Avatar answered Sep 22 '22 07:09

Ed B