public int size() { int size = 0; for ( long s : data ) { size += Long.bitCount( s ); } return size; }
/** Reduces every counter by half of its original value. */ void reset() { int count = 0; for (int i = 0; i < table.length; i++) { count += Long.bitCount(table[i] & ONE_MASK); table[i] = (table[i] >>> 1) & RESET_MASK; } size = (size >>> 1) - (count >>> 2); }
public static long parity1Set(long i) { if(CC.PARANOID && (i&1)!=0) throw new DBException.PointerChecksumBroken(); return i | ((Long.bitCount(i)+1)%2); }
public static int rank(long index, int bitNum) { return Long.bitCount(index & ~(-1L << bitNum)); }
private static int countContinuationBytes(long i64) { // Count the number of bytes that match 0b10xx_xxxx as follows: // 1. Mask off the 8th bit of every byte and shift it into the 7th position. // 2. Then invert the bytes, which turns the 0 in the 7th bit to a one. // 3. And together the restults of step 1 and 2, giving us a one in the 7th // position if the byte matched. // 4. Count the number of bits in the result, which is the number of bytes // that matched. i64 = ((i64 & TOP_MASK64) >>> 1) & (~i64); return Long.bitCount(i64); } }
public int getNumSetBits() { int setCount = 0; for (long datum : bitSet.getData()) { setCount += Long.bitCount(datum); } return setCount; }
/** * ByteBuffer based copy of logic of {@link BloomKFilter#getNumSetBits()} * @param bfBuffer * @param start * @return */ public static int getNumSetBits(ByteBuffer bfBuffer, int start) { ByteBuffer view = bfBuffer.duplicate().order(ByteOrder.BIG_ENDIAN); view.position(start); int numLongs = view.getInt(1 + start); int setBits = 0; for (int i = 0, pos = START_OF_SERIALIZED_LONGS + start; i < numLongs; i++, pos += Long.BYTES) { setBits += Long.bitCount(view.getLong(pos)); } return setBits; }
public static long[] getLabels( long bits ) { long[] result = new long[Long.bitCount( bits )]; for ( int labelId = 0, c = 0; labelId < LABEL_COUNT; labelId++ ) { int mask = 1 << labelId; if ( (bits & mask) != 0 ) { result[c++] = labelId; } } return result; } }
public static long parity1Get(long i) { if(Long.bitCount(i)%2!=1){ throw new DBException.PointerChecksumBroken(); } return i&0xFFFFFFFFFFFFFFFEL; }
public static long parity3Get(long i) { long ret = i&0xFFFFFFFFFFFFFFF8L; if((Long.bitCount(ret)+1)%8!=(i&0x7)){ throw new DBException.PointerChecksumBroken(); } return ret; }
public static long parity4Set(long i) { if(CC.PARANOID && (i&0xF)!=0) throw new DBException.PointerChecksumBroken(); return i | ((Long.bitCount(i)+1)%16); }
public static long parity3Set(long i) { if(CC.PARANOID && (i&0x7)!=0) throw new DBException.PointerChecksumBroken(); return i | ((Long.bitCount(i)+1)%8); }
public static long parity4Get(long i) { long ret = i&0xFFFFFFFFFFFFFFF0L; if((Long.bitCount(ret)+1)%16!=(i&0xF)){ throw new DBException.PointerChecksumBroken(); } return ret; }
LockFreeBitArray(long[] data) { checkArgument(data.length > 0, "data length is zero!"); this.data = new AtomicLongArray(data); this.bitCount = LongAddables.create(); long bitCount = 0; for (long value : data) { bitCount += Long.bitCount(value); } this.bitCount.add(bitCount); }
LockFreeBitArray(long[] data) { checkArgument(data.length > 0, "data length is zero!"); this.data = new AtomicLongArray(data); this.bitCount = LongAddables.create(); long bitCount = 0; for (long value : data) { bitCount += Long.bitCount(value); } this.bitCount.add(bitCount); }
public static ByteBuffer allocateAlignedByteBuffer(int capacity, int align) { Preconditions.checkArgument(Long.bitCount(align) == 1, "Alignment must be a power of 2"); final ByteBuffer buf = ByteBuffer.allocateDirect(capacity + align); long address = getAddress(buf); if ((address & (align - 1)) == 0) { buf.limit(capacity); } else { int offset = (int) (align - (address & (align - 1))); buf.position(offset); buf.limit(offset + capacity); } return buf.slice(); } }
LockFreeBitArray(long[] data) { checkArgument(data.length > 0, "data length is zero!"); this.data = new AtomicLongArray(data); this.bitCount = LongAddables.create(); long bitCount = 0; for (long value : data) { bitCount += Long.bitCount(value); } this.bitCount.add(bitCount); }
@Description("count number of set bits in 2's complement representation") @ScalarFunction @SqlType(StandardTypes.BIGINT) public static long bitCount(@SqlType(StandardTypes.BIGINT) long num, @SqlType(StandardTypes.BIGINT) long bits) { if (bits == 64) { return Long.bitCount(num); } if (bits <= 1 || bits > 64) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Bits specified in bit_count must be between 2 and 64, got " + bits); } long lowBitsMask = (1L << (bits - 1)) - 1; // set the least (bits - 1) bits if (num > lowBitsMask || num < ~lowBitsMask) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Number must be representable with the bits specified. " + num + " can not be represented with " + bits + " bits"); } long mask = (1L << bits) - 1; return Long.bitCount(num & mask); }
@Test public void testNextPowTwo2Long(){ for(long i=1;i<10000L*Integer.MAX_VALUE;i+= 1 + i/100000){ long pow = nextPowTwo(i); assertTrue(pow>=i); assertTrue(pow/2<i); assertTrue(Long.bitCount(pow)==1); } }