Java Bit Set setBit(int b0, boolean value, int original)

Here you can find the source of setBit(int b0, boolean value, int original)

Description

Sets n-th bit for the given integer

License

Creative Commons License

Parameter

Parameter Description
b0 Which bit, LSB is 0
value What to set it to
original The integer to set the bit for

Return

Int with the n-th bit set

Declaration

public static int setBit(int b0, boolean value, int original) 

Method Source Code

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

public class Main {
    /**//from ww w .  j  a  v  a  2  s  .com
     * 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(final byte pData, final int pBitIndex, final boolean pOn)
  2. setBit(final byte[] buf, final long bitIndex, final boolean value)
  3. setbit(final int num, final int bitnum, final boolean state)
  4. setBit(final int source, final int bit, final boolean value)
  5. setBit(final long word, final int idx, final boolean bit)
  6. setBit(int bit)
  7. setBit(int bitmask, int bit, boolean enabled)
  8. setBit(int bits, int index)
  9. setBit(int changeValue, int position, int value)