TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
check_even_odd.cpp File Reference

Implementation to [Check if a number is Even or Odd using Bitwise Operator] (https://www.log2base2.com/c-examples/bitwise/odd-or-even-program-in-c-using-bitwise-operator.html) More...

#include <cassert>
#include <cstdint>
#include <iostream>
#include <string>
Include dependency graph for check_even_odd.cpp:

Go to the source code of this file.

Namespaces

namespace  bit_manipulation
 for std::string
namespace  even_odd
 Functions for checking if a number is even or odd using bitwise operations.

Functions

bool bit_manipulation::even_odd::is_even (std::int64_t N)
 Checks if a number is even or odd using bitwise AND.
static void test ()
 Self-test implementations.
int main ()
 Main function.

Detailed Description

Implementation to [Check if a number is Even or Odd using Bitwise Operator] (https://www.log2base2.com/c-examples/bitwise/odd-or-even-program-in-c-using-bitwise-operator.html)

Given an integer N, determine whether it is even or odd using bitwise manipulation. The least significant bit (LSB) of a binary number determines its parity:

  • If the LSB is 0, the number is even.
  • If the LSB is 1, the number is odd.

This can be checked efficiently using the bitwise AND operator (&) with 1.

  • If (N & 1) == 0, N is even.
  • If (N & 1) == 1, N is odd.

Example: Consider 8-bit binary representations of two numbers: Number: 10 (decimal) -> 00001010 (binary) LSB = 0 -> Even number

Number: 13 (decimal) -> 00001101 (binary) LSB = 1 -> Odd number

In both cases, evaluating (N & 1) isolates the LSB:

  • For 10: 00001010 & 00000001 = 0 (Even)
  • For 13: 00001101 & 00000001 = 1 (Odd)

Worst Case Time Complexity: O(1) Space Complexity: O(1)

Author
Vedant Mukhedkar

Definition in file check_even_odd.cpp.

Function Documentation

◆ is_even()

bool bit_manipulation::even_odd::is_even ( std::int64_t N)

Checks if a number is even or odd using bitwise AND.

Parameters
NThe number to check.
Returns
"Even" if N is even, "Odd" if N is odd.

Definition at line 55 of file check_even_odd.cpp.

55 {
56 return (N & 1) == 0 ? true : false;
57 }

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 90 of file check_even_odd.cpp.

90 {
91 test(); // run self-test implementations
92 return 0;
93}
static void test()
Self-test implementations.

◆ test()

void test ( )
static

Self-test implementations.

Returns
void

Definition at line 66 of file check_even_odd.cpp.

66 {
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}
bool is_even(std::int64_t N)
Checks if a number is even or odd using bitwise AND.