C++ Tutorial/Operators statements/bitwise AND
determines if a value is a multiple of X
<source lang="cpp">#include <iostream> using namespace std; int multiple( int ); int main() {
int y; cout << "Enter an integer between 1 and 32000: "; cin >> y; if ( multiple( y ) ) cout << y << " is a multiple of X" << endl; else cout << y << " is not a multiple of X" << endl; return 0;
} int multiple( int num ) {
int mask = 1, mult = 1; for ( int i = 0; i < 10; i++, mask <<= 1 ) if ( ( num & mask ) != 0 ) { mult = 0; break; } return mult;
}</source>
Uppercase letters using bitwise AND.
<source lang="cpp">#include <iostream> using namespace std; int main() {
char ch; for(int i = 0 ; i < 10; i++) { ch = "a" + i; cout << ch << " " ; ch = ch & 223; cout << ch << "\n"; } return 0;
}</source>
a A b B c C d D e E f F g G h H i I j J