Concatenate two 32 bit int to get a 64 bit long in 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?
6
задан Acumenus 11 January 2019 в 06:48
поделиться

3 ответа

Сдвинуть влево первое число на количество бит во втором числе, затем сложить (или поразрядное ИЛИ - заменить + с | в следующих примерах) вторым номером.

result = (user_id << 32) + timestamp

Что касается вашего уменьшенного примера,

>>> x = 0b0010
>>> y = 0b0101
>>> (x << 4) + y
37
>>> 0b00100101
37
>>>
16
ответ дан 8 December 2019 в 03:51
поделиться
foo = <some int>
bar = <some int>

foobar = (foo << 32) + bar
5
ответ дан 8 December 2019 в 03:51
поделиться

Это должно сделать это:

(x << 32) + y
4
ответ дан 8 December 2019 в 03:51
поделиться
Другие вопросы по тегам:

Похожие вопросы: