如标题所示,为什么Swift中的 boolean 类型不能使用按位运算符,哪种Swift数据类型可以使用按位运算符?
最佳答案
整数是带有位的整数,因此所有整数类型都可以进行按位运算,您只需查阅 header 就可以轻松地亲眼看到;例如,这里是按位与:
public func &(lhs: Int, rhs: Int) -> Int
public func &(lhs: UInt, rhs: UInt) -> UInt
public func &(lhs: Int64, rhs: Int64) -> Int64
public func &(lhs: UInt64, rhs: UInt64) -> UInt64
public func &(lhs: Int32, rhs: Int32) -> Int32
public func &(lhs: UInt32, rhs: UInt32) -> UInt32
public func &(lhs: Int16, rhs: Int16) -> Int16
public func &(lhs: UInt16, rhs: UInt16) -> UInt16
public func &(lhs: Int8, rhs: Int8) -> Int8
public func &(lhs: UInt8, rhs: UInt8) -> UInt8
Bool 在 Swift 中不是数值类型; boolean 值与逻辑有关,因此它们执行逻辑运算。例如,这里是逻辑与:
public func &&<T : BooleanType, U : BooleanType>(lhs: T, @autoclosure rhs: () throws -> U) rethrows -> Bool
关于swift - 为什么 boolean 类型不能在swift中进行按位运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33684893/