gpt4 book ai didi

xcode - 左移 (<<) 在 Swift 中实际上做了什么?

转载 作者:搜寻专家 更新时间:2023-10-30 21:48:43 28 4
gpt4 key购买 nike

我正在摆弄一个 Flappy Bird 的克隆,我无法弄清楚以下代码的含义

let birdCategory: UInt32 = 1 << 0
let worldCategory: UInt32 = 1 << 1
let pipeCategory: UInt32 = 1 << 2
let scoreCategory: UInt32 = 1 << 3

抱歉,如果这很明显,我已经尝试寻找答案但找不到。谢谢

最佳答案

这就是按位左移运算符

基本上就是这样

// moves 0 bits to left for 00000001
let birdCategory: UInt32 = 1 << 0

// moves 1 bits to left for 00000001 then you have 00000010
let worldCategory: UInt32 = 1 << 1

// moves 2 bits to left for 00000001 then you have 00000100
let pipeCategory: UInt32 = 1 << 2

// moves 3 bits to left for 00000001 then you have 00001000
let scoreCategory: UInt32 = 1 << 3

你最终拥有

birdCategory = 1
worldCategory = 2
pipeCategory = 4
scoreCategory= 8

关于xcode - 左移 (<<) 在 Swift 中实际上做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30332724/

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