<p>Anything that doesn't encode a form of zero is considered to be true. Here's the code Bitcoin uses:</p>

<pre><code>bool CastToBool(const valtype&amp; vch)
{
    for (int i = 0; i &lt; vch.size(); i++)
    {
        if (vch[i] != 0)
        {
            // Can be negative zero
            if (i == vch.size()-1 &amp;&amp; vch[i] == 0x80)
                return false;
            return true;
        }
    }
    return false;
}
</code></pre>

CC BY-SA 3.0
2011-12-27T01:39:59.640Z
2011-12-27T01:39:59.640Z
David Schwartz
<p>In the <a href="https://en.bitcoin.it/wiki/Script" rel="nofollow">Script</a>, when one is checking whether a value is true of false, should the length of the expression be checked? For example, 0x01 is evaluated as true, but can the same be said  about 0x0001, 0x000001, etc?</p>

<development><protocol><script>
Script - true and false values
ThePiachu
# Script - true and false values

(Asked by: username_0 on 2011-12-26)

In the [Script](https://en.bitcoin.it/wiki/Script), when one is checking whether a value is true of false, should the length of the expression be checked? For example, 0x01 is evaluated as true, but can the same be said about 0x0001, 0x000001, etc?

**Tags:** development, protocol, script


---

## 1 answers

### Answer by username_1 on 2011-12-27. Score: 1 | Accepted answer

Anything that doesn't encode a form of zero is considered to be true. Here's the code Bitcoin uses:
bool CastToBool(const valtype& vch)
{
    for (int i = 0; i < vch.size(); i++)
    {
        if (vch[i] != 0)
        {
            // Can be negative zero
            if (i == vch.size()-1 && vch[i] == 0x80)
                return false;
            return true;
        }
    }
    return false;
}


---

