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:

  1.         function dec2bin(d)
  2.         {
  3.                 var b=;
  4.                 while(d>=1)
  5.                 {
  6.                         b=((d%2>=1)?‘1′:‘0′)+b;
  7.                         d=(d/2);
  8.                 }
  9.                 return b;
  10.         }
  11.  
  12.         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.

  1.         var vanilla     = 8;
  2.         var chocolate   = 4;
  3.         var strawberry  = 2;
  4.         var pistache    = 1;
  5.  
  6.         var icecream;
  7.  
  8.         icecream = vanilla + strawberry;
  9.  
  10.         if (icecream & vanilla) {
  11.                 alert(‘icecream with vanilla’);
  12.         } else {
  13.                 alert(‘icecream without vanilla’);
  14.         }

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

Example 3.

  1.         var icecream = 1010;
  2.         var c = 0;
  3.  
  4.         if (icecream & vanilla) { c++; }
  5.         if (icecream & chocolate) { c++; }
  6.         if (icecream & strawberry) { c++; }
  7.         if (icecream & pistache) { c++; }
  8.  
  9.         // Outputs ‘Icecream holds 2 flavors’.
  10.         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.

1 comment to Converting decimals to base2 (binary) representations.

  • [...] 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. [...]

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>