How to output binary in the C language?
In C language, binary numbers can be printed using bitwise operations.
Here is an example code:
#include <stdio.h>
// 以二进制形式输出整数
void printBinary(int num) {
int i;
for (i = sizeof(int) * 8 - 1; i >= 0; i--) {
if ((num >> i) & 1) {
printf("1");
} else {
printf("0");
}
}
printf("\n");
}
int main() {
int num = 10;
printf("二进制形式:");
printBinary(num);
return 0;
}
Running the above code will output the following results:
二进制形式:00000000000000000000000000001010
In the printBinary function, the integer is output in binary form bit by bit by using bitwise operations and loops.