连接两个32位int以在Python中获得64位长 [英] Concatenate two 32 bit int to get a 64 bit long in Python

查看:196
本文介绍了连接两个32位int以在Python中获得64位长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成64位长的int用作文档的唯一ID.

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

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

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.

按比例缩小的示例为:

将两个4位数字00100101组合在一起以形成8位数字00100101.

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

  1. 这种方案有意义吗?
  2. 如果可以,如何在Python中进行数字的串联"?

推荐答案

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

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

这篇关于连接两个32位int以在Python中获得64位长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆