Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active March 11, 2019 16:48
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 parzibyte/911c4c7f416273c52dfd3ddbdb4d44b1 to your computer and use it in GitHub Desktop.
Save parzibyte/911c4c7f416273c52dfd3ddbdb4d44b1 to your computer and use it in GitHub Desktop.
public static String convertirDecimalABinarioManual(long decimal) {
if (decimal <= 0) {
return "0";
}
StringBuilder binario = new StringBuilder();
while (decimal > 0) {
short residuo = (short) (decimal % 2);
decimal = decimal / 2;
// Insertar el dígito al inicio de la cadena
binario.insert(0, String.valueOf(residuo));
}
return binario.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment