Created
January 11, 2016 06:47
-
-
Save lobster1234/d3d10d7f12f7e25da582 to your computer and use it in GitHub Desktop.
Quick and dirty to convert decimal to binary
This file contains hidden or 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
public static String binary(int input){ | |
StringBuilder builder = new StringBuilder(); | |
int numerator = input; | |
while(true){ | |
int divisor = numerator/2; | |
int remainder = numerator%2; | |
builder.append(remainder+""); | |
numerator = divisor; | |
if(divisor==1){ | |
builder.append("1"); | |
break; | |
}else if(divisor==0){ | |
builder.append("0"); | |
break; | |
} | |
} | |
return builder.reverse().toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment