Skip to content

Instantly share code, notes, and snippets.

@ohac
Created January 17, 2009 04:49
Show Gist options
  • Save ohac/48263 to your computer and use it in GitHub Desktop.
Save ohac/48263 to your computer and use it in GitHub Desktop.
object a {
def sub(in: Long): Int = {
val masked: Long = in & 0x0101010101010101L
(0 until 7).foldLeft(masked) {(v, i) => (v & 0xff) | (v >> 7)}.toInt
}
def sub2(inp: Long): Int = {
val m1 = inp & 0x0101010101010101L
val m2 = (m1 & 0x0001000100010001L) | (m1 >> 7)
val m3 = (m2 & 0x0000000300000003L) | (m2 >> 14)
val m4 = (m3 & 0x000000000000000fL) | (m3 >> 28)
m4.toInt
}
def main(args: Array[String]): Unit = {
val in = 0xab000000000000ffL
println(Integer.toString(sub(in), 16))
println(Integer.toString(sub2(in), 16))
}
}
// $ scalac a.scala && scala a
// 81
// 81
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment