Advanced Operators

/* Advanced Operators -> 更新到XCode Beta5
1.默认运算是不可以溢出的,否则报错。
如果是故意溢出,但是swift提供了一组已&开头的运算符 &+可以溢出。
5个可溢出操作 &+ &- &* &/(除0操作返回0) &%

var a:UInt8 = 255 &+ 1; //结果为0
✨✨var a:UInt8 = 300 &+ 1;这个算式编译会报错,因为swift自动推倒类型后,
后面两个加法都是UInt8的,300范围外,会报如下错误:
Integer literal overflows when stored int UInt8.(即当把300存到Uint8时出错)

2.操作符重载
中缀运算符如+-等不需要特殊标示,混合赋值语句 += 也不需要加特殊标示
前置++用 prefix 后置++用postfix;注意新版本中去掉了@,=运算符不再需要assignment修饰
前置的还有一种情况就是只有一个+或者减号

3.自定义新的运算符需要先声明再定义,声明中需要infix,prefix,postfix说明,定义中infix不需要。
一元运算符需要指明前置,和后置,括号中的结合性,优先级不需要指定。
infix operator +- { associativity left precedence 140 } //声明
func +- (l, r){} //定义
*/

//Bitwise operator
let temp:UInt8 = 0b00001111

//NOT 按位取反
println("~temp: \(~temp)")//0b11110000

//Bitwise And 按位与
println(temp & (~temp))//0b00000000

//Bitwise OR 按位或
println(temp | (~temp))//0b11111111

//Bitwise XOR 按位异或  ^
//Bitwise Left and Right Shift  Operators <<  >>
//无符号数左右移动后补零
//有符号数最高位保持不变,其余位左移动时右边补零,右移动时负数补1正数补 0

//操作符重载,这里conform Printable协议,可以用println直接输出。
struct Vector2D: Printable
{
    var x = 0.0, y = 0.0
    
    // Printable协议
    var description:String {
        get {
            return "x: \(x), y: \(y)"
        }
    }
}

//+
func + (l: Vector2D, r: Vector2D) -> Vector2D
{
    return Vector2D(x: l.x + r.x, y: l.y + r.y)
}
//-
func - (l: Vector2D, r: Vector2D) -> Vector2D
{
    return Vector2D(x: l.x - r.x, y: l.y - r.y)
}
//postfix ++ 并且实现赋值功能
postfix func ++ (inout v: Vector2D) -> Vector2D
{
    return Vector2D(x: v.x++, y: v.y++)
}
//prefix ++
prefix func ++ (inout v: Vector2D) -> Vector2D
{
    return Vector2D(x: ++v.x, y: ++v.y)
}
//prefix -
prefix func - (inout v: Vector2D) -> Vector2D
{
    return Vector2D(x: -v.x, y: -v.y)
}
//==
func == (l: Vector2D, r: Vector2D) -> Bool
{
    return ( (l.x == l.x) && (l.y == r.y) ) ? true : false;
}
//!=
func != (l: Vector2D, r: Vector2D) -> Bool
{
    return ( (l.x == l.x) && (l.y == r.y) ) ? false : true;
}

func += (inout l:Vector2D, r:Vector2D)
{
    l = l + r//这里可以直接用前面实现的加法运算;a += b; a 是第一个左参数,b是第二个右参数
}

var v1 = Vector2D(x:1.0, y:3.0)
var v2 = Vector2D(x:1.0, y:1.0)
println("v1+v2: \(v1+v2)")

var v3 = v2 + ++v1
println("v2 + ++v1 : \(v3)")
println(v1)

v1 = Vector2D(x:1.0, y:3.0)
v3 = v2 + v1++
println(v3)
println(v1)

//可以自定义符号, = - + * % < > ! & | ^ . ~,开头的符号即可。

//新符号定义前需要声明✨✨,声明中需要用infix,prefix,postfix符号
infix operator +- { associativity left precedence 140 }
//当定义前置和后置运算符时,{}中的内容不可以定义,默认为最高级别的。

func +- (l:Vector2D, r: Vector2D) ->Vector2D
{
    return Vector2D(x: l.x + r.x, y: l.y - r.y)
}
//定义的时候不需要用infix,声明的时候需要。

/*Language Reference->Expression->Binary Expressions的介绍

Exponentiative (No associativity, precedence level 160)
    << Bitwise left shift
    >> Bitwise right shift
Multiplicative (Left associative, precedence level 150)
    * Multiply
    / Divide
    % Remainder
    &* Multiply, ignoring overflow
    &/ Divide, ignoring overflow
    &% Remainder, ignoring overflow
    & Bitwise AND
Additive (Left associative, precedence level 140)
    + Add
    - Subtract
    &+ Add with overflow
    &- Subtract with overflow
    | Bitwise OR
    ^ Bitwise XOR
Range (No associativity, precedence level 135)
    ..< Half-open range
    ... Closed range
Cast (No associativity, precedence level 132)
    is Type check
    as Type cast
Comparative (No associativity, precedence level 130)
    < Less than
    <= Less than or equal
    > Greater than
    >= Greater than or equal
    == Equal
    != Not equal
    === Identical
    !== Not identical
    ~= Pattern match
Conjunctive (Left associative, precedence level 120)
    && Logical AND
    Disjunctive (Left associative, precedence level 110)
    || Logical OR
Nil Coalescing (Right associative, precedence level 110)
    ?? Nil coalescing
Ternary Conditional (Right associative, precedence level 100)
    ?: Ternary conditional
Assignment (Right associative, precedence level 90)
    = Assign
    *= Multiply and assign
    /= Divide and assign
    %= Remainder and assign
    += Add and assign
    -= Subtract and assign
    <<= Left bit shift and assign
    >>= Right bit shift and assign
    &= Bitwise AND and assign
    ^= Bitwise XOR and assign
    |= Bitwise OR and assign
    &&= Logical AND and assign
    ||= Logical OR and assign
*/
//《完》


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值