sql server中的无符号右移'>>>'运算符
技术问答
191 人阅读
|
0 人回复
|
2023-09-14
|
如何在SQL Server中编写无符号右移运算符?表情就像value >>> 0
6 o' Z& |: T5 u% a. Z! R: [这是例如 -5381 >>> 0 = 4294961915. r- ~0 Z% `4 r* i, |1 W! G8 b
3 n9 F1 i! P3 K2 M; Z1 U. _. e解决方案:2 D" t" s6 l! r* ?- C) K
& ^% o$ \; O2 k' D, I- w
0 y! m2 a! b! K; K9 t8 l1 e4 v! ^% l
" J* |" C Y4 D6 p& ` T-SQL没有移位运算符,因此您必须自己实现。这里有一个按位移位的实现:http : //dataeducation.com/bitmask-
& P1 o3 x" }4 L8 X" J/ J: d! xhandling-part-4-left-shift-and-right-shift/
3 h _" R& y: B( g您必须将整数强制转换为varbinary,使用按位移位函数并强制返回整数,并(希望)嘿嘿!有您期望的结果。
+ |9 K" A m( t. \& v/ j5 G实施和测试留给读者练习。
" c' ], R0 i, R; k6 K: C X编辑-为了尝试阐明我在下面的注释中所做的内容,执行此SQL将演示各种CAST给出的不同结果:
) @- ~. v& L9 |+ |* C1 \SELECT -5381 AS Signed_Integer,
, u$ `* b* W/ N, N cast(-5381 AS varbinary) AS Binary_Representation_of_Signed_Integer,
& e8 Y, m5 ^5 k+ _. ^ cast(cast(-5381 AS bigint) AS varbinary) AS Binary_Representation_of_Signed_Big_Integer, 1 x- P- q! t4 E( ]+ A
cast(cast(-5381 AS varbinary) AS bigint) AS Signed_Integer_Transposed_onto_Big_Integer,
# B8 n# j4 c4 U$ g# L6 Y9 ~$ F$ q cast(cast(cast(-5381 AS varbinary) AS bigint) AS varbinary) AS Binary_Representation_of_Signed_Integer_Trasposed_onto_Big_Integer
4 x1 x+ D8 g- J; R( z) Q结果:
+ q/ R' E5 a6 w$ a, cSigned_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_Integer1 u: u9 y4 V, Z; {5 k" `
-------------- -------------------------------------------------------------- -------------------------------------------------------------- ------------------------------------------ ------------------------------------------------------------------
5 u9 y+ t/ w) e Z( r-5381 0xFFFFEAFB 0xFFFFFFFFFFFFEAFB 4294961915 0x00000000FFFFEAFB |
|
|
|
|
|