public static int search(int x, int set_value, int default_value) {
        int index = 0, num = 0;
        while (((x >> index) & 1) != set_value) {
            ++index;
        }
        while (((x >> index) & 1) == set_value) {
            ++index;
            ++num;
        }
        if (index == 32) {
            return default_value;
        } else {
            x ^= (1 << index);
            --index;
            --num;
            for (int i = index; i >= num; --i) {
                x = set_bit(x, i, set_value != 0);
            }
            for (int i = num - 1; i >= 0; --i) {
                x = set_bit(x, i, set_value == 0);
            }
            return x;
        }
    }