Skip to content

Commit 9027c94

Browse files
authored
Create check_even_odd.cpp
Implementation to Check if a number is Even or Odd using Bitwise Operator
1 parent 93a700c commit 9027c94

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
* Worst Case Time Complexity: O(1)
17+
* Space Complexity: O(1)
18+
*
19+
* @author [Vedant Mukhedkar](https://github.com/git5v)
20+
*/
21+
22+
#include <cassert> /// for assert
23+
#include <cstdint>
24+
#include <iostream> /// for IO operations
25+
#include <string>
26+
27+
/**
28+
* @namespace bit_manipulation
29+
* @brief Bit manipulation algorithms
30+
*/
31+
namespace bit_manipulation {
32+
/**
33+
* @namespace even_odd
34+
* @brief Functions for checking if a number is even or odd using bitwise operations
35+
*/
36+
namespace even_odd {
37+
38+
/**
39+
* @brief Checks if a number is even or odd using bitwise AND.
40+
* @param N The number to check.
41+
* @returns "Even" if N is even, "Odd" if N is odd.
42+
*/
43+
std::string checkEvenOdd(std::int64_t N) {
44+
return (N & 1) == 0 ? "Even" : "Odd";
45+
}
46+
47+
} // namespace even_odd
48+
} // namespace bit_manipulation
49+
50+
/**
51+
* @brief Self-test implementations
52+
* @returns void
53+
*/
54+
static void test() {
55+
using bit_manipulation::even_odd::checkEvenOdd;
56+
57+
// Test Even numbers
58+
assert(checkEvenOdd(0) == "Even");
59+
assert(checkEvenOdd(2) == "Even");
60+
assert(checkEvenOdd(100) == "Even");
61+
assert(checkEvenOdd(-4) == "Even");
62+
assert(checkEvenOdd(-1000) == "Even");
63+
64+
// Test Odd numbers
65+
assert(checkEvenOdd(1) == "Odd");
66+
assert(checkEvenOdd(3) == "Odd");
67+
assert(checkEvenOdd(101) == "Odd");
68+
assert(checkEvenOdd(-5) == "Odd");
69+
assert(checkEvenOdd(-999) == "Odd");
70+
71+
std::cout << "All test cases successfully passed!" << std::endl;
72+
}
73+
74+
/**
75+
* @brief Main function
76+
* @returns 0 on exit
77+
*/
78+
int main() {
79+
test(); // run self-test implementations
80+
81+
// Example usage:
82+
// std::int64_t num;
83+
// std::cout << "Enter a number: ";
84+
// std::cin >> num;
85+
// std::cout << num << " is " << bit_manipulation::even_odd::checkEvenOdd(num) << std::endl;
86+
87+
return 0;
88+
}

0 commit comments

Comments
 (0)