0

((timestamp - 1288834974657) << 32)

I included some more bits information, for example, total 32 bits after timestamp information needs, then the timestamp needs to be left shift 32 bits, such that the result exceeds long.max value. The result shown a negative value something like -7187691577906700288, it was wrong.

Hope I described my question correctly. Please help...

Lander
  • 11
  • 2

1 Answers1

1

I don't know snowflake well (I assume it's a language?) I also don't know what format that timestamp is. If 1288834974657 a unix timestamp, it's in the year 42811.

The issue is that this particular timestamp is larger than 32bit. Since you move it up another 32bit, your number overflows. It looks like the long in your language might be unsigned, which means that the maximum number is probably 2^63-1. If the long were unsigned, the maximum number would probably be 2^64-1.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • snowflake is a service for generating unique ID numbers. https://github.com/twitter/snowflake 1288834974657 timestamp accurate to millisecond – Lander Mar 02 '18 at 08:52
  • I want to use SnowFlake "mechanism", now there may be something needs to be improved, to meet my requirements - there may have more bits after timestamp needing "left shift" – Lander Mar 02 '18 at 08:55
  • I see. It helps to share that as well as which language you're using. Anyway, the answer stands: the number you are generating is simply larger than a `long` can handle, so it overflows. – Evert Mar 02 '18 at 08:55
  • Thanks Evert. Please check the java code https://github.com/stevenxia2016/SnowFlakeResearch/blob/master/code.java – Lander Mar 02 '18 at 09:06
  • Thanks, so it's Java. Good to know =) Do you understand my answer? – Evert Mar 02 '18 at 09:07
  • Yes, understood. I dont want to be beat by the "long" limitation. Any idea to improve the SnowFlake algorithm, or any workaround? – Lander Mar 02 '18 at 09:10
  • If I were in your situation I would ask myself, why do I need 1 long to pack all that information. Can you find a way to do this with 2 separate variables? – Evert Mar 02 '18 at 09:11