Converting decimals to base2 (binary) representations.
The base2 or binary representation uses combinations of the 2 values: 0 and 1 to form any number. This is done by using the unique functionalities of the powers of 2 sequence.
Each value in the powers of 2 sequence is unique in that it can not be formed by any combination of the others. For example: There is no way to get 8 using more then one of these numbers because the addition of all numbers lower than 8 (1, 2, 4) is 7.
It is possible to form any number by combining these numbers. But any number can be formed with only one unique combination. For example: the number 7 is formed out of 1, 2 and 4. The number 9 out of 8 and 1.
If we make a sequence of 8 powers of 2 and represent them either as zeros or ones, a zero if we don’t use the power of 2 in our combination, a one if we do.
The representation of the number 9 would then look like this:

function dec2bin(d)

{

var b=”;

while(d>=1)

{

b=((d%2>=1)?‘1′:‘0′)+b;

d=(d/2);

}

return b;

}


alert( dec2bin(30) ); // Outputs ‘11110′.
This program takes a decimal d and keeps dividing it by two ( d=(d/2) ) while the resulting number is higher or equal to one ( while(d>=1) ). It determines the remainder of each division and accordingly prepends a zero or one to the return value b ( b=((d%2>=1)?’1′:’0′)+b ).
Basic Usage of base 2 sequences.
Anne eats one icecream every day. For the sake of historic recollection she wants to store the flavors used. There are 4 flavors: vanilla, chocolate, strawberry and pistache. How should she store it so she can easily determine which flavor is used in a certain icecream?
The answer is simple:
Let’s give each flavor a unique power of 2 value:
vanilla = 8 (1000) chocolate = 4 (0100) strawberry = 2 (0010) pistache = 1 (0001)
If Anne’s icecream has vanilla and strawberry that would make
8 + 2 = 10
In base 2 notation that is:
1010
If we would like to know if the flavor vanilla is used in a particular icecream we need to perform a little math; If a 1 occurs at both the icecream and vanilla sequences at the same spot (eg. first from the left) we place a 1 in a result sequence at the same spot. Any other combination (0 and 1, 0 and 0 or 1 and 0) returns 0. Our resulting line now shows:
vanilla = 1000 icecream = 1010 ====================== result = 1000
This result sequence is the same as the vanilla sequence; so vanilla is used in the icecream.
In scripting/programming languages this operation (bitwise AND) is mostly represented with the & (ampersant) symbol.
Example 2.

var vanilla = 8;

var chocolate = 4;

var strawberry = 2;

var pistache = 1;


var icecream;


icecream = vanilla + strawberry;


if (icecream & vanilla) {

alert(‘icecream with vanilla’);

} else {

alert(‘icecream without vanilla’);

}
In order to count how many flavors are used in an icecream we just test for all the flavors.
Example 3.

var icecream = 1010;

var c = 0;


if (icecream & vanilla) { c++; }

if (icecream & chocolate) { c++; }

if (icecream & strawberry) { c++; }

if (icecream & pistache) { c++; }


// Outputs ‘Icecream holds 2 flavors’.

alert(‘Icecream holds ‘+c+‘ flavors’);
Using base2 notation we can store information about the kind and number of flavors used in an icecream using a combination of only 4 one’s and/or zero’s. Comparing this to storing the actual names of the flavors you have to know that every character uses at least 8 zero’s or one’s in base2 notation. For the word vanilla that would add up to 7*8 = 56.
[...] bash binary boolean C++ canvas chapter 2 chrome coding Computer Systems Book conversion decimal …Converting decimals to base2 (binary) representations …The base2 or binary representation uses combinations of the 2 values: 0 and 1 to form any number. [...]