Skip to content

Instantly share code, notes, and snippets.

@ShXck
Last active November 10, 2017 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShXck/bd92612e6b7e2fd0ed9939a7660124fe to your computer and use it in GitHub Desktop.
Save ShXck/bd92612e6b7e2fd0ed9939a7660124fe to your computer and use it in GitHub Desktop.
// Conviertan el binario a string así:
int binary_int = 10100110;
std::string bin_str = std::to_string( binary_int );
// cuenten los 1 o 0 no se
int counter = 0;
for( int i = 0; i < bin_str.lenght(); i++ ) {
int _integer = std::stoi( bin_str[0] );
if( _integer == 1 ) { // o 0
counter++;
}
// una vez contada agregen el bit ese al final
bin_str += "1" // o 0.
// y luego lo convierten a integer otra vez
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment