为两个数字的组合生成唯一值 [英] Generate a unique value for a combination of two numbers
本文介绍了为两个数字的组合生成唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑一下,我有两个数字1023232&44.我想生成一个代表此数字组合的唯一数字.我如何生成它?
Consider I've two numbers 1023232 & 44. I want to generate a unique number representing this combination of numbers. How can i generate it?
要求
f(x,y)= f(y,x)和f(x,y)对于每个(x,y)或(y,x)都是唯一的
f(x,y) = f(y,x) and f(x,y) is unique for every (x,y) or (y,x)
推荐答案
如果这是两个整数,则可以执行以下操作:
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;
}
如果需要为给定大小的两个变量生成一个真正唯一的值,则每个变量的大小大约需要加倍.(好的,现在f(x,y)== f(y,x)少了)
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.
这篇关于为两个数字的组合生成唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文