80
Bits, Bytes and Blobs Mrinal Wadhwa www.mrinalwadhwa.com Binary programming in Javascript

Bits, Bytes and Blobs

Embed Size (px)

DESCRIPTION

A presentation introducing binary programming in JavaScript, That I presented at jsFoo Bangalore - October 1, 2011

Citation preview

Page 1: Bits, Bytes and Blobs

Bits, Bytes and Blobs

Mrinal Wadhwawww.mrinalwadhwa.com

Binary programming in Javascript

Page 2: Bits, Bytes and Blobs

Abstraction

Page 3: Bits, Bytes and Blobs

Why think in binary ?

Page 4: Bits, Bytes and Blobs

Number Systems

Page 5: Bits, Bytes and Blobs

Decimal Numbers

Page 6: Bits, Bytes and Blobs

Decimal Numbers10 digits

Page 7: Bits, Bytes and Blobs

Decimal Numbers

0 1 2 3 4 5 6 7 8 9

10 digits

Page 8: Bits, Bytes and Blobs

Decimal Numbers

0 1 2 3 4 5 6 7 8 9

10 digits

base 10

Page 9: Bits, Bytes and Blobs

24

* the answer to life, universe, and everything

Page 10: Bits, Bytes and Blobs

2 * 1004 * 101 +

24

Page 11: Bits, Bytes and Blobs

2 * 1004 * 101 +

24

40 + 2

Page 12: Bits, Bytes and Blobs

2 * 1004 * 101 +

24

40 + 2

digit * base position + ... + digit * base position

Page 13: Bits, Bytes and Blobs

2 * 1004 * 101 +

248 * 102 +

8

40 + 2800 +

digit * base position + ... + digit * base position

Page 14: Bits, Bytes and Blobs

digit * base position + ... + digit * base position

Page 15: Bits, Bytes and Blobs

Binary Numbers

Page 16: Bits, Bytes and Blobs

Binary Numbers2 digits

Page 17: Bits, Bytes and Blobs

BitA binary digit.

0 1or

Page 18: Bits, Bytes and Blobs

Binary Numbers

base 2

0 1

2 digits

Page 19: Bits, Bytes and Blobs

01

Page 20: Bits, Bytes and Blobs

01

digit * base position + ... + digit * base position

Page 21: Bits, Bytes and Blobs

0 * 201 * 21 +

01

digit * base position + ... + digit * base position

Page 22: Bits, Bytes and Blobs

0 * 201 * 21 +

01

2 + 0

digit * base position + ... + digit * base position

Page 23: Bits, Bytes and Blobs

0 * 201 * 21 +

01

2 + 0

1 * 22 +

1

4 +

digit * base position + ... + digit * base position

Page 24: Bits, Bytes and Blobs

BitA binary digit.

0 1or

Page 25: Bits, Bytes and Blobs

NibbleA set of 4 bits.

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

Store values 0 to 15

Page 26: Bits, Bytes and Blobs

ByteA set of 8 bits.

0/1 0/1 0/1 0/10/1 0/1 0/1 0/1

Store values 0 to 255

Page 27: Bits, Bytes and Blobs

Hexadecimal Numbers

Page 28: Bits, Bytes and Blobs

Hexadecimal Numbers16 digits

Page 29: Bits, Bytes and Blobs

Hexadecimal Numbers

0 1 2 3 4 5 6 7 8 9 A B C D E F

16 digits

Page 30: Bits, Bytes and Blobs

Hexadecimal Numbers

0 1 2 3 4 5 6 7 8 9 A B C D E F

16 digits

base 16

Page 31: Bits, Bytes and Blobs

what’s interesting about Hexadecimal numbers is that a Hex digit fits exactly

into a Binary nibble.

Page 32: Bits, Bytes and Blobs

1 * 201 * 21 +

11

2 + 1

1 * 22 +

1

4 +

1 * 23 +

1

8 +

15

F

Page 33: Bits, Bytes and Blobs

Byte Order

Page 34: Bits, Bytes and Blobs

Big Endian / Network Byte Order

B0B1B2B3Most Significant Byte Least Significant Byte

Page 35: Bits, Bytes and Blobs

1000

Decimal 1 stored in a 4 byte big endian word

Most Significant Byte Least Significant Byte

Page 36: Bits, Bytes and Blobs

Little Endian

B3B2B1B0Least Significant Byte Most Significant Byte

Page 37: Bits, Bytes and Blobs

0001

Decimal 1 stored in a 4 byte little endian word

Least Significant Byte Most Significant Byte

Page 38: Bits, Bytes and Blobs

Numbers in Javascript

64 bit (8 bytes) - IEEE 754 double precision floating-point

Page 39: Bits, Bytes and Blobs

Bitwise Operators in JavaScript

Page 40: Bits, Bytes and Blobs

The operands of all bitwise operators are converted to signed 32-bit integers

in big-endian order and in two's complement format.

Page 41: Bits, Bytes and Blobs

Bitwise Logical Operators

Page 42: Bits, Bytes and Blobs

& 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

Page 43: Bits, Bytes and Blobs

| 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

Page 44: Bits, Bytes and Blobs

^ 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

Page 45: Bits, Bytes and Blobs

~ 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

Page 46: Bits, Bytes and Blobs

Bitwise Shift Operators

Page 47: Bits, Bytes and Blobs

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 Shift Left

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 10 01 0

9 << 2

Page 48: Bits, Bytes and Blobs

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 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 0 010

Page 49: Bits, Bytes and Blobs

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

>> 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 01 1 1

Page 50: Bits, Bytes and Blobs

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

>>> Bitwise Zero Fill Shift Right

-9 >>> 2

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 01 1 1

Page 51: Bits, Bytes and Blobs

Encoding

Page 52: Bits, Bytes and Blobs

Text Formats

Page 53: Bits, Bytes and Blobs

Binary Formats

Page 54: Bits, Bytes and Blobs

Fixed Width Data Types

Page 55: Bits, Bytes and Blobs

Variable Width Data Types

Page 56: Bits, Bytes and Blobs

Storage in JavaScript

Page 57: Bits, Bytes and Blobs

String

String.charCodeAt(index)String.fromCharCode(n1, ... nn)

Page 58: Bits, Bytes and Blobs

Array of Numbers

Page 59: Bits, Bytes and Blobs

BlobBinary Large Object

Page 60: Bits, Bytes and Blobs
Page 61: Bits, Bytes and Blobs
Page 62: Bits, Bytes and Blobs

BlobBuilder

Page 63: Bits, Bytes and Blobs

File Reader

Page 64: Bits, Bytes and Blobs

ArrayBufferAn Array Of Bytes in Memory

Page 65: Bits, Bytes and Blobs

ArrayBufferViewA view of a part of the ArrayBuffer

Page 66: Bits, Bytes and Blobs

TypedArrays

ArrayBufferViews where each element is of a certain type -

Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array

Page 67: Bits, Bytes and Blobs

TypedArrays

Page 68: Bits, Bytes and Blobs

DataView

Page 69: Bits, Bytes and Blobs

ImageData

Page 70: Bits, Bytes and Blobs

Transport

Page 71: Bits, Bytes and Blobs

HTTP

Content-Type: application/octet-stream

or a custom header

Page 72: Bits, Bytes and Blobs

WebSockets

Page 73: Bits, Bytes and Blobs

Applications

Page 74: Bits, Bytes and Blobs

Faster Calculations

Page 75: Bits, Bytes and Blobs

Lighter Data Exchange

Page 76: Bits, Bytes and Blobs

Speak Binary Protocols

Page 77: Bits, Bytes and Blobs

File Manipulation

Page 78: Bits, Bytes and Blobs

?

Page 80: Bits, Bytes and Blobs

Thank You

http://mrinalwadhwa.com

http://twitter.com/mrinal