Skip to content

Instantly share code, notes, and snippets.

@ryoasai
Created August 29, 2011 16:34
Show Gist options
  • Save ryoasai/1178781 to your computer and use it in GitHub Desktop.
Save ryoasai/1178781 to your computer and use it in GitHub Desktop.
Print a numerical value in bit pattern.
public class BinaryStringPrinter {
public static void print(byte value) {
System.out.println(zeroPadding(8, Integer.toBinaryString(value)));
}
public static void print(short value) {
System.out.println(zeroPadding(16, Integer.toBinaryString(value)));
}
public static void print(char value) {
System.out.println(zeroPadding(16, Integer.toBinaryString(value)));
}
public static void print(int value) {
System.out.println(zeroPadding(32, Integer.toBinaryString(value)));
}
public static void print(long value) {
System.out.println(zeroPadding(64, Long.toBinaryString(value)));
}
public static void print(float value) {
print(Float.floatToIntBits(value));
}
public static void print(double value) {
print(Double.doubleToLongBits(value));
}
private static String zeroPadding(int numDigits, String binaryString) {
return String
.format("%" + numDigits + "s", binaryString)
.replace(' ','0');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment