private const int UNUSED_BITS = 1;
private const int EPOCH_BITS = 41;
private const int NODE_ID_BITS = 10;
private const int SEQUENCE_BITS = 12;
private const long CUSTOM_EPOCH = 1609459200000L;
public static void Main()
long currentTimestamp = GetTimestamp();
GenerateID(923,currentTimestamp);
GenerateID(923,currentTimestamp+1);
public static void GenerateID(int nodeId, long currentTimestamp)
Console.WriteLine($"GenerateId - nodeId - {nodeId}, ts - {currentTimestamp}");
long shiftedts = currentTimestamp << (NODE_ID_BITS + SEQUENCE_BITS);
long shiftednodeid = _nodeId << SEQUENCE_BITS;
Console.WriteLine($"-----------------------------------------------------------------------");
Console.WriteLine(LongToBinary(shiftedts, "shiftedts"));
Console.WriteLine(LongToBinary(shiftednodeid, "shiftednodeid"));
Console.WriteLine(LongToBinary(_sequence, "sequence"));
long id = shiftedts + shiftednodeid + _sequence;
Console.WriteLine("-------------------------------------------------------------------------------");
Console.WriteLine(LongToBinary(id, "id - final"));
public static string LongToBinary(long value, string name)
string binary = Convert.ToString(value, 2).PadLeft(64, '0');
return $"{binary} - {value} - {name}";
private static long ToEpochMillisec(DateTime dateTime) => (long)(dateTime - DateTime.UnixEpoch).TotalMilliseconds;
private static long GetTimestamp()
return ToEpochMillisec(DateTime.UtcNow) - CUSTOM_EPOCH;