class MergeBinaryNumbers {

  public int merge(int n, int m, int i, int j) {
    int maskFullOfOnes = ~0;

    int fullOfOnesBeforeJ = maskFullOfOnes << (j + 1);
    int fullOfOnesAfterI = ((1 << i) - 1);

    int allOnesExceptIandJ = fullOfOnesBeforeJ | fullOfOnesAfterI;

    int clearBitsBetweenIandJ = n & allOnesExceptIandJ;
    int moveMToTheCorrectPosition = m << i;

    return clearBitsBetweenIandJ | moveMToTheCorrectPosition;
  }

}