for (int i = sz - 1; i >= 0; --i, n >>= 1) { str[i] = (n & 1) + '0'; }
时间: 2024-05-24 09:10:08 浏览: 67
Gson解析(List和Map)格式json数据 - CSDN博客1
& 1) + '0'; }
This code snippet is a loop that iterates through the binary representation of a given integer 'n' and assigns each binary digit to a string 'str' in reverse order.
The loop starts from the last binary digit and goes towards the first digit. At each iteration, the least significant bit (the rightmost digit) of 'n' is extracted using the bitwise AND operator with value 1. This is done to check if the last digit is 1 or 0. If it is 1, then it is added to the string 'str' as a character '1', else it is added as a character '0'.
The bitwise right shift operator '>>=' is used to shift the bits of 'n' to the right by 1 position. This is done to discard the least significant bit that has been added to the string 'str' in the current iteration. The loop continues until all bits of 'n' have been processed and added to the string 'str' in reverse order.
Overall, this code snippet is useful for converting an integer into its binary representation as a string.
阅读全文