Bitwise operators work on bits, logical operators evaluate boolean expressions. As long as expressions return bool
, why don't we use bitwise operators instead of logical?
In this example I use bitwise instead of logical:
#include <iostream>
int main(){
int age;
std::cin >> age;
if( (age < 0) | (age > 100) ) // eg: -50: 1 | 0 = 1
std::cout << "Invalid age!" << std::endl;
// if( (age < 0) || (age > 100) )
// std::cout << "Invalid age!" << std::endl;
return 0;
}