/*
 * Copyright (C) 2014 Pedro Vicente Gómez Sánchez.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.pedrovgs.problem71;

/**
 * Given an integer passed as parameter, can you write a method to return the reverse binary
 * number? The reverse binary number is defined like the number where every 1 in the original
 * number has a 0 in the reversed number and the same with 0 in the original number.
 *
 * For example:
 *
 * Input: 63 = 111111
 * Output: 0
 *
 * Input = 5 = 101
 * Output = 2 = 010
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class ReverseBinaryNumber {

}

@@ -645,16 +645,82 @@
 lem71;%0A%0A
+import com.github.pedrovgs.problem70.ReverseOrderOfBinaryNumber;%0A%0A
 /**%0A * G
@@ -1169,10 +1169,1204 @@
 mber %7B%0A%0A
+  private ReverseOrderOfBinaryNumber reverseOrder;%0A%0A  public ReverseBinaryNumber() %7B%0A    reverseOrder = new ReverseOrderOfBinaryNumber();%0A  %7D%0A%0A  /**%0A   * Iterative algorithm based on binary operators. The key of this algorithm is take in to account%0A   * the number of digits of the binary representation. First we are going to create the reverse%0A   * number with a 1 as first binary number. Then we are going to revert the order of the binary%0A   * number and at the end we are going to remove the first one we added to take into account the%0A   * number of digits of this binary number. The complexity order of this algorithm in space terms%0A   * is O(1) and in time terms is equals to O(N) where N is the number of digits in the binary%0A   * representation.%0A   */%0A  public int reverse(int input) %7B%0A    int result = 1;%0A    do %7B%0A      result %3C%3C= 1;%0A      result %7C= (input & 1) == 1 ? 0 : 1;%0A      input %3E%3E= 1;%0A    %7D while (input != 0);%0A    result = reverseBitsOrder(result);%0A    result = removeFirstOne(result);%0A    return result;%0A  %7D%0A%0A  private int removeFirstOne(int number) %7B%0A    return number %3E%3E 1;%0A  %7D%0A%0A  private int reverseBitsOrder(int number) %7B%0A    return reverseOrder.reverse(number);%0A  %7D%0A
 %7D%0A

Add solution to problem 71