I am trying to generate unique values in c# with the help of DateTime ticks and and incrementing number. Pseudo code:
- Take last 43 significant bits from DateTime.Now ticks (lets name it A)
- Take last 21 bits from increasing sequence (lets name it 'B')
- Left shift 'A' 21 times (lets name it 'C')
- Do binary OR in A and C
I ran the test for generating 2 million number and inserting in database column which has unique constraint set and it ran successfully.
Here is the piece of code that does that:
private static long _sequence = 1;
public static long GetUniqueNumber()
{
const int timeShift = 21;
var dateTime = DateTime.Now.Ticks;
const long dateTimeMask = ~(0L) >> timeShift;
const long sequenceMask = ((~(0L) >> (64 - timeShift)));
var seq = Interlocked.Increment(ref _sequence);
var dateTimeNo = (dateTimeMask & dateTime) << timeShift;
var seqNum = (seq & sequenceMask);
var num = dateTimeNo | seqNum;
return num;
}
I have two questions: 1. Is this logic good enough to generate unique numbers ? 2. I find that some generated numbers are '-ve' which I didn't understand.
Any help/suggestions/improvements are welcome.