Use bitwise and to check if an integer is odd/even

This commit is contained in:
Devin Torres
2013-10-18 13:25:53 -05:00
parent ea9b0e07da
commit bf7662f9ec
+4 -2
View File
@@ -3,12 +3,14 @@ defmodule Integer do
Functions for working with integers.
"""
import Bitwise
@doc """
Returns true if `n` is an odd number, otherwise false.
Implemented as a macro so it is allowed in guard clauses.
"""
defmacro odd?(n) do
quote do: rem(unquote(n), 2) != 0
quote do: (unquote(n) &&& 1) == 1
end
@doc """
@@ -16,7 +18,7 @@ defmodule Integer do
Implemented as a macro so it is allowed in guard clauses.
"""
defmacro even?(n) do
quote do: rem(unquote(n), 2) == 0
quote do: (unquote(n) &&& 1) == 0
end
@doc """