Here I have 2 functions one is in c++ and one is Java. Both have the same purpose: C++:
void VarIntToBufferArray(int* buffer, int in) { std::vector<int> buffVect; int value = in; do { int temp = (int)(value & 0b01111111); value >> 7; if (value != 0) { temp |= 0b10000000; } buffVect.push_back(temp); } while (value != 0); buffer = &buffVect[0]; } Java:
public static void writeVarInt(int value) { do { byte temp = (byte)(value & 0b01111111); // Note: >>> means that the sign bit is shifted with the rest of the number rather than being left alone value >>>= 7; if (value != 0) { temp |= 0b10000000; } writeByte(temp); } while (value != 0); } Something is wrong with C++ version of code because I get this output on C++: 0 0 99 -35 and on Java version: -35 -57 1 which should be. I think >> by itself can't replace >>>=. Any way of doing that?
value >> 7;is a no-op. Did you meanvalue >>= 7;? But for the equivalent of>>>, you probably needvalue = int(unsigned(value) >> 7);In Java, a separate operator for "unsigned shift" is needed because Java lacks unsigned integral types.>>=operator?value = int(unsigned(value) >> 7);, I get output: -858993460 -858993460 -858993460 -858993460VarIntToBufferArrayis an elaborate no-op, as it doesn't have any side effects.int buffer[sizeof(int)]; VarIntToBufferArray(buffer, 25565);