- 按位 和 位移位操作符 与 C 和 OC 的类似
- 溢出行为被捕获并报出一个错误
- 溢出操作符一般始于 &,第二个字符使用算术运算符,例如 &+
- 可为自定义类型的 Swift 标准库中的操作符的提供自定义实现。
位操作符
支持在 C 中的所有位操作符
位非操作符
Perfix Operator : ~ + number; eg: 00001111 = ~11110000
let initalBits: UInt8 = 0b00001111
let invertedBits = ~initalBits // equal
位或操作符
Operator : |
1 | 0 = 1
1 | 1 = 1
0 | 0 = 0
let someBits: UInt8 = 0b10110010
let moreBits: UInt8 = 0b01011110
let combinedbits = someBits | moreBits
位异或操作符
Operator : ^
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 0 = 0
let firstBits: UInt8 = 0b00010100
let otherBits: UInt8 = 0b00000101
let outputBits = firstBits ^ otherBits
位左移和右移操作符
左 << 1 bit * 2 、右 >> 1bit / 2
无符号整型位移
- 存在的位左移或右移
- 任何位被位移超出整型存储访问范围则被丢弃
- 原始位左移和右移之后,0被插入遗留下的空间
有符号整型位移
- 第一位是符号位,其余为值有效位
- 有符号和无符号整型正数的值相同
- 右移补充空余位置补充与符号位相同的位值(补码)
负 1 正 0
- 负数编码使用其补码代替
- 补码:除了符号位,其余取反
-4 | 10000100 | 11111011
-1 | 10000001 | 11111110
-5 | 10000101 | 11111001
溢出操作符
var potentialOverFlow = Int16.max
potentialOverFlow += 1
// this causes an error
&+
var unsignedOverflow = UInt8.max
unsignedOverflow = unsignedOverflow &+ 1
// unsignedOverflow is now equal to 0
/*
0|11111111
0|00000001
1|00000000
*/
&-
var unsignedOverflow = UInt8.min
unsignedOverflow = unsignedOverflow &- 1
// unsignedOverflow is now equal to 255
/*
0|0000000
- 0|0000001
1|1111111
*/
操作符方法
- 重载加号运算符(中缀)
struct Vector2D {
var x = 0.0, y = 0.0
}
extension Vector2D {
static func + (left: Vector2D, right: Vector2D) -> Vector2D {
return Vector2D(x: left.x + right.x, y: left.y + right.y)
}
}
- 前缀和后缀运算符
extension Vector2D {
static prefix func - (vector: Vector2D) -> Vector2D {
return Vector2D(x: -vector.x, y: -vector.y)
}
}
- 复合运算符
extension Vector2D {
static func += (left: inout Vector2D, right: Vector2D) {
left = left + right
}
}
分配符 = 和 三元运算符 ?: 不能被重载
- 等价运算符(中缀)
== 和 !=,实现 ==,默认 != 结果为 == 取反
遵循 Equatable 协议
extension Vector2D: Equatable {
static func == (left: Vector2D, right: Vector2D) -> Bool {
return(left.x == right.x) && (left.y == right.y)
}
}
以下默认提供等价运算符合成实现:
- 只含有存储属性且遵循 Equatable 协议的结构体
- 只含有关联类型且遵循 Equatable 协议的枚举
- 没有关联类型的枚举
struct Vector3D: Equatable {
var x = 0.0, y = 0.0, z = 0.0
}
let twoThreeFour = Vector3D(x: 2.0, y: 3.0, z: 4.0)
let anotherTwoThreeFour = Vector3D(x: 2.0, y: 3.0, z: 4.0)
if twoThreeFour == anotherTwoThreeFour {
print("These two vectors are also equivalent.")
}
// Prints "These two vectors are also equivalent."
自定义操作符
- 为 Swift 声明和实现自定义操作符
- 新操作符声明一个全局使用 operator 关键字,并修饰 prefix,infix,postfix
- code:
prefix operator +++
extension Vector2D {
static prefix func +++ (vector: inout Vector2D) -> Vector2D {
vector += vector
return vector
}
}
var toBeDoubled = Vector2D(x: 1.0, y: 4.0)
let afterDoubling = +++toBeDoubled
优先级和自定义中缀操作符
- 默认的中缀运算符没有设置优先级组,会自动放置到默认优先组(优先级高于三元条件运算符)
- 可以为操作符添加到指定优先级组
- 若前缀和后缀没有指定优先级,同时出现时,优先后缀
infix operator +-: AdditionPrecedence
extension Vector2D {
static func +- (left: Vector2D, right: Vector2D) -> Vector2D {
return Vector2D(x: left.x + right.x, y: left.y - right.y)
}
}
let firstVector = Vector2D(x: 1.0, y: 2.0)
let secondVector = Vector2D(x: 3.0, y: 4.0)
let plusMinusVector = firstVector +- secondVector