How to toggle a bit at specific position in Swift

1 Answer

0 votes
import Foundation

func getBits(_ n: Int) -> String {
    return String(n, radix: 2)
}

var n = 365
let pos = 2

print(getBits(n))

n ^= (1 << pos)

print(getBits(n))



/*
run:

101101101
101101001

*/

 



answered Apr 3 by avibootz
...