Andreas Wundsam | 19dfc1e | 2014-04-24 19:00:27 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import com.google.common.base.Preconditions; |
| 4 | |
| 5 | public class HashValueUtils { |
| 6 | private HashValueUtils() { } |
| 7 | |
| 8 | public static long combineWithValue(long key, long value, int keyBits) { |
| 9 | Preconditions.checkArgument(keyBits >= 0 && keyBits <= 64, "keyBits must be [0,64]"); |
| 10 | |
| 11 | int valueBits = 64 - keyBits; |
| 12 | long valueMask = valueBits == 64 ? 0xFFFFFFFFFFFFFFFFL : (1L << valueBits) - 1; |
| 13 | |
| 14 | return key ^ (value & valueMask); |
| 15 | } |
| 16 | |
Andreas Wundsam | c5e9870 | 2014-05-05 13:12:19 -0700 | [diff] [blame] | 17 | public static int prefixBits(long raw1, int numBits) { |
| 18 | Preconditions.checkArgument(numBits >= 0 && numBits <= 32, |
| 19 | "numBits must be in range [0, 32]"); |
| 20 | |
| 21 | if(numBits == 0) |
| 22 | return 0; |
| 23 | |
| 24 | final int shiftDown = 64 - numBits; |
| 25 | |
| 26 | return (int) (raw1 >>> shiftDown); |
| 27 | } |
| 28 | |
Andreas Wundsam | 19dfc1e | 2014-04-24 19:00:27 -0700 | [diff] [blame] | 29 | } |