Since we want the smallest next value , shift all 1 ' s ▁ at ▁ the ▁ end ▁ in ▁ the ▁ binary ▁ ▁ substring ▁ starting ▁ from ▁ index ▁ ' i + 2 '
int j = i + 2 , k = l - 1 ; while ( j < k ) { if ( bnum . at ( j ) == '1' && bnum . at ( k ) == '0' ) { char ch = bnum . at ( j ) ; bnum . at ( j ) = bnum . at ( k ) ; bnum . at ( k ) = ch ; j ++ ; k -- ; }