Bits, Bytes and Blobs

 Binary programming in Javascript




          Mrinal Wadhwa
        www.mrinalwadhwa.com
Abstraction
Why think in binary ?
Number Systems
Decimal Numbers
Decimal Numbers
    10 digits
Decimal Numbers
                10 digits
0   1   2   3     4   5     6   7   8   9
Decimal Numbers
                10 digits
0   1   2   3     4   5     6   7   8   9

                base 10
4          2




* the answer to life, universe, and everything
4         2
     1         0
4 * 10 + 2 * 10
4            2
      1           0
4 * 10 + 2 * 10

 40       +   2
4              2
                          1           0
                4 * 10 + 2 * 10

                   40         +   2



               position                          position
digit * base              + ... + digit * base
8                 4              2
          2                  1           0
  8 * 10 + 4 * 10 + 2 * 10

    800       +       40         +   2



                  position                          position
digit * base                 + ... + digit * base
position                          position
digit * base              + ... + digit * base
Binary Numbers
Binary Numbers
    2 digits
Bit
A binary digit.




 0    or    1
Binary Numbers
    2 digits
     0   1

     base 2
1   0
1         0




               position                          position
digit * base              + ... + digit * base
1             0
                          1          0
                 1*2          + 0*2




               position                          position
digit * base              + ... + digit * base
1              0
                          1           0
                 1*2          + 0*2

                    2         +   0



               position                          position
digit * base              + ... + digit * base
1               1              0
         2                  1           0
   1*2       +     1*2          + 0*2

     4       +        2         +   0



                 position                          position
digit * base                + ... + digit * base
Bit
A binary digit.




 0    or    1
Nibble
  A set of 4 bits.




           0/1 0/1 0/1 0/1



Store values 0 to 15
Byte
         A set of 8 bits.




0/1 0/1 0/1 0/1   0/1 0/1 0/1 0/1



      Store values 0 to 255
Hexadecimal Numbers
Hexadecimal Numbers
      16 digits
Hexadecimal Numbers
                            16 digits
0   1   2   3   4   5   6     7   8     9   A   B   C   D   E   F
Hexadecimal Numbers
                            16 digits
0   1   2   3   4   5   6     7   8     9   A   B   C   D   E   F


                            base 16
what’s interesting about Hexadecimal
numbers is that a Hex digit fits exactly
         into a Binary nibble.
1            1             1           1
     3            2             1           0
1*2      +   1*2      +    1*2      + 1*2

 8       +    4       +     2       +   1

                      15

                      F
Byte Order
Big Endian / Network Byte Order


         B3            B2   B1    B0
Most Significant Byte             Least Significant Byte
Decimal 1 stored in a 4 byte big endian word



           0           0   0          1
Most Significant Byte              Least Significant Byte
Little Endian


         B0              B1      B2      B3
Least Significant Byte                   Most Significant Byte
Decimal 1 stored in a 4 byte little endian word



             1           0   0         0
 Least Significant Byte              Most Significant Byte
Numbers in Javascript
64 bit (8 bytes) - IEEE 754 double precision floating-point
Bitwise Operators in JavaScript
The operands of all bitwise operators
are converted to signed 32-bit integers
   in big-endian order and in two's
         complement format.
Bitwise Logical Operators
&           Bitwise AND
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   1   1


0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   0   1




0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   1
|       Bitwise OR
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   1   0   0   0   0   1   0   0   0   0   0   0


0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   1   0   0   0   1   0   0   0   0   0   0




0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   1   1   0   0   0   1   0   0   0   0   0   0
^           Bitwise XOR
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   0   1   1


0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   1   1   1




0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   0   0
~           Bitwise NOT

0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   1




1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   0   1   0
Bitwise Shift Operators
<<              Bitwise Shift Left
                                                            9 << 2

0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   1




0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0   0   1   0   0   1   0   0
>>              Bitwise Sign Propagating Shift Right
                                                            9 >> 2

0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   1




0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0
>>              Bitwise Sign Propagating Shift Right
                                                        -9 >> 2

1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   0   1   1   1




1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   0   1
>>>             Bitwise Zero Fill Shift Right
                                                        -9 >>> 2

1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   0   1   1   1




0   0   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   0   1
Encoding
Text Formats
Binary Formats
Fixed Width Data Types
Variable Width Data Types
Storage in JavaScript
String
   String.charCodeAt(index)
String.fromCharCode(n1, ... nn)
Array of Numbers
Blob
Binary Large Object
BlobBuilder
File Reader
ArrayBuffer
An Array Of Bytes in Memory
ArrayBufferView
A view of a part of the ArrayBuffer
TypedArrays

      ArrayBufferViews where each element is of a certain type -

Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array,
                       Float32Array, Float64Array
TypedArrays
DataView
ImageData
Transport
HTTP
Content-Type: application/octet-stream
         or a custom header
WebSockets
Applications
Faster Calculations
Lighter Data Exchange
Speak Binary Protocols
File Manipulation
?
References
Bitwise Operators
JavaScript Numbers
File API
XmlHttpRequest Level 2
Typed Array
Canvas ImageData
Thank You


http://mrinalwadhwa.com
http://twitter.com/mrinal

Bits, Bytes and Blobs