Java Bit Set setBit(int data, int bit, int value)

Here you can find the source of setBit(int data, int bit, int value)

Description

Sets bit in a number

License

Open Source License

Parameter

Parameter Description
data The number
bit Bit position
value New bit value

Return

The number

Declaration

public static int setBit(int data, int bit, int value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/* w ww.j a v  a  2  s  .  c o m*/
     * Sets bit in a number
     *
     * @param data The number
     * @param bit Bit position
     * @param value New bit value
     * @return The number
     */
    public static int setBit(int data, int bit, int value) {
        if (value == 1) {
            data = data | (1 << bit);
        } else {
            data = data & ~(1 << bit);
        }

        return data;
    }
}

Related

  1. setBit(int b0, boolean value, int original)
  2. setBit(int bit)
  3. setBit(int bitmask, int bit, boolean enabled)
  4. setBit(int bits, int index)
  5. setBit(int changeValue, int position, int value)
  6. setBit(int f, int i)
  7. setBit(int flag, int i)
  8. setBit(int flags, int bit)
  9. setBit(int i, int bit, boolean enabled)