
// weight of a nonnegative integer x is the number of bits that a re set to one in its binary representation.
// e.g. 92 in binary is 1011100 and therefore its weight is 4.
// write a program which takes a positive int x and returns a value(y) which is not equal to x but |x-y| is minimum. 

public class ClosestIntegerWeight {
    public static void main(String[] args) {
        System.out.println("closestWeight(92) = " + closestWeight(92));
        System.out.println("closestWeight(5) = " + closestWeight(5));
        System.out.println("closestWeight(6) = " + closestWeight(6));

    }

    private static int closestWeight(int n) {
        int n_weight = weight(n);
        int x = 1;
        while(true) {            
            System.out.println("\n----------------------------------------");
            System.out.println("n = " + n);
            System.out.println("n_weight = " + n_weight);
            System.out.println("x = " + x);

            // check x below n
            System.out.println("(n - x) = " + (n - x));
            if((n - x) > 0) {
                System.out.println("weight(n - x) = " + weight(n - x));
                if(weight(n - x) == n_weight) {
                    return (n - x);
                }
            }

            // check x above n
            System.out.println("(n + x) = " + (n + x));
            // if((Integer.MAX_VALUE - n) > x) {
            if((Integer.MAX_VALUE - x) > n) {
                System.out.println("weight(n + x) = " + weight(n + x));
                if(weight(n + x) == n_weight) {
                    return (n + x);
                }
            }
            x++;
        }
    }

    private static int weight(int x) {
        int weight = 0;
        int rem = 0;

        while(x > 0) {
            rem = x % 2;
            x = x / 2;
            weight += rem;
        }

        return weight;
    }
}

@@ -579,23 +579,1230 @@
 ht(6));%0A
-%0A    %7D%0A
+        // System.out.println(%22closestWeight(1073741824) = %22 + closestWeight(1073741824));%0A        System.out.println(%22closestIntSameWeight(1073741824L) = %22 + closestIntSameWeight(1073741824L));%0A    %7D%0A%0A    // if bits ant indexes k1 and k2 are flipped (where k! %3E k2), the the absolute value between the %0A    // original integer and the result of the swaps would be (2%5Ek1 - 2%5Ek2). To minimize this,%0A    // k1 must be made as small as possible and k2 as close as possible to k1. %0A    // the most efficient technique is to swap the two rightmost consecutive bits that differ. %0A    public static long closestIntSameWeight(long x) %7B%0A        // x is given to be nonnegative with leading bit 0%0A        // therefore only 63 LSB's to consider%0A        final int NUM_UNSIGNED_BITS = 63;%0A%0A        for(int i = 0; i %3C NUM_UNSIGNED_BITS; ++i) %7B%0A            if((((x %3E%3E%3E i) & 1) != ((x %3E%3E%3E (i + 1)) & 1))) %7B%0A                x %5E= (1L %3C%3C i) %7C (1L %3C%3C (i + 1));   // swaps bit-i and bit-(i + 1)%0A                return x;%0A            %7D%0A        %7D%0A%0A        // error if all bits of x are 0 or 1%0A        throw new IllegalArgumentException(%22All bits are 0 or 1.%22);%0A    %7D%0A%0A    // works horribly on certain inputs (e.g. large full powers of 2)
 %0A    pri

Add bitwise implementation