gpt4 book ai didi

swift - 对 64 位 UInt 右移充当有符号移位感到困惑

转载 作者:行者123 更新时间:2023-11-28 07:53:29 25 4
gpt4 key购买 nike

为什么右移会有如下表现?

let a: UInt = 0x6177206d, b: UInt = 0x9e3779b1
let m: UInt = a * b

print("""
let m = \(String(format:"%x", a)) * \(String(format:"%x", b)) = \(String(format:"%x", m))

m>>4=\(String(format:"%x", m >> 4))
m>>8=\(String(format:"%x", m >> 8))
m>>16=\(String(format:"%x", m >> 16))
m>>32=\(String(format:"%x", m >> 32))
""")

产生:

let m = 6177206d * 9e3779b1 = ef1bf05dm>>4=fef1bf05m>>8=efef1bf0m>>16=a4efef1bm>>32=3c3ca4ef

最佳答案

m0xef1bf05d;它是 0x3c3ca4efef1bf05d

%x 仅处理 32 位值,而您处理的是 64 位值。使用具有 64 位值的 %lxString(m, radix: 16):

let a: UInt = 0x6177206d, b: UInt = 0x9e3779b1
let m: UInt = a * b

print("""
let m = \(String(format:"%lx * %lx = %lx", a, b, m))

m>>4 = \(String(format:"%lx", m >> 4))
m>>8 = \(String(format:"%lx", m >> 8))
m>>16 = \(String(format:"%lx", m >> 16))
m>>32 = \(String(format:"%lx", m >> 32))
""")

或者使用 %016lx 如果你想看到它被零填充(这样更容易看到过程中的转移),产生:

let m = 000000006177206d * 000000009e3779b1 = 3c3ca4efef1bf05dm>>4  = 03c3ca4efef1bf05m>>8  = 003c3ca4efef1bf0m>>16 = 00003c3ca4efef1bm>>32 = 000000003c3ca4ef

关于swift - 对 64 位 UInt 右移充当有符号移位感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49041378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com