Shifts bits of a BitSet object one place to the right. - Java java.util

Java examples for java.util:BitSet

Description

Shifts bits of a BitSet object one place to the right.

Demo Code


//package com.java2s;

import java.util.BitSet;

public class Main {
    /**/*from w  w w .j  a  v  a  2 s  . co m*/
     * Shifts bits of a BitSet object one place to the right.
     * Stores new value in the same BitSet.
     *
     * @param s Shifted BitSet object.
     * @param length Length of binary number.
     * @param logical true for logical right shift; false for arithmetic right shift.
     * @return BitSet s after shifting.
     */
    public static BitSet shiftRight(BitSet s, int length, boolean logical) {
        for (int i = 0; i < length - 1; i++) {
            s.set(i, s.get(i + 1));
        }

        if (logical)
            s.set(length - 1, false);

        return s;
    }
}

Related Tutorials