在 Python 中连接两个 32 位 int 以获得 64 位 long

发布于 2024-09-15 08:22:52 字数 294 浏览 8 评论 0原文

我想生成 64 位长的 int 作为文档的唯一 ID。

一种想法是将用户 ID(一个 32 位 int)与 Unix 时间戳(另一个 32 位 int)组合起来,形成一个唯一的 64 位长整数。

一个按比例缩小的示例是:

将两个 4 位数字 00100101 组合起来形成 8 位数字 00100101

  1. 这个方案有意义吗?
  2. 如果是这样,我该如何在Python中进行数字的“串联”?

I want to generate 64 bits long int to serve as unique ID's for documents.

One idea is to combine the user's ID, which is a 32 bit int, with the Unix timestamp, which is another 32 bits int, to form an unique 64 bits long integer.

A scaled-down example would be:

Combine two 4-bit numbers 0010 and 0101 to form the 8-bit number 00100101.

  1. Does this scheme make sense?
  2. If it does, how do I do the "concatenation" of numbers in Python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月亮邮递员 2024-09-22 08:22:52

将第一个数字左移第二个数字中的位数,然后添加(或按位或 - 在以下示例中将 + 替换为 |)第二个数字。

result = (user_id << 32) + timestamp

关于你缩小的例子,

>>> x = 0b0010
>>> y = 0b0101
>>> (x << 4) + y
37
>>> 0b00100101
37
>>>

Left shift the first number by the number of bits in the second number, then add (or bitwise OR - replace + with | in the following examples) the second number.

result = (user_id << 32) + timestamp

With respect to your scaled-down example,

>>> x = 0b0010
>>> y = 0b0101
>>> (x << 4) + y
37
>>> 0b00100101
37
>>>
莫言歌 2024-09-22 08:22:52
foo = <some int>
bar = <some int>

foobar = (foo << 32) + bar
foo = <some int>
bar = <some int>

foobar = (foo << 32) + bar
初心 2024-09-22 08:22:52

这应该可以做到:

(x << 32) + y

This should do it:

(x << 32) + y
假装爱人 2024-09-22 08:22:52

对于下一个人(在这种情况下就是我)。一般来说,这是一种方法(对于缩小的示例):

def combineBytes(*args):
    """
    given the bytes of a multi byte number combine into one
    pass them in least to most significant 
    """
    ans = 0
    for i, val in enumerate(args):
        ans += (val << i*4)
    return ans

对于其他尺寸,将 4 更改为 32 或其他。

>>> bin(combineBytes(0b0101, 0b0010))
'0b100101'

For the next guy (which was me in this case was me). Here is one way to do it in general (for the scaled down example):

def combineBytes(*args):
    """
    given the bytes of a multi byte number combine into one
    pass them in least to most significant 
    """
    ans = 0
    for i, val in enumerate(args):
        ans += (val << i*4)
    return ans

for other sizes change the 4 to a 32 or whatever.

>>> bin(combineBytes(0b0101, 0b0010))
'0b100101'
轻拂→两袖风尘 2024-09-22 08:22:52

在此之前的答案都没有涵盖合并和拆分数字。分裂和合并一样是必要的。

NUM_BITS_PER_INT = 4  # Replace with 32, 48, 64, etc. as needed.
MAXINT = (1 << NUM_BITS_PER_INT) - 1

def merge(a, b):
    c = (a << NUM_BITS_PER_INT) | b
    return c

def split(c):
    a = (c >> NUM_BITS_PER_INT) & MAXINT
    b = c & MAXINT
    return a, b

# Test
EXPECTED_MAX_NUM_BITS = NUM_BITS_PER_INT * 2
for a in range(MAXINT + 1):
    for b in range(MAXINT + 1):
        c = merge(a, b)
        assert c.bit_length() <= EXPECTED_MAX_NUM_BITS
        assert (a, b) == split(c)

None of the answers before this cover both merging and splitting the numbers. Splitting can be as much a necessity as merging.

NUM_BITS_PER_INT = 4  # Replace with 32, 48, 64, etc. as needed.
MAXINT = (1 << NUM_BITS_PER_INT) - 1

def merge(a, b):
    c = (a << NUM_BITS_PER_INT) | b
    return c

def split(c):
    a = (c >> NUM_BITS_PER_INT) & MAXINT
    b = c & MAXINT
    return a, b

# Test
EXPECTED_MAX_NUM_BITS = NUM_BITS_PER_INT * 2
for a in range(MAXINT + 1):
    for b in range(MAXINT + 1):
        c = merge(a, b)
        assert c.bit_length() <= EXPECTED_MAX_NUM_BITS
        assert (a, b) == split(c)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文