Java Bit Set setBitBiInt(int b0, int b1, int value, int original)

Here you can find the source of setBitBiInt(int b0, int b1, int value, int original)

Description

Sets an int into another int.

License

Creative Commons License

Parameter

Parameter Description
b0 First bit.
b1 Second bit.
value 0-3 int.
original Original int to modify.

Return

Modified int.

Declaration

public static int setBitBiInt(int b0, int b1, int value, int original) 

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

public class Main {
    /**//from   w  ww  . java  2 s .c o m
     * Sets an int into another int.
     * @param b0 First bit.
     * @param b1 Second bit.
     * @param value 0-3 int.
     * @param original Original int to modify.
     * @return Modified int.
     */
    public static int setBitBiInt(int b0, int b1, int value, int original) {
        original = setBit(b0, getBit(0, value), original);
        original = setBit(b1, getBit(1, value), original);
        return original;
    }

    /**
     * Sets n-th bit for the given integer
     * @param b0 Which bit, LSB is 0
     * @param value What to set it to
     * @param original The integer to set the bit for
     * @return Int with the n-th bit set
     */
    public static int setBit(int b0, boolean value, int original) {
        if (getBit(b0, original) != value) {
            int mask = (1 << b0);
            return original ^ mask;
        }
        return original;
    }

    /**
     * Returns n-th bit from an integer
     * @param b0 which bit
     * @param number The integer in question
     * @return Returns the bit
     */
    public static boolean getBit(int b0, int number) {
        return ((number >> b0) & 1) == 1;
    }
}

Related

  1. setBit(short shortIn, int bitPos)
  2. setBitAt(byte flags, int pos, boolean value)
  3. setBitAt(int bitIndex, boolean value, byte b)
  4. setBitAt(int offset, boolean bitValue, byte aByte)
  5. setBitAt(int offset, boolean bitValue, byte aByte)
  6. setBitByPos(byte byt, boolean bool, int pos)
  7. setBitInInt(int bits, int bitIndex, boolean flag)
  8. setBitInLong(long l, long n, int v)
  9. setBitLE(byte[] data, int index)