2

I was looking around the Minecraft's internal packet handling when I saw their VarInt reading code to read the packet length. As a java developer that does not have any course of java I was confused when I saw the statement out |= ( in & 0x7F ) << ( bytes++ * 7 );. Can someone please explain it to me? Thanks in advance!

If you want the whole code, just check the readVarInt function on BungeeCord's Github https://github.com/SpigotMC/BungeeCord/blob/master/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java#L70 .

Ryan Leach
  • 4,262
  • 5
  • 34
  • 71
D4rKDeagle
  • 39
  • 2
  • 2
    It means someone was into writing cryptic code. – Hot Licks Apr 07 '14 at 23:34
  • What do you think it means? Have you looked into it at all? – Sotirios Delimanolis Apr 07 '14 at 23:34
  • Yeah, it isn't really that hard to read, if you take it piece by piece. – Hot Licks Apr 07 '14 at 23:35
  • I haven't really messed with bit shifting and all that too much and yes, I've tried understanding it before posting. I hate people that just post questions like these without trying to understand first myself, so, I would not do that. – D4rKDeagle Apr 07 '14 at 23:36
  • Umm.. what have you tried? Do you understand what the operators are doing? If not, have you tried googling? Start here &= http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html and here >> http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html – Nick Grealy Apr 07 '14 at 23:37
  • 2
    (But it may not be obvious to the novice, even after the piece-by-piece interpretation, that it's simply taking a series of bytes and concatenating together the low-order 7 bits of each byte (assuming that `in` is reloaded from an array or whatever with each iteration).) – Hot Licks Apr 07 '14 at 23:37
  • 2
    Oh and `*` is the multiplication operator. – Sotirios Delimanolis Apr 07 '14 at 23:38
  • I know that * is the multiplication operator :\ Anyway, thanks for your answer Hot Licks, I think I understand it now. – D4rKDeagle Apr 07 '14 at 23:41
  • Useful link for understanding bitwise-ANDing http://stackoverflow.com/questions/19061544/bitwise-anding-with-0xff-is-important. You can also look at the other links for bitwise operators. If you need more help let us know -- perhaps you can clarify your question about what certain parts you don't understand? If it's how bytes work in general, maybe you can clarify that in your question. – aug Apr 07 '14 at 23:45

1 Answers1

2

I didn't look at the link you included, but I would expect this line to be called in a loop to convert a number stored as a collection of 7-bit values back to an int or long.

The line you gave can be expanded to this:

int value = in & 0x7F;            // Grab 7 bits of data from "in"
int shift = bytes * 7;            // Calculate shift amount based on byte index
bytes = bytes+1;                  // Increment byte index (from bytes++)
out = out | (value << shift);     // Shift value and OR into output integer/long
Markus A.
  • 12,349
  • 8
  • 52
  • 116