Best way to convert a signed integer to an unsigned long?

后端 未结 7 1455
清酒与你
清酒与你 2020-11-28 06:25

For certain hash functions in Java it would be nice to see the value as an unsigned integer (e.g. for comparison to other implementations) but Java supports only signed type

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 06:59

    You can use a function like

    public static long getUnsignedInt(int x) {
        return x & (-1L >>> 32);
    }
    

    however in most cases you don't need to do this. You can use workarounds instead. e.g.

    public static boolean unsignedEquals(int a, int b) {
        return a == b;
    }
    

    For more examples of workarounds for using unsigned values. Unsigned utility class

提交回复
热议问题