Unique int to int hash

I am curious if there is any simple and / or known hash method with the following properties:

  • It converts a 32-bit int to another 32-bit int
  • There are no two unequal inputs producing the same output
  • You can’t immediately look at the result that the two inputs were similar (in terms of difference and bitmask), which means that the hash (a) and hash (a + 1) should have significantly different results, like hash (a) and hash (a and 0x100000). (This eliminates just XORing with a random value.)

Although such systems should obviously exist in theory, do they exist in practice?

+3
source share
5 answers

. .

uint8_t arr[32]={4,7,24,9,15,3,...}; // an order you know
uint32_t orgVal;
uint32_t modVal =0;
uint32_t pos = 1;

for (int i=0; i<32;i++) {
  modVal += (orgVal&pos)? (1>>arr[i]):0;
  pos*=2;
}

( IDE , )

, , : 0 1 . xor. .

+2

!

, 32 . :

y = (x * YOUR_ODD_NUMBER) & 0xffffffff;

. , , 3, (, , , 0xffffffff, ), , , .

- :

x ^= x >> YOUR_FIRST_SHIFT;
x ^= x << YOUR_SECOND_SHIFT;
y = x ^ (x >> YOUR_THIRD_SHIFT);

, , . , . , , shift - .

PRNGs, , , . , , , - 1:1, - ( ) . PRNG , , .

- , y x , PRNG, , dieharder.

PRNG , -, PRNG , .

+5

:

17(10) = 1110(2) -> 10111(reversed, set first bit as indicator) = 23
18(10) = 10010(2) -> 101001 = 41

:

17(10) = 11|10(2) -> 1011 = 11
18(10) = 100|10(2) -> 10100 = 20

, , , .

+3

, :

  • 32- .
  • ( ) .

, , , , .

0

: hash(x) = rotate-shl(x, K1) xor C

You can combine a few simple steps to achieve a more "random" results, such as rotate-shl/shr, bit-reverse, xor, notetc.

0
source

Source: https://habr.com/ru/post/1528227/


All Articles