What does ^ do in swift

please can someone tell me what "mean" in swift thanks

Answered by Claude31 in 133302022

It is the bitwise XOR operator.


If you have 2 numbers,

a = 01100101

b = 10011100


the ^ result will be number whose digits are 1 of the digits are different in a and b, 0 otherwise


a ^b = 11111001

Accepted Answer

It is the bitwise XOR operator.


If you have 2 numbers,

a = 01100101

b = 10011100


the ^ result will be number whose digits are 1 of the digits are different in a and b, 0 otherwise


a ^b = 11111001

Thanks man I wrote a code like this var n = 2 While n < 100 { n = n^2 } Print(n) And xcode blew up

You have an infinite loop !


when n == 2, n^2 = 0

then, n^2 = 2

then again n^2 = 0


So you never get out of the while.


Test the following code with a "Run" button connected to this IBAction:

    @IBAction func run(sender: AnyObject) {
        var n = 2
        for i in 0..<10 {
            n = n^2
            print(i)
            print(n)
        }
    }


If you want exponentiation, you should write n *= 2

Wait a second, squaring would be done using n = n*n or n = pow(n, 2). What you wrote is just doubling the value.

Right, I mistyped. That would just work for the n=2 case.

Thanks guys :)

What does ^ do in swift
 
 
Q