MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / is_even

Function is_even

bit_manipulation/is_even.py:1–31  ·  view source on GitHub ↗

return true if the input integer is even Explanation: Lets take a look at the following decimal to binary conversions 2 => 10 14 => 1110 100 => 1100100 3 => 11 13 => 1101 101 => 1100101 from the above examples we can observe that for all the odd integers ther

(number: int)

Source from the content-addressed store, hash-verified

1def is_even(number: int) -> bool:
2 """
3 return true if the input integer is even
4 Explanation: Lets take a look at the following decimal to binary conversions
5 2 => 10
6 14 => 1110
7 100 => 1100100
8 3 => 11
9 13 => 1101
10 101 => 1100101
11 from the above examples we can observe that
12 for all the odd integers there is always 1 set bit at the end
13 also, 1 in binary can be represented as 001, 00001, or 0000001
14 so for any odd integer n => n&1 is always equals 1 else the integer is even
15
16 >>> is_even(1)
17 False
18 >>> is_even(4)
19 True
20 >>> is_even(9)
21 False
22 >>> is_even(15)
23 False
24 >>> is_even(40)
25 True
26 >>> is_even(100)
27 True
28 >>> is_even(101)
29 False
30 """
31 return number & 1 == 0
32
33
34if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected