user133732: Testnet transaction c474c67cabc13f74c65e7cf503db971d5ba176794bff7de36f46d51ca675cf3a locked 0.0005BTC with a simple math script:
OP_2DUP
OP_ADD
5102
OP_EQUALVERIFY
OP_SUB
230d
OP_EQUAL

Transaction b6714e05dd014cc92de09271886e62d85e7d41dd44fad417c520eb51d57f94c0 spent that output with the sigscript:
ba07
6985

I can't see how (ba07 + 6985) == 5102 or (ba07 - 6985) == 230d.
What am I missing about Bitcoin script, integers, and math?

user114204: For mathematical operations, Bitcoin treats byte vectors as signed (signed magnitude) little endian values. So let's look what the numbers are then:

The byte vector ba07 is the hexadecimal number (0)7ba.
The byte vector 6985 is 8569 in big endian. Since the highest bit is set the number is negative. So the hexadecimal number is -(0)569.
The byte vector 5102 is the hexadecimal number (0)251.
The byte vector 0d23 is the hexadecimal number (0)23d.

Therefore we end up with the two statements 7ba+(-569)=251 and 7ba-(-569)=d23 which are true.

133732: <p>Testnet transaction <code>c474c67cabc13f74c65e7cf503db971d5ba176794bff7de36f46d51ca675cf3a</code> locked 0.0005BTC with a simple math script:</p>
<pre><code>OP_2DUP
OP_ADD
5102
OP_EQUALVERIFY
OP_SUB
230d
OP_EQUAL
</code></pre>
<p>Transaction <code>b6714e05dd014cc92de09271886e62d85e7d41dd44fad417c520eb51d57f94c0</code> spent that output with the sigscript:</p>
<pre><code>ba07
6985
</code></pre>
<p>I can't see how <code>(ba07 + 6985) == 5102</code> or <code>(ba07 - 6985) == 230d</code>.</p>
<p>What am I missing about Bitcoin script, integers, and math?</p>
 114204: <p>For mathematical operations, Bitcoin treats byte vectors as signed (signed magnitude) little endian values. So let's look what the numbers are then:</p>
<ul>
<li>The byte vector <code>ba07</code> is the hexadecimal number (0)7ba.</li>
<li>The byte vector <code>6985</code> is <code>8569</code> in big endian. Since the highest bit is set the number is negative. So the hexadecimal number is -(0)569.</li>
<li>The byte vector <code>5102</code> is the hexadecimal number (0)251.</li>
<li>The byte vector <code>0d23</code> is the hexadecimal number (0)23d.</li>
</ul>
<p>Therefore we end up with the two statements <code>7ba+(-569)=251</code> and <code>7ba-(-569)=d23</code> which are true.</p>
