what's ^ operator for


(Mr Bee) #1

Hi all, I wrote this in XCode Playground…
let i = 2^2print(i)
It runs fine without any errors and print 0, while I expect it to print 4 from 2 power by 2 or 2². Swift ebook and even Google gave me nothing on this.
What did I miss?
Thank you.
Regards,
–Mr Bee


(Mr Bee) #2

Forget it. Just found the answer myself on Swift ebook. It's a bitwise xor operator. And there's no power operator in Swift. Sorry.

I've looked for this for hours. Why did I find the answer after I asked here? Life. :slight_smile:
–Mr Bee

    Pada Minggu, 18 September 2016 19:41, Mr Bee <pak.lebah@yahoo.com> menulis:

Hi all, I wrote this in XCode Playground…
let i = 2^2print(i)
It runs fine without any errors and print 0, while I expect it to print 4 from 2 power by 2 or 2². Swift ebook and even Google gave me nothing on this.
What did I miss?
Thank you.
Regards,
–Mr Bee


(Saagar Jha) #3

The ^ operator performs a bitwise XOR <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>. This is not the same as exponentiation.

Saagar Jha

···

On Sep 18, 2016, at 05:41, Mr Bee via swift-users <swift-users@swift.org> wrote:

Hi all,

I wrote this in XCode Playground…

let i = 2^2
print(i)

It runs fine without any errors and print 0, while I expect it to print 4 from 2 power by 2 or 2². Swift ebook and even Google gave me nothing on this.

What did I miss?

Thank you.

Regards,

–Mr Bee

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users