<p>generic big endian MPI format
bitcoin-specific little endian format, with implicit size</p>

<p>python implementation</p>

<p><a href="https://github.com/petertodd/python-bitcoinlib/blob/master/bitcoin/core/_bignum.py" rel="nofollow">https://github.com/petertodd/python-bitcoinlib/blob/master/bitcoin/core/_bignum.py</a></p>

CC BY-SA 3.0
2015-08-05T01:45:29.040Z
This is the right direction. I found the relevant source code in Bitcoind, but I was hoping for a concise explanation of the format if it was documented anywhere outside of code.
Matt
CC BY-SA 3.0
2015-08-03T16:20:44.603Z
2015-08-03T16:20:44.603Z
bitaps.com
<p>CompactSize <strong>Unsigned</strong> Integers are not used in Script, they are used on the Bitcoin protocol to signify the amount of bytes that the next data structure contains. </p>

<p>You are confusing these with ScriptNumbers, <a href="https://github.com/bitcoin/bitcoin/blob/master/src/script/interpreter.cpp#L765-L767" rel="nofollow">which are interpreted for Arithmetic operations inside of the Script Interpreter</a>.</p>

<p>It should be noted that these actually <strong>can</strong> be 5 bytes, however if you perform <strong>another</strong> operation on that 5 byte integer, you will receive an error in the Interpreter. </p>

<p>If you want to see how negative numbers are handled you can look at the <a href="https://github.com/bitcoin/bitcoin/blob/ff46dd4a3457c321aef14f9778094c9db9b00083/src/script/script.h#L210" rel="nofollow">implementation of CScriptNum, which the underlying numeric type is a int64_t</a></p>

CC BY-SA 3.0
2016-07-29T15:21:42.617Z
2016-07-29T15:21:42.617Z
Chris Stewart
<p>I am not sure where you found negative integers. But in most places in bitcoin protocol a compact variable length is used for integers. See reference here <a href="https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer" rel="nofollow">https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer</a></p>

CC BY-SA 3.0
2015-07-03T13:40:55.760Z
The variable length integers are used in the protocol and serialization formats. The compact integer is used in the Script language for representing integer values during operations. So for example, 

decodescript "4f01e40164"￼
{
"asm" : "-1 -100 100",
"type" : "nonstandard",
"p2sh" : "2N8BQEfUK2Q1e6Svt9ro9LkjVeB5tXQJnBh"
}
Matt
CC BY-SA 3.0
2015-07-03T14:26:25.770Z
@Matt, Do not mix Variable_length_integer_encoding (the number of inputs/outputs in transaction) and encoding push operations in scripts. These are different things.
amaclin
CC BY-SA 3.0
2015-07-03T14:57:05.050Z
I'm not mixing it up, my question is about integer encoding for push operations.
Matt
CC BY-SA 3.0
2015-08-02T11:12:38.693Z
This answer is incorrect. The variable length integers in the P2P protocol are not the same as the integer encoding for numbers inside Script.
Pieter Wuille
CC BY-SA 3.0
2015-07-03T07:07:41.640Z
2015-07-03T07:07:41.640Z
dark knight
<p>I've been looking at the compact representation of integers for integer operations in the Script language. It seems that most operations produce or push byte arrays, but when integer operations are performed those arrays are interpreted using a signed compact format.  Is this format actually defined anywhere, or is it strictly bitcoin specific? </p>

<p>As I understand it you can process up to 4 bytes as a integer, the negative of an integer is binary or'd with <code>0x80</code>. I'm having trouble decoding 1-4 byte negative integers into the correct bit string encoding.</p>

<p>Does anyone have any examples, or can point me in the right direction?</p>

CC BY-SA 3.0
2015-08-03T13:05:40.830Z
Related: http://bitcoin.stackexchange.com/q/37415/18196
morsecoder
<bitcoin-core><script><encoding>
Compact integer in Scripts
Matt
# Compact integer in Scripts

(Asked by: username_0 on 2015-07-03)

I've been looking at the compact representation of integers for integer operations in the Script language. It seems that most operations produce or push byte arrays, but when integer operations are performed those arrays are interpreted using a signed compact format. Is this format actually defined anywhere, or is it strictly bitcoin specific?

As I understand it you can process up to 4 bytes as a integer, the negative of an integer is binary or'd with `0x80`. I'm having trouble decoding 1-4 byte negative integers into the correct bit string encoding.

Does anyone have any examples, or can point me in the right direction?

**Tags:** bitcoin-core, script, encoding

> **Comments:**
> **username_1**: Related: http://bitcoin.stackexchange.com/q/37415/18196  

---

## 3 answers

### Answer by username_2 on 2015-08-03. Score: 1

generic big endian MPI format
bitcoin-specific little endian format, with implicit size

python implementation

<https://github.com/petertodd/python-bitcoinlib/blob/master/bitcoin/core/_bignum.py>

> **Comments:**
> **username_0**: This is the right direction. I found the relevant source code in Bitcoind, but I was hoping for a concise explanation of the format if it was documented anywhere outside of code.  

---

### Answer by username_3 on 2016-07-29. Score: 1

CompactSize **Unsigned** Integers are not used in Script, they are used on the Bitcoin protocol to signify the amount of bytes that the next data structure contains.

You are confusing these with ScriptNumbers, [which are interpreted for Arithmetic operations inside of the Script Interpreter](https://github.com/bitcoin/bitcoin/blob/master/src/script/interpreter.cpp#L765-L767).

It should be noted that these actually **can** be 5 bytes, however if you perform **another** operation on that 5 byte integer, you will receive an error in the Interpreter.

If you want to see how negative numbers are handled you can look at the [implementation of CScriptNum, which the underlying numeric type is a int64\_t](https://github.com/bitcoin/bitcoin/blob/ff46dd4a3457c321aef14f9778094c9db9b00083/src/script/script.h#L210)


---

### Answer by username_4 on 2015-07-03. Score: -1

I am not sure where you found negative integers. But in most places in bitcoin protocol a compact variable length is used for integers. See reference here <https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer>

> **Comments:**
> **username_0**: The variable length integers are used in the protocol and serialization formats. The compact integer is used in the Script language for representing integer values during operations. So for example,   decodescript "4f01e40164"￼ { "asm" : "-1 -100 100", "type" : "nonstandard", "p2sh" : "2N8BQEfUK2Q1e6Svt9ro9LkjVeB5tXQJnBh" }  
> **username_5**: @username_0, Do not mix Variable_length_integer_encoding (the number of inputs/outputs in transaction) and encoding push operations in scripts. These are different things.  
> **username_0**: I'm not mixing it up, my question is about integer encoding for push operations.  
> **username_6**: This answer is incorrect. The variable length integers in the P2P protocol are not the same as the integer encoding for numbers inside Script.  

---

