brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [1/2] brooklyn-server git commit: Simplify BitUtils methods
Date Mon, 09 Jan 2017 16:53:09 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master eee88bf93 -> 3641456b3


Simplify BitUtils methods


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/1f1fea15
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/1f1fea15
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/1f1fea15

Branch: refs/heads/master
Commit: 1f1fea152e7b73afaa86533c1664b9243a8585c9
Parents: 3b4f394
Author: Andrew Donald Kennedy <andrew.kennedy@cloudsoftcorp.com>
Authored: Thu Dec 15 17:19:53 2016 +0000
Committer: Andrew Donald Kennedy <andrew.kennedy@cloudsoftcorp.com>
Committed: Thu Dec 15 17:20:05 2016 +0000

----------------------------------------------------------------------
 .../org/apache/brooklyn/util/math/BitUtils.java | 28 +++++++-------------
 .../apache/brooklyn/util/math/BitUtilsTest.java |  3 +++
 2 files changed, 13 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/1f1fea15/utils/common/src/main/java/org/apache/brooklyn/util/math/BitUtils.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/math/BitUtils.java b/utils/common/src/main/java/org/apache/brooklyn/util/math/BitUtils.java
index 9585124..9e091fe 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/math/BitUtils.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/math/BitUtils.java
@@ -18,25 +18,22 @@
  */
 package org.apache.brooklyn.util.math;
 
+import com.google.common.primitives.Bytes;
+import com.google.common.primitives.Ints;
+
 public class BitUtils {
 
     /** reverses the bits in a byte, i.e.  128 = 0b1000000 = bit list {0,0,0,0,0,0,0,1},
      * reversed yields 1 = 0b00000001 = bit list {1,0,0,0,0,0,0,0} */
     public static byte reverseBitSignificance(byte b) {
-        int result = 0;
-        for (int i=0; i<8; i++) {
-            result <<= 1;
-            if ((b&1)==1) result++;
-            b >>= 1;
-        }
-        return (byte)result;
+        return (byte) (Integer.reverse(b) >> 24);
     }
-    
+
     /** as {@link #reverseBitSignificance(byte)} but accepting int for convenience */
     public static byte reverseBitSignificanceInByte(int b) {
         return reverseBitSignificance((byte)b);
     }
-    
+
     /** returns an array of bytes where the bits in each byte have been reversed;
      * note however the order of the arguments is not reversed;
      * useful e.g. in working with IP address CIDR's */
@@ -49,22 +46,17 @@ public class BitUtils {
 
     /** as {@link #reverseBitSignificance(byte...)}, but taking ints for convenience (ignoring
high bits) */
     public static byte[] reverseBitSignificanceInBytes(int ...bytes) {
-        byte[] result = new byte[bytes.length];
-        for (int i=0; i<bytes.length; i++)
-            result[i] = reverseBitSignificance((byte)bytes[i]);
-        return result;
+        return reverseBitSignificance(Bytes.toArray(Ints.asList(bytes)));
     }
-    
+
     /** why oh why are bytes signed! */
     public static int unsigned(byte b) {
-        if (b<0) return b+256;
-        return b;
+        return b & 0xff;
     }
 
     /** returns the value in 0..255 which is equivalent mod 256 */
     public static int unsignedByte(int b) {
-        if (b<0) return (b%256)+256;
-        return (b%256);
+        return b & 0xff;
     }
 
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/1f1fea15/utils/common/src/test/java/org/apache/brooklyn/util/math/BitUtilsTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/math/BitUtilsTest.java b/utils/common/src/test/java/org/apache/brooklyn/util/math/BitUtilsTest.java
index bc9d5cb..debb9a5 100644
--- a/utils/common/src/test/java/org/apache/brooklyn/util/math/BitUtilsTest.java
+++ b/utils/common/src/test/java/org/apache/brooklyn/util/math/BitUtilsTest.java
@@ -26,6 +26,8 @@ public class BitUtilsTest {
 
     @Test
     public void checkReverseBitSignificance() {
+        Assert.assertEquals(BitUtils.reverseBitSignificanceInByte(0b00000001), (byte)0b10000000);
+        Assert.assertEquals(BitUtils.reverseBitSignificanceInByte(0x0f), (byte)0xf0);
         Assert.assertEquals(BitUtils.reverseBitSignificanceInByte(1), (byte)128);
         Assert.assertEquals(BitUtils.reverseBitSignificanceInByte(2), 64);
         Assert.assertEquals(BitUtils.reverseBitSignificanceInByte(8), 16);
@@ -40,6 +42,7 @@ public class BitUtilsTest {
     
     @Test
     public void checkUnsigned() {
+        Assert.assertEquals(BitUtils.unsigned((byte)0xff), 0xff);
         Assert.assertEquals(BitUtils.unsigned((byte)-96), 160);
         Assert.assertEquals(BitUtils.unsigned((byte)160), 160);
         Assert.assertEquals(BitUtils.unsignedByte(-96), 160);


Mime
View raw message