the sad, strange tale of the boy with only two fingers: an introduction to binary for humans
TRANSCRIPT
10101010001111101101100100100111010100111010000110101010110010010100001011110100111010011010101010101110010010110100101010010101010010001100000111101011010101001010010101000101001010010
10001110101111001100100100111010100111010000110101010110010010100001100001 1011110100111010011010101010101110010010110100101010010101010010001100000111101011010101001010010101000101001010010100011101011110110010010100001011
Binary is scary.
• 0…1…10
• 10…11…100
• 1111100…1111101…1111110…1111111… 1000000
Run out of symbols -> start a new column.
128 64 32 16 8 4 2 1 1 1 1 1 1 1 1 1
128+64+32+16+8+4+2+1=255
Value of each Bit in a Byte
Powers of 2
1001 (9)x 1100 (12)——————————-
0000 xxx 00000 x xxx 100100 x xxx+ 1001000 x xxx -—————————————-1101100 (108)
Arithmetic works the same way.
Memory.Turing Machine.
“Turing's greatest contribution to the development of the digital computer [was the] idea of controlling the function
of a computing machine by storing a program of … numerically, encoded instructions in the machine's
memory”
http://www.alanturing.net/
How many 1’s and 0’s determine the maximum size of the number
8 bits (0 to 255) 16 bits (0 to ~64 thousand) 32 bits (0 to ~4 billion)
Signed numbers: Use 1 bit to allow negatives.
This cuts the maximum number in half.
8 bits (-127 to 127) 16 bits (-32K to 32K) 32 bits (-2.2B to 2.2B)
A vastly simplified version… that is still pretty confusing
1 + 1/2 + 1/4 + 1/8 + 1/16…1 0 1 1…
24 bits determine the base number
7 bits for the exponent
1.23456781 bit for the sign
1
0001101
×29-
-632.0987136
An object is a collection of smaller data types.
Keys (property names) are used to reference values stored in the object.
0 0 1 0 0 1 0 1 0 0 1 0 0 1 0 1
Data in memory
Pointer to location 0
Pointer to location 4
Pointer to location 8
Pointers
Can be as simple as 3 numbers… • A location in memory • The size (in bits) of each item • The number of items
An array is a type of object that represents an ordered set of values.
• 1 million + glyphs
• Allows a huge number of characters to be represented with as few bits as possible.
• Uses a form of data compression.
Unicode.
Approximate amplitude
(volume)
Time
Encoding a waveform into binary
Each sample is a 16-bit integer
Samples are played back
thousands of times per second
Colors are 24 bits (aka 3 bytes or 6 hex digits)
Colors sometimes have an alpha channel (32 bits)
Each color channel has 256 possible brightness levels
Typed Arrays ArrayBuffer DataView Bitwise operators |,&,^,~,>>,<<
// Javascript http://developer.mozilla.org/en/docs/Web/JavaScript/Typed_arrays
// Convert Number to binary string let num = 9; let binary = num.toString(2); // “1001”
// Javascript
// Convert binary string to Number let binary = “1001”; let num = parseInt(binary, 2); // 9
// & bitwise AND 1011 & 1100 = 1000
// | bitwise OR 1011 | 1100 = 1111
// ^ bitwise XOR 1011 ^ 1100 = 0111
// Bitwise Operators
// ~ bitwise NOT ~1011 = 0100
// << shift left 1011 << 2 = 101100
// >>> shift right 1011 >>> 2 = 001011
// Bitwise Operators
const redOffset = 16; const greenOffset = 8; const blueOffset = 0; const channelMask = 0xFF;
let getComponents = (color) => { return [color >>> redOffset & channelMask, color >>> greenOffset & channelMask, color >>> blueOffset & channelMask]; };
let orange = 0xFF6633; let [red, green, blue] = getComponents(orange); console.log (red, green, blue); // 255 102 51
// Javascript
Orange 0xFF6633 = 11111111 01000010 00100001 channelMask 0xFF = 00000000 00000000 11111111 = 11111111
// Red 11111111 01000010 00100001 >> 16 = 11111111 01000010 00100001 11111111 & 11111111 = 11111111
// Green 11111111 01000010 00100001 >> 8 = 11111111 01000010 0100001 11111111 01000010 & 11111111 = 11111111 01000010
// Blue 11111111 01000010 00100001 >> 0 = 11111111 01000010 00100001 11111111 01000010 00100001 & 11111111 = 11111111 01000010 00100001
What’s happening (in binary)