Returns a value with the least significant bit set to the value - Java java.util

Java examples for java.util:BitSet

Description

Returns a value with the least significant bit set to the value

Demo Code


//package com.java2s;

public class Main {
    /**//from   w  w w  .j av  a2  s . com
     * Returns a value with the least significant bit set to the value
     * @param byteValue The int value to set
     * @param value The value to set the LSB (true : 1 false : 0)
     * @return new int with lsb changed
     */
    public static int setLeastSignificanBit(int byteValue, boolean value) {
        boolean leastIsOne = (byteValue % 2) == 1;
        /*
         * Covers cases:
         * If least sig is 1 and we want to set 0
         * If least sig is 0 and we want to set 1
         */
        if (leastIsOne ^ value)
            return byteValue ^= 1;

        // else leave value
        else
            return byteValue;
    }
}

Related Tutorials