无符号右移'>>>'SQL Server中的运算符 [英] unsigned right shift '>>>' Operator in sql server

查看:45
本文介绍了无符号右移'>>>'SQL Server中的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在SQL Server中编写无符号右移运算符?该表达式类似于 value>>>0

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

例如 -5381 >>> 0 = 4294961915

推荐答案

T-SQL没有移位运算符,因此您必须自己实现一个.这里有一个按位移位的实现: http://dataeducation.com/bitmask-handling-part-4-left-shift-and-right-shift/

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/

您必须将整数转换为varbinary,使用按位平移函数并转换回整数,(希望如此)嘿!有您期望的结果.

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...

编辑-为了尝试阐明我在下面的注释中所做的内容,执行此SQL将演示各种CAST给出的不同结果:

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

结果:

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

这篇关于无符号右移'>>>'SQL Server中的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆