Skip to content

Instantly share code, notes, and snippets.

@ziginsider
Last active March 27, 2018 11:54
Show Gist options
  • Save ziginsider/3e6e1f21ae89ceda46e791f6491f8284 to your computer and use it in GitHub Desktop.
Save ziginsider/3e6e1f21ae89ceda46e791f6491f8284 to your computer and use it in GitHub Desktop.
package someproject;
/**
*
* @author Alex Kisel
*/
public class BinaryConverter {
public static void main(String[] args) {
for(int i = -5; i < 33; i++) {
System.out.println(i + ": " + toBinary(i));
System.out.println(i);
//always another way
System.out.println(i + ": " + Integer.toBinaryString(i));
}
}
/*
* pre: none
* post: returns a String with base10Num in base 2
*/
public static String toBinary(int base10Num) {
boolean isNeg = base10Num < 0;
base10Num = Math.abs(base10Num);
String result = "";
while(base10Num > 1) {
result = (base10Num % 2) + result;
base10Num /= 2;
}
assert base10Num == 0 || base10Num == 1 : "value is not <= 1: " + base10Num;
result = base10Num + result;
assert all0sAnd1s(result);
if(isNeg)
result = "-" + result;
return result;
}
/*
* pre: cal != null
* post: return true if val consists only of characters 1 and 0, false otherwise
*/
public static boolean all0sAnd1s(String val) {
assert val != null : "Failed precondition all0sAnd1s. parameter cannot be null";
boolean all = true;
int i = 0;
char c;
while(all && i < val.length()) {
c = val.charAt(i);
all = c == '0' || c == '1';
i++;
}
return all;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment