ios - Generic XOR Swap in Swift -


Today I was reading about XOR swaps, and thought I would try to implement a function in Swift Who would like to do this. I successfully wrote a non-generic version of this function:

  Import UIKit func xorSwap (Exact x: int, in-out Wi: IT) {if x! = Y {x ^ = yy ^ Xx ^ = y}} var a = 10 var b = 20 xorSwap (& amp; a, & amp; nbsp;) println (a) // = & gt; 20 printen (b) // = & gt; 10  

I tried to write a normal version after that, but the compiler complained that 't is not equal to balls' I'm assuming there is another protocol that makes me conform Is required, but I'm not sure what that is. Here's a general version attempt:

  Import UIKit func xorSwap & lt; T: Equatable & gt; (X: T, Inbound Y: T) {if x! = Y {x ^ = yy ^ = xx ^ = y}} var a = 10 var b = 20 xor swap (and a, and b) println (a) println (b)  

After the

you can use the built in BitwiseOperationsType

< Pre> Funk Ezire Speech & lt; T: Protocol & lt; BitWord Operation Type, Equitable & gt; & Gt; (Inout X: T, Y inout: T) {! If x = y {x ^ = yy ^ = xx ^ = y}} at a = 10 on b = 20 xorSwap (& amp; A, & amp; nbsp;) println (a) // - & gt; 20 printen (b) // - & gt; A == on NSStringCompareOptions.CaseInsensitiveSearch = NSStringCompareOptions.BackwardsSearch xorSwap (& amp; A, & B) == at 10
  .BackwardsSearch // - & Gt; True b == .CaseInsensitiveSearch // - & gt; True  

Comments

Popular posts from this blog

HTML/CSS - Automatically set height width from background image? -

php - Mysql Show Process - Sleep Commands and what to do -

c - What is the address of buf (the local variable in the main function)? -