How to use bitwise XOR in Swift

1 Answer

0 votes
import Foundation

func printBits(_ n: Int) {
    let binary = String(n, radix: 2)
    let padded = String(repeating: "0", count: 8 - binary.count) + binary
    
    print(padded)
}

let cases = [(5, 5), (7, 0), (0, 6), (0, 0)]

for (x, y) in cases {
    printBits(x)
    print("^")
    printBits(y)
    print("=")
    printBits(x ^ y)
    print("\n")
}



/*
run:
   
00000101
^
00000101
=
00000000


00000111
^
00000000
=
00000111


00000000
^
00000110
=
00000110


00000000
^
00000000
=
00000000

*/

 



answered Jul 11, 2025 by avibootz

Related questions

1 answer 92 views
92 views asked Jul 12, 2025 by avibootz
1 answer 99 views
1 answer 112 views
1 answer 87 views
1 answer 82 views
82 views asked Jul 11, 2025 by avibootz
...