Skip to content

Commit

Permalink
Fix:drewnoakes#640 Optimized formatHex method for efficiency and memo…
Browse files Browse the repository at this point in the history
…ry usage
  • Loading branch information
Salman-Sayyed committed Dec 24, 2023
1 parent db9b141 commit 51b7b86
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Source/com/drew/metadata/bmp/BmpHeaderDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ public static String formatHex(@Nullable Long value, int digits) {

@NotNull
public static String formatHex(long value, int digits) {
return String.format("0x%0"+ digits + "X", value);
StringBuilder sb = new StringBuilder();
sb.append("0x");
long maskedValue = value & ((1L << (digits * 4)) - 1);
int leadingZeros = Long.numberOfLeadingZeros(maskedValue) >> 2;
sb.append(Long.toHexString(maskedValue | (1L << (digits * 4))).substring(leadingZeros));
return sb.toString();
}

@Nullable
Expand Down

0 comments on commit 51b7b86

Please sign in to comment.