blob: 15f8a9bb24bd38675a572a3c219ec4b39e528b10 [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.projectfloodlight.openflow.types;
2
3import com.google.common.base.Preconditions;
4
5public 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
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
29}