#include #include using namespace std; /* To display bit number i of the variable n, we shift n i bits to the right, so that the bit we want to display is all the way to the right. Then we say & 0x1 to zero out all the other bits, so that the only surviving bit is the bit we want to display (the bit that was originally in bit number i). The parentheses around (n >> i) are not necessary. Even without the parentheses, the >> would execute before the & because the >> has higher precedence. */ int main() { cout << "Please input an integer: "; int n {0}; cin >> n; if (!cin) { cerr << "Sorry, couldn't receive that.\n"; return EXIT_FAILURE; } //Output the 32 bits (bit 31 to bit 0 inclusive) of an int //on our machine. for (int i {31}; i >= 0; --i) { const int bitnumberi {(n >> i) & 0x1}; cout << bitnumberi; if (i > 0 && i % 8 == 0) { //the other kind of "and" cout << " "; //space for legibility after each byte } } cout << "\n"; return EXIT_SUCCESS; }