Skip to content

Instantly share code, notes, and snippets.

@FDelporte
Last active September 26, 2024 07:29
Show Gist options
  • Save FDelporte/ab37b64d379560ae3f8db0403f5a8ed1 to your computer and use it in GitHub Desktop.
Save FDelporte/ab37b64d379560ae3f8db0403f5a8ed1 to your computer and use it in GitHub Desktop.
Code of the blog "Deep dive into bits, bytes, shorts, ints, longs, signed, and unsigned with Java"
// Read the full explanation of this code on this blog post:
// https://webtechie.be/post/2024-09-26-java-bits-bytes-short-int-long-signed-unsigned/
public class SignedUnsigned {
public static void main(String[] args) {
System.out.println("0 to 15 as bits:");
System.out.println("Value\tBits\tHex");
for (int i = 0; i <= 15; i++) {
System.out.println(i
+ "\t" + String.format("%4s", Integer.toBinaryString(i)).replace(' ', '0')
+ "\t0x" + Integer.toHexString(i).toUpperCase());
}
System.out.println(" ");
System.out.println("------------------------------------");
System.out.println(" ");
System.out.println("0 to 255 as bits:");
System.out.println("Value\tBits\tHex");
for (int i = 0; i <= 255; i++) {
System.out.println(i
+ "\t" + String.format("%8s", Integer.toBinaryString(i)).replace(' ', '0')
+ "\t0x" + Integer.toHexString(i).toUpperCase());
}
System.out.println(" ");
System.out.println("------------------------------------");
System.out.println(" ");
System.out.println("Object min and max values");
System.out.println(" ");
System.out.println("Byte");
System.out.println(" Min: " + Byte.MIN_VALUE
+ "\n\t= " + String.format("%8s", Integer.toBinaryString(Byte.MIN_VALUE & 0xFF)).replace(' ', '0'));
System.out.println(" Max: " + Byte.MAX_VALUE
+ "\n\t= " + String.format("%8s", Integer.toBinaryString(Byte.MAX_VALUE & 0xFF)).replace(' ', '0'));
System.out.println("Short");
System.out.println(" Min: " + Short.MIN_VALUE
+ "\n\t= " + String.format("%16s", Integer.toBinaryString(Short.MIN_VALUE & 0xFFFF)).replace(' ', '0'));
System.out.println(" Max: " + Short.MAX_VALUE
+ "\n\t= " + String.format("%16s", Integer.toBinaryString(Short.MAX_VALUE & 0xFFFF)).replace(' ', '0'));
System.out.println("Integer");
System.out.println(" Min: " + Integer.MIN_VALUE
+ "\n\t= " + String.format("%32s", Integer.toBinaryString(Integer.MIN_VALUE)).replace(' ', '0'));
System.out.println(" Max: " + Integer.MAX_VALUE
+ "\n\t= " + String.format("%32s", Integer.toBinaryString(Integer.MAX_VALUE)).replace(' ', '0'));
System.out.println("Long");
System.out.println(" Min: " + Long.MIN_VALUE
+ "\n\t= " + String.format("%64s", Long.toBinaryString(Long.MIN_VALUE)).replace(' ', '0'));
System.out.println(" Max: " + Long.MAX_VALUE
+ "\n\t= " + String.format("%64s", Long.toBinaryString(Long.MAX_VALUE)).replace(' ', '0'));
System.out.println(" ");
System.out.println("------------------------------------");
System.out.println(" ");
System.out.println("Bits to byte");
System.out.println("Byte value 00000001: " + ((byte) Integer.parseInt("00000001", 2)));
System.out.println("Byte value 00001111: " + ((byte) Integer.parseInt("00001111", 2)));
System.out.println("Byte value 01111111: " + ((byte) Integer.parseInt("01111111", 2)));
System.out.println("Byte value 10000000: " + ((byte) Integer.parseInt("10000000", 2)));
System.out.println("Byte value 10000001: " + ((byte) Integer.parseInt("10000001", 2)));
System.out.println("Byte value 10001111: " + ((byte) Integer.parseInt("10001111", 2)));
System.out.println(" ");
System.out.println("------------------------------------");
System.out.println(" ");
System.out.println("Byte to Integer with mask");
var b = (byte) Integer.parseInt("10001111", 2);
System.out.println("Byte value: " + b);
var bWithMask = b & 0xff;
System.out.println("Byte value with mask: " + bWithMask);
System.out.println("Object Type: " + printObjectType(bWithMask));
System.out.println(" ");
System.out.println("------------------------------------");
System.out.println(" ");
System.out.println("Byte to Integer with toUnsignedInt");
var unsignedInt = Byte.toUnsignedInt(b);
System.out.println("Byte to unsigned integer: " + unsignedInt);
System.out.println("Object Type: " + printObjectType(unsignedInt));
System.out.println(" ");
System.out.println("------------------------------------");
System.out.println(" ");
System.out.println("Byte to Long with toUnsignedLong");
var unsignedLong = Byte.toUnsignedLong(b);
System.out.println("Byte to unsigned long: " + unsignedLong);
System.out.println("Object Type: " + printObjectType(unsignedLong));
System.out.println(" ");
System.out.println("------------------------------------");
System.out.println(" ");
System.out.println("Example with short");
short s1 = (short) Integer.parseInt("1000000000000000", 2);
System.out.println("Short value 1000000000000000: " + s1);
System.out.println("Short value 1000000000000000 with mask: " + (s1 & 0xFFFF));
System.out.println("Using Short.toUnsignedLong: " + Short.toUnsignedInt(s1));
short s2 = (short) Integer.parseInt("1111111111111111", 2);
System.out.println("Short value 1111111111111111: " + s2);
System.out.println("Short value 1111111111111111 to unsigned: " + Short.toUnsignedInt(s2));
}
private static String printObjectType(Object obj) {
if (obj != null) {
return obj.getClass().getName();
} else {
return "NULL";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment