Last active
August 29, 2015 14:18
-
-
Save JadenGeller/198637975801dcf3674e to your computer and use it in GitHub Desktop.
INTERCAL Select Operator in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Learn more: http://en.wikipedia.org/wiki/INTERCAL#Operators | |
infix operator ~ { | |
associativity left | |
precedence 140 | |
} | |
// Intercal select | |
func ~(var lhs: Int, var rhs: Int) -> Int { | |
let lsb = 1 | |
var result = 0 | |
var foundCount = 0 | |
for i in 0..<sizeof(Int) { | |
let search = rhs & lsb | |
if lhs & lsb == search { | |
result |= search << foundCount++ | |
rhs >>= 1 | |
} | |
lhs >>= 1 | |
} | |
return result | |
} | |
51 ~ 21 // -> 5 | |
// lhs: 51; 1 *1* 0 *0* 1 *1* | |
// rhs: 21; 0 1 0 *1* *0* *1* | |
// result: 5; 0 0 0 1 0 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment