Symmetric Bijective Algorithm for Integers

后端 未结 11 890
甜味超标
甜味超标 2020-12-01 03:33

I need an algorithm that can do a one-to-one mapping (ie. no collision) of a 32-bit signed integer onto another 32-bit signed integer.

My real concern is enough entr

11条回答
  •  误落风尘
    2020-12-01 04:09

    Split the number in two (16 most significant bits and 16 least significant bits) and consider the bits in the two 16-bit results as cards in two decks. Mix the decks forcing one into the other.

    So if your initial number is b31,b30,...,b1,b0 you end up with b15,b31,b14,b30,...,b1,b17,b0,b16. It's fast and quick to implement, as is the inverse.

    If you look at the decimal representation of the results, the series looks pretty obscure.

    You can manually map 0 -> maxvalue and maxvalue -> 0 to avoid them mapping onto themselves.

提交回复
热议问题