<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sasgaming.net/index.php?action=history&amp;feed=atom&amp;title=Minecraft%3AJava_Edition_protocol%2FVarInt_and_VarLong</id>
	<title>Minecraft:Java Edition protocol/VarInt and VarLong - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sasgaming.net/index.php?action=history&amp;feed=atom&amp;title=Minecraft%3AJava_Edition_protocol%2FVarInt_and_VarLong"/>
	<link rel="alternate" type="text/html" href="https://wiki.sasgaming.net/index.php?title=Minecraft:Java_Edition_protocol/VarInt_and_VarLong&amp;action=history"/>
	<updated>2026-08-06T18:38:37Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://wiki.sasgaming.net/index.php?title=Minecraft:Java_Edition_protocol/VarInt_and_VarLong&amp;diff=149715&amp;oldid=prev</id>
		<title>SyncBot: Sync: new page from Minecraft</title>
		<link rel="alternate" type="text/html" href="https://wiki.sasgaming.net/index.php?title=Minecraft:Java_Edition_protocol/VarInt_and_VarLong&amp;diff=149715&amp;oldid=prev"/>
		<updated>2026-06-17T11:17:07Z</updated>

		<summary type="html">&lt;p&gt;Sync: new page from Minecraft&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Variable-length format such that smaller numbers use fewer bytes.  These are very similar to [http://developers.google.com/protocol-buffers/docs/encoding#varints Protocol Buffer Varints]: the 7 least significant bits are used to encode the value and the most significant bit indicates whether there&amp;#039;s another byte after it for the next part of the number.  The least significant group is written first, followed by each of the more significant groups; thus, VarInts are effectively little endian (however, groups are 7 bits, not 8).&lt;br /&gt;
&lt;br /&gt;
VarInts are never longer than 5 bytes, and VarLongs are never longer than 10 bytes. Within these limits, unnecessarily long encodings (e.g. &amp;lt;code&amp;gt;81 00&amp;lt;/code&amp;gt; to encode 1) are allowed.&lt;br /&gt;
&lt;br /&gt;
Pseudocode to read and write VarInts:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
int readVarInt() {&lt;br /&gt;
    int value = 0;&lt;br /&gt;
&lt;br /&gt;
    for (int position = 0; position &amp;lt; 32; position += 7) {&lt;br /&gt;
        byte currentByte = readByte();&lt;br /&gt;
&lt;br /&gt;
        // Note: In C this must be performed on an unsigned type to avoid undefined overflow&lt;br /&gt;
        // behavior with negative VarInts.&lt;br /&gt;
        value |= (int)(currentByte &amp;amp; 0x7F) &amp;lt;&amp;lt; position;&lt;br /&gt;
&lt;br /&gt;
        if ((currentByte &amp;amp; 0x80) == 0)&lt;br /&gt;
            return value;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    error(&amp;quot;VarInt too big&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
void writeVarInt(int value) {&lt;br /&gt;
    while ((value &amp;amp; ~0x7F) != 0) {&lt;br /&gt;
        writeByte((value &amp;amp; 0x7F) | 0x80);&lt;br /&gt;
&lt;br /&gt;
        // Note: &amp;gt;&amp;gt;&amp;gt; means that the leftmost bits are filled with zeroes regardless of the sign,&lt;br /&gt;
        // rather than being filled with copies of the sign bit to preserve the sign.&lt;br /&gt;
        // In languages that don&amp;#039;t have a &amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;quot; operator, This behavior can often be selected by&lt;br /&gt;
        // performing the shift on an unsigned type.&lt;br /&gt;
        value &amp;gt;&amp;gt;&amp;gt;= 7;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    writeByte(value);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code may be adapted for VarLongs by changing the type of &amp;lt;code&amp;gt;value&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; and the for loop condition to &amp;lt;code&amp;gt;position &amp;lt; 64&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{{warning|Note Minecraft&amp;#039;s VarInts are identical to {{wikipedia|LEB128}} with the slight change of throwing an exception if it goes over a set amount of bytes.}}&lt;br /&gt;
&lt;br /&gt;
{{warning|Note that Minecraft&amp;#039;s VarInts are not encoded using Protocol Buffers; it&amp;#039;s just similar.  If you try to use Protocol Buffers Varints with Minecraft&amp;#039;s VarInts, you&amp;#039;ll get incorrect results in some cases.  The major differences:&lt;br /&gt;
* Minecraft&amp;#039;s VarInts are all signed, but do not use the ZigZag encoding.  Protocol buffers have 3 types of Varints: &amp;lt;code&amp;gt;uint32&amp;lt;/code&amp;gt; (normal encoding, unsigned), &amp;lt;code&amp;gt;sint32&amp;lt;/code&amp;gt; (ZigZag encoding, signed), and &amp;lt;code&amp;gt;int32&amp;lt;/code&amp;gt; (normal encoding, signed).  Minecraft&amp;#039;s are the &amp;lt;code&amp;gt;int32&amp;lt;/code&amp;gt; variety.  Because Minecraft uses the normal encoding instead of ZigZag encoding, negative values always use the maximum number of bytes.&lt;br /&gt;
* Minecraft&amp;#039;s VarInts are never longer than 5 bytes and its VarLongs will never be longer than 10 bytes, while Protocol Buffer Varints will always use 10 bytes when encoding negative numbers, even if it&amp;#039;s an &amp;lt;code&amp;gt;int32&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Sample VarInts:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
 ! Value !! Hex bytes !! Decimal bytes&lt;br /&gt;
 |-&lt;br /&gt;
 | 0 || 0x00 || 0&lt;br /&gt;
 |-&lt;br /&gt;
 | 1 || 0x01 || 1&lt;br /&gt;
 |-&lt;br /&gt;
 | 2 || 0x02 || 2&lt;br /&gt;
 |-&lt;br /&gt;
 | 127 || 0x7f || 127&lt;br /&gt;
 |-&lt;br /&gt;
 | 128 || 0x80 0x01 || 128 1&lt;br /&gt;
 |-&lt;br /&gt;
 | 255 || 0xff 0x01 || 255 1&lt;br /&gt;
 |-&lt;br /&gt;
 | 25565 || 0xdd 0xc7 0x01 || 221 199 1&lt;br /&gt;
 |-&lt;br /&gt;
 | 2097151 || 0xff 0xff 0x7f || 255 255 127&lt;br /&gt;
 |-&lt;br /&gt;
 | 2147483647 || 0xff 0xff 0xff 0xff 0x07 || 255 255 255 255 7&lt;br /&gt;
 |-&lt;br /&gt;
 | -1 || 0xff 0xff 0xff 0xff 0x0f || 255 255 255 255 15&lt;br /&gt;
 |-&lt;br /&gt;
 | -2147483648 || 0x80 0x80 0x80 0x80 0x08 || 128 128 128 128 8&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
Sample VarLongs:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
 ! Value !! Hex bytes !! Decimal bytes&lt;br /&gt;
 |-&lt;br /&gt;
 | 0 || 0x00 || 0&lt;br /&gt;
 |-&lt;br /&gt;
 | 1 || 0x01 || 1&lt;br /&gt;
 |-&lt;br /&gt;
 | 2 || 0x02 || 2&lt;br /&gt;
 |-&lt;br /&gt;
 | 127 || 0x7f || 127&lt;br /&gt;
 |-&lt;br /&gt;
 | 128 || 0x80 0x01 || 128 1&lt;br /&gt;
 |-&lt;br /&gt;
 | 255 || 0xff 0x01 || 255 1&lt;br /&gt;
 |-&lt;br /&gt;
 | 2147483647 || 0xff 0xff 0xff 0xff 0x07 || 255 255 255 255 7&lt;br /&gt;
 |-&lt;br /&gt;
 | 9223372036854775807 || 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0x7f || 255 255 255 255 255 255 255 255 127&lt;br /&gt;
 |-&lt;br /&gt;
 | -1 || 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0x01 || 255 255 255 255 255 255 255 255 255 1&lt;br /&gt;
 |-&lt;br /&gt;
 | -2147483648 || 0x80 0x80 0x80 0x80 0xf8 0xff 0xff 0xff 0xff 0x01 || 128 128 128 128 248 255 255 255 255 1&lt;br /&gt;
 |-&lt;br /&gt;
 | -9223372036854775808 || 0x80 0x80 0x80 0x80 0x80 0x80 0x80 0x80 0x80 0x01 || 128 128 128 128 128 128 128 128 128 1&lt;br /&gt;
 |}&lt;br /&gt;
&amp;lt;noinclude&amp;gt;&lt;br /&gt;
{{license wiki.vg}}&lt;br /&gt;
&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>SyncBot</name></author>
	</entry>
</feed>