Skip to content

Commit eaeba76

Browse files
authored
Merge branch 'master' into myContribution
2 parents 5d04574 + 0301b4c commit eaeba76

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @file
3+
* @brief Implementation to [Check if a number is Even or Odd using Bitwise Operator]
4+
* (https://www.log2base2.com/c-examples/bitwise/odd-or-even-program-in-c-using-bitwise-operator.html)
5+
*
6+
* @details
7+
* Given an integer N, determine whether it is even or odd using bitwise manipulation.
8+
* The least significant bit (LSB) of a binary number determines its parity:
9+
* - If the LSB is 0, the number is even.
10+
* - If the LSB is 1, the number is odd.
11+
*
12+
* This can be checked efficiently using the bitwise AND operator (&) with 1.
13+
* - If (N & 1) == 0, N is even.
14+
* - If (N & 1) == 1, N is odd.
15+
*
16+
* Example:
17+
* Consider 8-bit binary representations of two numbers:
18+
* Number: 10 (decimal) -> 00001010 (binary)
19+
* LSB = 0 -> Even number
20+
*
21+
* Number: 13 (decimal) -> 00001101 (binary)
22+
* LSB = 1 -> Odd number
23+
*
24+
* In both cases, evaluating (N & 1) isolates the LSB:
25+
* - For 10: 00001010 & 00000001 = 0 (Even)
26+
* - For 13: 00001101 & 00000001 = 1 (Odd)
27+
*
28+
* Worst Case Time Complexity: O(1)
29+
* Space Complexity: O(1)
30+
*
31+
* @author [Vedant Mukhedkar](https://github.com/git5v)
32+
*/
33+
34+
#include <cassert> /// for assert
35+
#include <cstdint> /// for uint32_t
36+
#include <iostream> /// for IO operations
37+
#include <string> /// for std::string
38+
39+
/**
40+
* @namespace bit_manipulation
41+
* @brief Bit manipulation algorithms
42+
*/
43+
namespace bit_manipulation {
44+
/**
45+
* @namespace even_odd
46+
* @brief Functions for checking if a number is even or odd using bitwise operations
47+
*/
48+
namespace even_odd {
49+
50+
/**
51+
* @brief Checks if a number is even or odd using bitwise AND.
52+
* @param N The number to check.
53+
* @returns "Even" if N is even, "Odd" if N is odd.
54+
*/
55+
bool is_even(std::int64_t N) {
56+
return (N & 1) == 0 ? true : false;
57+
}
58+
59+
} // namespace even_odd
60+
} // namespace bit_manipulation
61+
62+
/**
63+
* @brief Self-test implementations
64+
* @returns void
65+
*/
66+
static void test() {
67+
using bit_manipulation::even_odd::is_even;
68+
69+
// Test Even numbers
70+
assert(is_even(0) == true);
71+
assert(is_even(2) == true);
72+
assert(is_even(100) == true);
73+
assert(is_even(-4) == true);
74+
assert(is_even(-1000) == true);
75+
76+
// Test Odd numbers
77+
assert(is_even(1) == false);
78+
assert(is_even(3) == false);
79+
assert(is_even(101) == false);
80+
assert(is_even(-5) == false);
81+
assert(is_even(-999) == false);
82+
83+
std::cout << "All test cases successfully passed!" << std::endl;
84+
}
85+
86+
/**
87+
* @brief Main function
88+
* @returns 0 on exit
89+
*/
90+
int main() {
91+
test(); // run self-test implementations
92+
return 0;
93+
}

0 commit comments

Comments
 (0)