From 62d40fb2c3cf65138e73fab56887bc7ca30dc58d Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Sun, 12 Jun 2016 20:43:23 +0200 Subject: [PATCH] Improved unsigned byte comparison --- core/src/main/java/ch/dissem/bitmessage/utils/Bytes.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/ch/dissem/bitmessage/utils/Bytes.java b/core/src/main/java/ch/dissem/bitmessage/utils/Bytes.java index 8107eb0..986c288 100644 --- a/core/src/main/java/ch/dissem/bitmessage/utils/Bytes.java +++ b/core/src/main/java/ch/dissem/bitmessage/utils/Bytes.java @@ -23,6 +23,8 @@ package ch.dissem.bitmessage.utils; * situations. */ public class Bytes { + public static final byte BYTE_0x80 = (byte) 0x80; + public static void inc(byte[] nonce) { for (int i = nonce.length - 1; i >= 0; i--) { nonce[i]++; @@ -82,11 +84,7 @@ public class Bytes { } private static boolean lt(byte a, byte b) { - if (a < 0) return b < 0 && a < b; - if (b < 0) return a >= 0 || a < b; - return a < b; - // This would be easier to understand, but is (slightly) slower: - // return (a & 0xff) < (b & 0xff); + return (a ^ BYTE_0x80) < (b ^ BYTE_0x80); } /**