Java Array Truncate truncateI(long[] v, int len)

Here you can find the source of truncateI(long[] v, int len)

Description

Truncate a bit string to the given length (setting any higher bit to 0).

License

Open Source License

Parameter

Parameter Description
v String to process
len Length (in bits) to truncate to

Declaration

public static long[] truncateI(long[] v, int len) 

Method Source Code


//package com.java2s;
/*//  www. jav a  2 s.  c o m
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures
    
 Copyright (C) 2015
 Ludwig-Maximilians-Universit?t M?nchen
 Lehr- und Forschungseinheit f?r Datenbanksysteme
 ELKI Development Team
    
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.
    
 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Arrays;

public class Main {
    /**
     * Shift factor for a long: 2^6 == 64 == Long.SIZE
     */
    private static final int LONG_LOG2_SIZE = 6;
    /** Masking for long shifts. */
    private static final int LONG_LOG2_MASK = 0x3f;
    /** Long with all bits set */
    private static final long LONG_ALL_BITS = -1L;

    /**
     * Truncate a bit string to the given length (setting any higher bit to 0).
     * 
     * @param v String to process
     * @param len Length (in bits) to truncate to
     */
    public static long[] truncateI(long[] v, int len) {
        final int zap = (v.length * Long.SIZE) - len;
        final int zapWords = (zap >>> LONG_LOG2_SIZE);
        final int zapbits = zap & LONG_LOG2_MASK;
        Arrays.fill(v, v.length - zapWords, v.length, 0);
        if (zapbits > 0) {
            v[v.length - zapWords - 1] &= (LONG_ALL_BITS >>> zapbits);
        }
        return v;
    }
}

Related

  1. truncateAndConvertToInt(long[] longArray)
  2. truncateBytes(byte[] inputBytes)
  3. truncateByteSegment(byte[] byteSegment, int toSize)
  4. truncateData(byte[] data, int numOfBytes)
  5. truncateHashToLong(byte[] bytes)
  6. truncateLeft(byte[] b1, int bytes)