Skip to content

Instantly share code, notes, and snippets.

@ryanonsrc
Created January 6, 2016 03:41
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 ryanonsrc/d395a0892d70752ec128 to your computer and use it in GitHub Desktop.
Save ryanonsrc/d395a0892d70752ec128 to your computer and use it in GitHub Desktop.
object BinaryAddition {
def binaryAddition(a: Array[Int], b: Array[Int]): Array[Int] =
((Array.empty[Int] -> 0) /: (a zip b)) { case ((c, carry), (ai, bi)) =>
(((ai + bi + carry) % 2) +: c) -> (ai + bi + carry) / 2
}._1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment