为什么布尔类型在swift中不能进行按位运算

Why Boolean type cannot do bitwise operation in swift

如题,为什么swift中的布尔类型不能用位运算符,哪种swift数据类型可以用位运算符?

整数是带位的,因此所有整数类型都可以进行按位运算,您只要查阅header就可以很容易地自己看到这一点;例如,这里是 bitwise-and:

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 中的数字类型;布尔值与逻辑有关,因此它们执行 逻辑 操作。例如,这里是 logical-and:

public func &&<T : BooleanType, U : BooleanType>(lhs: T, @autoclosure rhs: () throws -> U) rethrows -> Bool