//this code prints the binary representation of number x

//--------------------------
//the parameter of the code, it must be positive

x = 62762;

//-----------------
//the actual code

//this code is complicated because we print the binary
//number starting with the most significant bit

print "The binary representation of ";
print x; 
print " is: ";

if x == 0 then
  
  print "0";

else

  //print the most significant 1 
  print "1"; 

  //compute the highest power of 2 smaller than "x"

  power2 = 2;  //will hold the resulting power of 2
  oldexp = 1; //will hold the position of the most significant bit 

  while power2 <= x  do     
    power2 = power2 * 2;     
    oldexp = oldexp + 1;
  endwhile

  oldexp = oldexp - 1;
  power2 = power2/2;

  temp = x - power2;    //holds the remaining binary contents of x

  while temp > 0 do

    //compute the highest power of 2 smaller than "temp"

    power2 = 2;  //will hold the resulting power of 2
    exp = 1;     //exp will hold the exponent of 2
                 //essentialy, exp holds the position
                 //of the current most significant bit of "temp"
    
    while power2 <= temp  do     
      power2 = power2 * 2;     
      exp = exp + 1;
    endwhile

    exp = exp - 1;
    power2 = power2/2;
     
    //compute the distance of the previous 1 bit with
    //the current 1 bit.

    distance = oldexp - exp - 1;

    //print the 0's between two consecutive 1's 
      
    i = 0;
    while i < distance do
      print "0";
      i = i+1;
    endwhile

    //print the the current most significant 1 
    print "1"; 
      
    temp = temp - power2;
    oldexp = exp;

  endwhile

  //print the last 0's
 
  i = 0;
  while i < oldexp do
    print "0";
    i = i+1;
  endwhile

endif

print newline;