int main() {
int input, numbits = sizeof(int) * 8;
cout << "Enter integer to be printed in binary: ";
cin >> input;
cout << input << " in binary: ";
for (int i = 0, x = input; i < numbits; i++) {
unsigned int y = (1 << (numbits-1));
y &= x;
cout << (y ? '1' : '0');
x <<= 1;
}
cout << endl;
}
Exactly how is this code producing binary? I am not able to understand the logic under the hood.
Also, how to use bitwise operation with integers? I think it is first converted to binary, and then the operators are applied. But if it is converted into binary, why not directly print it?
I have written different code that does the same, but I am not understanding this.
4