for (int i = sz - 1; i >= 0; --i, n >>= 1) { str[i] = (n & 1) + '0'; }
时间: 2024-06-01 11:12:18 浏览: 35
& 1) + '0'; }
This code is used to convert a decimal number 'n' into a binary string 'str'. The loop starts from the most significant bit (MSB) of the binary representation of 'n' and goes down to the least significant bit (LSB). At each iteration, the LSB of 'n' is extracted using a bitwise AND operation with 1 and added to the ASCII value of the character '0'. The resulting character is stored in the 'i'-th position of the string 'str'. Finally, 'n' is right-shifted by 1 bit to get the next higher bit in the next iteration.
相关问题
for (int i = sz - 1; i >= 0; --i, n >>= 1) { str[i] = (n & 1) + '0'; } str[sz] = '\0';
& 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.
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) d:\TDXDATA\tes111t.py in line 70 67 return df 69 # 读取日线数据 ---> 70 day_data = read_data(r"D:\Quant1\sz000001.day") 72 # 读取1分钟线数据 73 min1_data = read_data('1分钟线数据文件路径') d:\TDXDATA\tes111t.py in line 60, in read_data(filename) 58 data = f.read() 59 if filename.endswith('.day'): ---> 60 df = parse_day_data(data) 61 elif filename.endswith('.lc1'): 62 df = parse_1min_data(data) d:\TDXDATA\tes111t.py in line 12, in parse_day_data(data) 10 df = pd.DataFrame(columns=['date', 'open', 'high', 'low', 'close', 'volume', 'amount']) 11 for i in range(len(data)): ---> 12 row_data = struct.unpack('iiiiifd', data[i]) 13 date = str(row_data[0]) 14 open_price = row_data[1] / 100.0 TypeError: a bytes-like object is required, not 'int'
这是一个Python的错误提示,看起来是在读取某个文件时出现了错误。具体来说,代码中调用了一个叫做parse_day_data的函数,该函数以二进制形式读取一个文件,然后解析其中的数据。但是,在解析数据时出现了问题,似乎是由于读取的数据类型不正确导致的。你可以检查一下代码,确认读取的数据类型是否正确。
阅读全文