Skip to content

Instantly share code, notes, and snippets.

@ppazos
Created April 14, 2021 20:57
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 ppazos/5a73e1bf3d4c448a78609c3c7cbcf12b to your computer and use it in GitHub Desktop.
Save ppazos/5a73e1bf3d4c448a78609c3c7cbcf12b to your computer and use it in GitHub Desktop.
displays binary string from int
void toBinary(int decimal)
{
int[] binary = new int[40]
int index = 0
String bin = ""
while(decimal > 0)
{
binary[index++] = decimal%2
decimal = decimal/2
}
for(int i = index-1; i >= 0; i--)
{
bin += binary[i]
}
print bin.padLeft(4, '0')
println ""
}
(0..15).each {
toBinary(it)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment