DEV Community

Discussion on: Daily Challenge #255 - Is There an Odd Bit?

Collapse
 
vidit1999 profile image
Vidit Sarkar • Edited

Here is a C++ solution, (considering given number is positive and 32-bit integer)

#include <bits/stdc++.h>
using namespace std;

int any_odd(unsigned int number){
    return (number & 0xAAAAAAAA) != 0; // 0xAAAAAAAA has all its odd bits set, so if result is not 0 then number has at least one odd set bit
}

int main(){
    cout << any_odd(2) << "\n"; // output -> 1
    cout << any_odd(170) << "\n"; // output -> 1
    cout << any_odd(5) << "\n"; // output -> 0
    cout << any_odd(7) << "\n"; // output -> 1
    cout << any_odd(10) << "\n"; // output -> 1
    cout << any_odd(0) << "\n"; // output -> 0
    return 0;
}