SQL Server中的无符号右移'>>>'运算符

穆萨克希尔·赛义德

如何在SQL Server中编写无符号右移运算符?表情就像value >>> 0

这是例如-5381 >>> 0 = 4294961915

乙乙

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

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

实施和测试留给读者练习。

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章