Generate a unique value for a combination of two numbers

后端 未结 9 921
有刺的猬
有刺的猬 2020-12-16 16:09

Consider I\'ve two numbers 1023232 & 44. I want to generate a unique number representing this combination of numbers. How can i generate it?

Requirement

9条回答
  •  青春惊慌失措
    2020-12-16 16:39

    if those are two ints, you could just do this:

    ulong F(int x, int y) {
        ulong id = x > y ? (uint)y | ((ulong)x << 32) :  
                           (uint)x | ((ulong)y << 32);
        return id;
    }
    

    if you need to generate a truly unique value for two variables of a given size, you need about double the size of each variable. (ok, a bit less now that f(x,y) == f(y,x))

    You could also get your original values back by reversing the same operation.

提交回复
热议问题