Created
April 14, 2021 20:57
-
-
Save ppazos/5a73e1bf3d4c448a78609c3c7cbcf12b to your computer and use it in GitHub Desktop.
displays binary string from int
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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