如何避免使用 AutoIT BitShift() 进行符号扩展?
How to avoid sign extension with AutoIT BitShift()?
在尝试从 C 移植算法时,我确定如果设置了 32 位字段的高位,AutoIT BitShift( ) 函数会进行符号扩展。
ConsoleWrite("Bit shift test: (0x80112233 >> 4)=" & hex(BitShift(0x80112233,4)) & @CRLF) ;### Debug Console
ConsoleWrite("Bit shift test: (0x60112233 >> 4)=" & hex(BitShift(0x60112233,4)) & @CRLF) ;### Debug Console
Bit shift test: (0x80112233 >> 4)=F8011223
Bit shift test: (0x60112233 >> 4)=06011223
看看第一个测试如何在前面添加一个 'F'。
我认为我偏离了 AutoIT 的正常操作(没有很多关于 BitShift 和 BitRotate 的文档,而且我没有看到其他人 运行 遇到这个问题),但我'我希望有人能轻松解决这个问题。
我正在使用 SciTe 的 3.6.6,如果这很重要的话。
我不认为当前的 BitShift() 工作有误。官方documentation是这样说的:
Bit operations are performed as 32-bit integers.
既然没有说"as unsigned 32-bit integers",符号扩展似乎还可以。
不过,我没看出你的意思。如果您知道所需的行为,为什么不实现自定义功能来满足您的需求呢?这是我的变体:
Func BitShiftUnsigned($value, $shift)
If $shift > 0 Then
Return BitAnd(BitShift($value,$shift), BitShift(0x7fffffff, $shift-1))
Else
Return BitShift($value,$shift)
EndIf
EndFunc
在尝试从 C 移植算法时,我确定如果设置了 32 位字段的高位,AutoIT BitShift( ) 函数会进行符号扩展。
ConsoleWrite("Bit shift test: (0x80112233 >> 4)=" & hex(BitShift(0x80112233,4)) & @CRLF) ;### Debug Console
ConsoleWrite("Bit shift test: (0x60112233 >> 4)=" & hex(BitShift(0x60112233,4)) & @CRLF) ;### Debug Console
Bit shift test: (0x80112233 >> 4)=F8011223
Bit shift test: (0x60112233 >> 4)=06011223
看看第一个测试如何在前面添加一个 'F'。
我认为我偏离了 AutoIT 的正常操作(没有很多关于 BitShift 和 BitRotate 的文档,而且我没有看到其他人 运行 遇到这个问题),但我'我希望有人能轻松解决这个问题。
我正在使用 SciTe 的 3.6.6,如果这很重要的话。
我不认为当前的 BitShift() 工作有误。官方documentation是这样说的:
Bit operations are performed as 32-bit integers.
既然没有说"as unsigned 32-bit integers",符号扩展似乎还可以。
不过,我没看出你的意思。如果您知道所需的行为,为什么不实现自定义功能来满足您的需求呢?这是我的变体:
Func BitShiftUnsigned($value, $shift)
If $shift > 0 Then
Return BitAnd(BitShift($value,$shift), BitShift(0x7fffffff, $shift-1))
Else
Return BitShift($value,$shift)
EndIf
EndFunc