How to check if the least significant bit (LSB) of a number is set or not in C++

1 Answer

0 votes
#include <iostream>

int main() {
    int n = 15; // binary: 1111

    if (n & 1) // Method 1
        std::cout << "LSB is set\n";
    else
        std::cout << "LSB is not set\n";
        
    if (n % 2 == 1) // Method 2
        std::cout << "LSB is set\n";
    else
        std::cout << "LSB is not set\n";
        
    if (4 & 1)
        std::cout << "LSB is set\n";
    else
        std::cout << "LSB is not set\n";
}

 
 
/*
run:
 
LSB is set
LSB is set
LSB is not set
 
*/

 



answered Dec 27, 2025 by avibootz
...