Edit in JSFiddle

<pre>
<code>$('.red').compare($('.green')) = <span id='result'></span></code>
</pre>
<div id="demo-html" style="margin: 0;">
<div>
    A
    <div>A.1</div>
    <div>A.2</div>
</div>
<div>
    B
</div>
</div>

<table>
    <tr><th>Bitmask</th><th>Number</th><th>Meaning</th></tr>
    <tr class="no_0"><td>000000</td><td>0</td><td>.red and .green are identical</td></tr>
    <tr class="no_1"><td>000001</td><td>1</td><td>The nodes are in different documents (or one is outside of a document)</td></tr>
    <tr class="no_2"><td>000010</td><td>2</td><td>.green precedes .red</td></tr>
    <tr class="no_4"><td>000100</td><td>4</td><td>.red precedes .green</td></tr>
    <tr class="no_8"><td>001000</td><td>8</td><td>.green contains .red</td></tr>
    <tr class="no_16"><td>010000</td><td>16</td><td>.red contains .green</td></tr>
</table>
var tableNumbers = [0, 1, 2, 4, 8, 16],
    placing = 'red';
    
$('div').click(function(ev){
  $('.'+placing).removeClass(placing)
  $(this).addClass(placing) 
  placing = placing == 'red' ? 'green' : 'red';
  ev.stopPropagation();
  if($('.green').length){
      var result = $('.red').compare($('.green'))
      $("#result").text(result);
      $('tr.matches').removeClass('matches');
      $.each(tableNumbers, function(i, value) {
         if(result & value || result === value) {
             $('tr.no_' + value).addClass('matches');
         }
      });
  }
})

$(function(){
    if(window.parent == window){
        $('.hide').show()
    }
})
  body {
    font-family: verdana;
  }

  div {
    border: solid 1px black;
    margin: 5px;
    padding: 5px;
    font-size: 12px;
  }

  pre {
    margin-bottom: 10px;
  }

  .red {
    background-color: red;
  }

  .green {
    background-color: green;
  }

  .hide {
    display: none;
  }

  h3 {
    margin: 20px 0px 0px 0px;
  }

  table {
    margin-top: 20px;
    border-collapse: collapse;
  }

  th, td {
    border: 1px solid gray;
    padding: 5px;
  }

  th {
    color: gray;
    text-align: left;
  }

  .matches {
    background-color: yellow;
  }