YonF
YonF

Reputation: 651

Why Boolean type cannot do bitwise operation in swift

As the title, why bitwise operator cannot apply to Boolean type in swift, which kind of swift data type can work with bitwise operator?

Upvotes: 2

Views: 899

Answers (1)

matt
matt

Reputation: 534950

Integers are the ones with bits, so all integer types can do bitwise operations, as you could readily have seen for yourself merely by consulting the header; for example, here's 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 is not a numeric type in Swift; Booleans are about logic, so they do logical operations. For example, here's logical-and:

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

Upvotes: 2

Related Questions