简体   繁体   English

int(str) 一个巨大的数字

[英]int(str) of a huge number

if i have a number that is too big to be represented with 64 bits so i receive a string that contains it.如果我有一个太大而无法用 64 位表示的数字,那么我会收到一个包含它的字符串。 what happens if i use:如果我使用会发生什么:

num = int(num_str)

i am asking because it looks like it works accurately and i dont understand how, does is allocate more memory for that?我问是因为它看起来工作正常,但我不明白如何,是否为此分配了更多内存?

i was required to check if a huge number is a power of 2. someone suggested:我被要求检查一个巨大的数字是否是 2 的幂。有人建议:

def power(self, A):
    A = int(A)
    if A == 1:
        return 0
    x =bin(A)
    if x.count('1')>1:
        return 0
    else:
        return 1

while i understand why under regular circumstances it would work, the fact that the numbers are much larger than 2^64 and it still works baffles me.虽然我理解为什么在正常情况下它会起作用,但数字远大于 2^64 并且它仍然有效的事实让我感到困惑。

According to the Python manual's description on the representation of integers:根据 Python 手册对整数表示的描述:

These represent numbers in an unlimited range, subject to available (virtual) memory only.这些代表无限范围内的数字,仅受可用(虚拟)内存的影响。 For the purpose of shift and mask operations, a binary representation is assumed, and negative numbers are represented in a variant of 2's complement which gives the illusion of an infinite string of sign bits extending to the left.出于移位和掩码操作的目的,假设使用二进制表示,并且负数以 2 的补码的变体表示,这会产生向左延伸的无限符号位串的错觉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM