简体   繁体   中英

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. 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:

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.

According to the Python manual's description on the representation of integers:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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