Mus*_*yed 3 sql sql-server bitwise-operators sql-server-2008
如何在sql server中编写无符号右移运算符?表达就像value >>> 0
这是例如-5381 >>> 0 = 4294961915
T-SQL没有位移操作符,所以你必须自己实现一个.这里有一个按位变换的实现:http://dataeducation.com/bitmask-handling-part-4-left-shift-and-right-shift/
你必须将你的整数转换为varbinary,使用按位移位函数并转换回整数和(希望)hey-presto!这是你期待的结果.
实施和测试留给读者练习......
编辑 - 为了澄清我在下面的注释中添加的内容,执行此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
Run Code Online (Sandbox Code Playgroud)
结果:
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
Run Code Online (Sandbox Code Playgroud)