Created
August 29, 2011 16:34
-
-
Save ryoasai/1178781 to your computer and use it in GitHub Desktop.
Print a numerical value in bit pattern.
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
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