Javascript binary file reading
<p>From <a href="http://blog.vjeux.com/wp-content/uploads/2010/01/binaryReader.js" rel="nofollow">here</a>:</p>

<pre><code>_shl: function (a, b){
        for (++b; --b; a = ((a %= 0x7fffffff + 1) &amp; 0x40000000) == 0x40000000 ? a * 2 : (a - 0x40000000) * 2 + 0x7fffffff + 1);
        return a;
    },

_readByte: function (i, size) {
        return this._buffer.charCodeAt(this._pos + size - i - 1) &amp; 0xff;
    },

_readBits: function (start, length, size) {
        var offsetLeft = (start + length) % 8;
        var offsetRight = start % 8;
        var curByte = size - (start &gt;&gt; 3) - 1;
        var lastByte = size + (-(start + length) &gt;&gt; 3);
        var diff = curByte - lastByte;

        var sum = (this._readByte(curByte, size) &gt;&gt; offsetRight) &amp; ((1 &lt;&lt; (diff ? 8 - offsetRight : length)) - 1);

        if (diff &amp;&amp; offsetLeft) {
            sum += (this._readByte(lastByte++, size) &amp; ((1 &lt;&lt; offsetLeft) - 1)) &lt;&lt; (diff-- &lt;&lt; 3) - offsetRight; 
        }

        while (diff) {
            sum += this._shl(this._readByte(lastByte++, size), (diff-- &lt;&lt; 3) - offsetRight);
        }

        return sum;
    },
</code></pre>

<p>This code does binary file reading. Unfortunately this code isn't documented.
I would like to understand how it works. (especially _readBits and _shl methods)
In _readBits what's offsetright for? also curByte and lastByte:
i thought this way about it:</p>

<p>_readBits(0,16,2) curByte becomes 1. lastByte becomes 0. Why is lastByte  less than curByte? Or i made a mistake? Where? Please, help! </p>
