![]() |
TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
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>
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. |
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:
This can be checked efficiently using the bitwise AND operator (&) with 1.
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:
Worst Case Time Complexity: O(1) Space Complexity: O(1)
Definition in file check_even_odd.cpp.
bool bit_manipulation::even_odd::is_even | ( | std::int64_t | N | ) |
Checks if a number is even or odd using bitwise AND.
N | The number to check. |
Definition at line 55 of file check_even_odd.cpp.
int main | ( | void | ) |
Main function.
Definition at line 90 of file check_even_odd.cpp.
|
static |
Self-test implementations.
Definition at line 66 of file check_even_odd.cpp.