Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7710833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:13:21+00:00 2026-06-01T01:13:21+00:00

I am trying to generate unique values in c# with the help of DateTime

  • 0

I am trying to generate unique values in c# with the help of DateTime ticks and and incrementing number.
Pseudo code:

  1. Take last 43 significant bits from DateTime.Now ticks (lets name it A)
  2. Take last 21 bits from increasing sequence (lets name it ‘B’)
  3. Left shift ‘A’ 21 times (lets name it ‘C’)
  4. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:13:22+00:00Added an answer on June 1, 2026 at 1:13 am

    Is this logic good enough to generate unique numbers

    Unique across what scope? Across multiple computers/processes/AppDomains?, certainly not. Within a single AppDomain? Not really. Generating 2 million numbers is irrelevant – that’s just testing that your sequence part works. (221 is just over 2 million.)

    If you can call GetUniqueNumber 221+1 times within the granularity of DateTime.Now (which is likely to be ~10-15ms) then you’ll get a repeat. Have you measured how fast your computed can call this?

    Then there’s the fact that those 43 bits will be repeated in 243 ticks’ time… or at least would be if you had a sufficiently fine-grained clock. (And sooner or later the granularity will work against you.)

    I find that some generated numbers are ‘-ve’ which I didn’t understand.

    Whenever dateTimeNo has its top bit (out of 43) set, you’ll end up with a long with the top bit set – which means it’ll be negative.

    EDIT: Also note that your shifting is broken. This:

    const long dateTimeMask = ~(0L) >> timeShift;
    

    is performed a sign-extended shift – so you’re just ending up with ~0L.

    In short: use Guid.NewGuid. It’s what it’s there for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand crc32 to generate the unique url for web page.
Im trying to generate a color gradient using ColdFusion. My current code below works
I am trying to generate a list of all possible number combinations within a
i am trying to generate unique subsets of length 3 from integers ranging from
I am trying to generate a random enough number quickly. Right Now I am
I am trying to use the Data.Unique in Haskell. The newUnique can generate an
I'm trying to solve this problem where I have a unique array of values
I am trying to generate a unique token for every user on my site.
This is a Javascript / jQuery question: I'm trying to generate six unique random
I am trying to solve a unique problem. Thanks for any help with this.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.