Java storing two ints in a long

前端 未结 2 1817
旧时难觅i
旧时难觅i 2020-12-03 06:43

I want to store two ints in a long (instead of having to create a new Point object every time).

Currently, I tried this. It\'s not working, but I don\'t

2条回答
  •  余生分开走
    2020-12-03 07:09

    Here is another option which uses a bytebuffer instead of bitwise operators. Speed-wise, it is slower, about 1/5 the speed, but it is much easier to see what is happening:

    long l = ByteBuffer.allocate(8).putInt(x).putInt(y).getLong(0);
    //
    ByteBuffer buffer = ByteBuffer.allocate(8).putLong(l);
    x = buffer.getInt(0);
    y = buffer.getInt(4);
    

提交回复
热议问题