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

后端 未结 7 1454
清酒与你
清酒与你 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:56

    Just my 2 cents here, but I think it's a good practice to use:

    public static long getUnsignedInt(int x) { return x & (~0L); // ~ has precedence over & so no real need for brackets }

    instead of:

    return x & 0xFFFFFFFFL;

    In this situation there's not your concern how many 'F's the mask has. It shall always work!

提交回复
热议问题