title: How to determine the shortest path between two nodes of an infinite, complete binary tree C++
body: <p>this is my first post on Stack Overflow as I'm quite new to programming.
  I'm aware that this question has been asked before but as there was only one answer and it wasn't clear enough for me to understand, I decided to ask again.
  Given an infinite binary tree:</p>
  <p><a href="https://i.stack.imgur.com/GUBFy.png" rel="nofollow noreferrer">Representation of infinite binary tree</a></p>
  <p>And two random nodes, u and v, find the shortest path between them(distance between node 1 and 2 is 1)in a very time efficient method.
  I've currently tried this, but given sizes of up to 500.000 questions of u,v &lt;2.000.000.000, it isn't efficient</p>
  <pre><code>#include &lt;iostream&gt;
  #include&lt;cmath&gt;
  using namespace std;
  int Distance(int x,int y)
  {
      int Levelx=log2(x);
      int Levely=log2(y);
      while(x!=y)
      {
          if(x&lt;y)
              y/=2;
          if(x&gt;y)
              x/=2;
      }
      int LevelOfLCA=log2(x);
      return Levelx+Levely-2*LevelOfLCA;
  }
  int main()
  {
      int NumberOfQuestions;
      cin&gt;&gt;NumberOfQuestions;
      for(int i=1;i&lt;=NumberOfQuestions;i++)
      {
          int x,y;
          cin&gt;&gt;x&gt;&gt;y;
          cout&lt;&lt;Distance(x,y)&lt;&lt;'\n';
      }
  }
  </code></pre>
  <p>Could anyone help me understand a better method of approaching this problem, as I couldn't understand templatetypedef's answer in <a href="https://stackoverflow.com/questions/22960057/shortest-path-between-two-nodes-in-an-infinite-complete-binary-tree">the previous similar question</a> asked.</p>