for (int i = sz - 1; i >= 0; --i, n >>= 1) { str[i] = (n & 1) + '0'; } str[sz] = '\0';
时间: 2024-05-25 22:18:36 浏览: 81
& 1) ? '1' : '0'; }
This code is used to convert an integer number into a binary string representation. The variable "n" is the integer number, and "sz" is the number of binary digits required to represent the number. The loop starts from the most significant bit (i.e., the highest power of 2), and for each bit, it checks whether it is 0 or 1. If it is 1, the corresponding character in the string is set to '1', and if it is 0, the corresponding character is set to '0'. The ">>=" operator is used to shift the bits of the integer to the right by one position in each iteration, effectively dividing the number by 2. The loop continues until all the binary digits are processed, resulting in a binary string representation of the integer.
阅读全文