default IntSerializer seems to save integer value shifted by one to the left

22 views
Skip to first unread message

Tomaž Kunaver

unread,
Jul 11, 2018, 6:04:53 PM7/11/18
to kryo-users
Hi,
I have the following problem:

 Kryo kryo = new Kryo();
 
Output output = new Output(getSaveFileHandle().write(false));
 
int state = 1;
 kryo
.writeObject(output, state);
 output
.close();

When I open the generated file in a hex editor, it contains value 2. In general I've seen that values get shifted by 1 to the left (like doing "value << 1").

Am I missing something?

Nate

unread,
Jul 11, 2018, 6:46:59 PM7/11/18
to kryo-users
You can see how ints are written in the source. Try stepping through. Is there a problem you are worried about?


--
You received this message because you are subscribed to the "kryo-users" group.
http://groups.google.com/group/kryo-users
---
You received this message because you are subscribed to the Google Groups "kryo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kryo-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tomaž Kunaver

unread,
Jul 12, 2018, 2:33:35 AM7/12/18
to kryo-...@googlegroups.com
Well yes, the problem is that the values get shifted. This means that I lose 1 bit, which is something I need to be aware of when programming, since I use bit masks to save boolean flags to file. Anyway, looking on the net, I found out that the most recent version of Kryo is 5.00 RC1, and I am using version 2.21, so I think I'll update first and recheck the behaviour :)

Tomaž Kunaver

unread,
Jul 12, 2018, 3:34:28 AM7/12/18
to kryo-...@googlegroups.com
OK I've looked into the problem. I can't use version 5.0 since I'm using KryoNet which uses version 2.21, but it doesn't matter. Anyway, the value does get shifted to the left by 1 bit, which can be seen in the code of Output.java (both 2.21 and 5.0 versions). The code is within "writeVarInt" method:

public int writeVarInt (int value, boolean optimizePositive) throws KryoException {
  if (!optimizePositive) value = (value << 1) ^ (value >> 31);
...

In other words, it seems to try to save the int sign in bit position 0, this is why it shifts everything to the left. Not sure if this code is correct though, I think it should use logical shift rather than arithmetic one (>>> instead of >>) if it wants to move the sign to the right.

Nate

unread,
Jul 12, 2018, 9:58:18 AM7/12/18
to kryo-users
See here about variable length encoding:
Kryo writes and reads values correctly. That you don't see your exact value in the binary data doesn't mean it is wrong.

You seem to be focused on how Kryo stores values rather than a functional problem. Digging around in the inner workings of Kryo is great, but if you have encountered an actual problem then an example showing what isn't working as expected would be helpful.

KryoNet can be modified locally to use v5, or you can provide it a KryoSerialization that uses v5. I haven't had time to update it to v5 but doing so should be very easy.

Reply all
Reply to author
Forward
0 new messages