public class LeetCode {
private final byte chkSum;
private final short lenId;
public LeetCode(short value) {
this.lenId = (short) (value & 0x0FFF);
this.chkSum = (byte) ((~((( (value >>> 8) & 0x0F) + ((value >>> 4) & 0x0F) + (value & 0x0F)) % 16) + 1) & 0x0F);
}
public byte getChkSum(){
return chkSum;
}
public short getLenId() {
return lenId;
}
public short getValue() {
return (short) (chkSum << 12 | lenId);
}
private String toBinaryString() {
return String.format("%04d %04d %04d %04d",
Integer.parseInt(Integer.toBinaryString(getChkSum())),
Integer.parseInt(Integer.toBinaryString(getLenId()>>>8)),
Integer.parseInt(Integer.toBinaryString((getLenId() >>> 4) & 0x0F)),
Integer.parseInt(Integer.toBinaryString(getLenId() & 0x0F)));
}
public String toHexString() {
return Integer.toHexString(getValue() & 0xFFFF);
}
@Override
public String toString() {
return toHexString().toUpperCase() + "H";
}
public static void main(String[] args) {
LeetCode leetCode = new LeetCode(Short.parseShort("000000010010", 2));
System.out.println(Integer.toBinaryString(leetCode.getChkSum()));
System.out.println(Integer.toBinaryString(leetCode.getLenId()));
System.out.println(leetCode.toBinaryString());
System.out.println(leetCode);
}
}