如何从这个函数中获得特定的值?

2024-03-29 14:18:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我读到:https://stackoverflow.com/a/37605582/6426449

START_TIME = a constant that represents a unix timestamp

def make_id():

    t = int(time.time()*1000) - START_TIME
    u = random.SystemRandom().getrandbits(23)
    id = (t << 23 ) | u

    return id

def reverse_id(id):
    t  = id >> 23
    return t + START_TIME 

从上面的def,如何得到tuid(它是由def make_id生成的)?你知道吗

就像

def get_t(id):
    some methods
    return t

def get_u(id):
    some methods
    return u

Tags: httpscomidgetmakereturnthattime
1条回答
网友
1楼 · 发布于 2024-03-29 14:18:00

要获得t,只需用右移撤消左移。你知道吗

def get_t(id):
    return id >> 23

要获得u,请使用设置了最右边23位的位掩码

def get_u(id):
    return id & 0x7fffff

相关问题 更多 >