Cody

# Problem 108. Given an unsigned integer x, find the largest y by rearranging the bits in x

Solution 466182

Submitted on 4 Jul 2014 by Nick
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
%% x = 76; y_correct = 112; assert(isequal(maxit(x),y_correct))

b = 76 i = 1 AB = 0 b = 38 i = 2 AB = 0 0 b = 19 i = 3 AB = 0 0 1 b = 9 i = 4 AB = 0 0 1 1 b = 4 i = 5 AB = 0 0 1 1 0 b = 2 i = 6 AB = 0 0 1 1 0 0 b = 1 i = 7 AB = 0 0 1 1 0 0 1 b = 0 i = 8 a_binary = 1 0 0 1 1 0 0 re_ar = 1 1 1 0 0 0 0

2   Pass
%% x = 555; y_correct = 992; assert(isequal(maxit(x),y_correct))

b = 555 i = 1 AB = 1 b = 277 i = 2 AB = 1 1 b = 138 i = 3 AB = 1 1 0 b = 69 i = 4 AB = 1 1 0 1 b = 34 i = 5 AB = 1 1 0 1 0 b = 17 i = 6 AB = 1 1 0 1 0 1 b = 8 i = 7 AB = 1 1 0 1 0 1 0 b = 4 i = 8 AB = 1 1 0 1 0 1 0 0 b = 2 i = 9 AB = 1 1 0 1 0 1 0 0 0 b = 1 i = 10 AB = 1 1 0 1 0 1 0 0 0 1 b = 0 i = 11 a_binary = 1 0 0 0 1 0 1 0 1 1 re_ar = 1 1 1 1 1 0 0 0 0 0

3   Pass
%% x = 1000; y_correct = 1008; assert(isequal(maxit(x),y_correct))

b = 1000 i = 1 AB = 0 b = 500 i = 2 AB = 0 0 b = 250 i = 3 AB = 0 0 0 b = 125 i = 4 AB = 0 0 0 1 b = 62 i = 5 AB = 0 0 0 1 0 b = 31 i = 6 AB = 0 0 0 1 0 1 b = 15 i = 7 AB = 0 0 0 1 0 1 1 b = 7 i = 8 AB = 0 0 0 1 0 1 1 1 b = 3 i = 9 AB = 0 0 0 1 0 1 1 1 1 b = 1 i = 10 AB = 0 0 0 1 0 1 1 1 1 1 b = 0 i = 11 a_binary = 1 1 1 1 1 0 1 0 0 0 re_ar = 1 1 1 1 1 1 0 0 0 0

4   Pass
%% x = 10000000; y_correct = 16711680; assert(isequal(maxit(x),y_correct))

b = 10000000 i = 1 AB = 0 b = 5000000 i = 2 AB = 0 0 b = 2500000 i = 3 AB = 0 0 0 b = 1250000 i = 4 AB = 0 0 0 0 b = 625000 i = 5 AB = 0 0 0 0 0 b = 312500 i = 6 AB = 0 0 0 0 0 0 b = 156250 i = 7 AB = 0 0 0 0 0 0 0 b = 78125 i = 8 AB = 0 0 0 0 0 0 0 1 b = 39062 i = 9 AB = 0 0 0 0 0 0 0 1 0 b = 19531 i = 10 AB = 0 0 0 0 0 0 0 1 0 1 b = 9765 i = 11 AB = 0 0 0 0 0 0 0 1 0 1 1 b = 4882 i = 12 AB = 0 0 0 0 0 0 0 1 0 1 1 0 b = 2441 i = 13 AB = 0 0 0 0 0 0 0 1 0 1 1 0 1 b = 1220 i = 14 AB = 0 0 0 0 0 0 0 1 0 1 1 0 1 0 b = 610 i = 15 AB = 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 b = 305 i = 16 AB = 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 1 b = 152 i = 17 AB = Columns 1 through 16 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 1 Column 17 0 b = 76 i = 18 AB = Columns 1 through 16 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 1 Columns 17 through 18 0 0 b = 38 i = 19 AB = Columns 1 through 16 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 1 Columns 17 through 19 0 0 0 b = 19 i = 20 AB = Columns 1 through 16 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 1 Columns 17 through 20 0 0 0 1 b = 9 i = 21 AB = Columns 1 through 16 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 1 Columns 17 through 21 0 0 0 1 1 b = 4 i = 22 AB = Columns 1 through 16 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 1 Columns 17 through 22 0 0 0 1 1 0 b = 2 i = 23 AB = Columns 1 through 16 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 1 Columns 17 through 23 0 0 0 1 1 0 0 b = 1 i = 24 AB = Columns 1 through 16 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 1 Columns 17 through 24 0 0 0 1 1 0 0 1 b = 0 i = 25 a_binary = Columns 1 through 16 1 0 0 1 1 0 0 0 1 0 0 1 0 1 1 0 Columns 17 through 24 1 0 0 0 0 0 0 0 re_ar = Columns 1 through 16 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 Columns 17 through 24 0 0 0 0 0 0 0 0

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!