Skip to content

Commit 327a59d

Browse files
committed
determine whether a number is odd or even using modulus and bitwise approaches
1 parent e7ebd76 commit 327a59d

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

FreeCodeCamp/CodingQ/OddOrEven.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""
2+
Odd or Even?
3+
Given a positive integer, return "Odd" if it's an odd number, and "Even" is it's even.
4+
"""
5+
6+
import unittest
7+
8+
class OddOrEvenTest(unittest.TestCase):
9+
10+
def test1(self):
11+
self.assertEqual(odd_or_even(1), "Odd")
12+
13+
def test2(self):
14+
self.assertEqual(odd_or_even(2), "Even")
15+
16+
def test3(self):
17+
self.assertEqual(odd_or_even(13), "Odd")
18+
19+
def test4(self):
20+
self.assertEqual(odd_or_even(196), "Even")
21+
22+
def test5(self):
23+
self.assertEqual(odd_or_even(123456789), "Odd")
24+
25+
26+
27+
def odd_or_even(n):
28+
29+
if n % 2 == 0:
30+
return "Even"
31+
else:
32+
return "Odd"
33+
34+
def odd_or_even(n):
35+
36+
return "Even" if n % 2 == 0 else "Odd"
37+
38+
def odd_or_even_bit_wise(n):
39+
40+
return "Odd" if (n & 1) else "Even"
41+
42+
"""
43+
44+
45+
1. What does n & 1 mean?
46+
=> & is the bitwise AND operator.
47+
=> It compares each bit of two numbers.
48+
=> 1 in binary is ...0001.
49+
=> So n & 1 checks only the last bit of n.
50+
51+
52+
2. Why does the last bit matter?
53+
=> In binary, even numbers always end with 0, odd numbers end with 1.
54+
Example:
55+
-> 4 -> 100 (last bit = 0 -> even)
56+
-> 7 -> 111 (last bit = 1 -> odd)
57+
58+
3. How the check works
59+
=> If n & 1 == 1 -> last bit is 1 -> Odd.
60+
=> If n & 1 == 0 -> last bit is 0 -> Even.
61+
62+
4. Why use bitwise instead of %?
63+
-> % 2 is more readable and common in high-level code.
64+
-> n & 1 is faster at the machine level because it directly inspects the binary representation.
65+
-> In practice, modern compilers optimize % 2 into the same bitwise operation, so performance difference is negligible.
66+
-> But n & 1 is a neat trick that shows you understand how numbers are represented in binary.
67+
68+
69+
Method Code Idea
70+
71+
Modulus n % 2 == 0 Divide by 2, check remainder
72+
73+
Bitwise AND n & 1 == 0 Inspect last binary bit
74+
75+
Both are correct - modulus is more readable, bitwise is lower-level and faster in theory.
76+
77+
The paranthesis for the case of the remainder but not the other is
78+
79+
=> Here, n % 2 == 0 is a comparison expression.
80+
-> Python's operator precedence makes it clear: % is evaluated first , then ==
81+
=> so parathesis aren't needed = the intent is obvious.
82+
83+
84+
In the next example
85+
86+
-> n & 1 is a bitwisee operation.
87+
-> It produces either 0 or 1.
88+
-> Since 0 is falsy and 1 is truthy, we use it directly in the conditional expression.
89+
-> Parentheses are added for clarity, not necessity, you could write.
90+
91+
return "Odd" if n & 1 else "Even"
92+
93+
-> it works the same as well. But many developers prefer parantheses around bitwise expressions to make it visually clear that the condition is based on a binary mask, not a logical comparison.
94+
95+
No functional difference - paranthesis are just stylistic clarity in the bitwise case.
96+
97+
Logical AND , OR , NOT work with truth values (True/ False)
98+
Example:
99+
if x > 0 and y > 0:
100+
print("Both positive")
101+
=> Bitwise operators:
102+
& (AND), | (OR), ^(XOR), -(NOT) , <<(LEFT SHIFT), >> (RIGHT SHIFT)
103+
104+
-> Work at the bit level of integers.
105+
-> Example:
106+
a = 6 # 110 in binary
107+
b = 3 # 011 in binary
108+
109+
print(a & b) # 2 (010)
110+
print(a | b) # 7 (111)
111+
print(a ^ b) # 5 (101)
112+
113+
114+
Logical operators (and , or) work on truth values.
115+
Bitwise operators (&, |, ^) work on the binary representation of integers .
116+
"""
117+
118+
if __name__ == "__main__":
119+
120+
print(odd_or_even(20))
121+
unittest.main()

0 commit comments

Comments
 (0)