Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JadenGeller/198637975801dcf3674e to your computer and use it in GitHub Desktop.
Save JadenGeller/198637975801dcf3674e to your computer and use it in GitHub Desktop.
INTERCAL Select Operator in Swift
// 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