Mccoy_34
OP
Created
Replies
6
Boosts
0
Views
284
Participants
3
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 :)