Java Bit Set setBit(int value, int bit)

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

Description

Set the bit within the int.

License

Apache License

Parameter

Parameter Description
value The value in which to set the bit.
bit The bit to set.

Return

The new value with the bit set.

Declaration

public static int setBit(int value, int bit) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*  w ww.  j a v a 2  s .c  o m*/
     * Set the bit within the int.
     * @param value The value in which to set the bit.
     * @param bit The bit to set.
     * @return The new value with the bit set.
     */
    public static int setBit(int value, int bit) {
        assert bit >= 0 && bit < 16;
        return value | bitMask(bit);
    }

    /**
     * Set the bit within the byte.
     * @param value The value in which to set the bit.
     * @param bit The bit to set.
     * @return The new value with the bit set.
     */
    public static byte setBit(byte value, int bit) {
        assert bit >= 0 && bit < 16;
        return (byte) (value | bitMask(bit));
    }

    /**
     * Calculate a bitmask of the bit.
     * @param bit The bit to be used to calculate the bitmask.
     * @return The value of the bitmask.
     */
    public static int bitMask(int bit) {
        assert bit >= 0 && bit < 16;
        return 1 << bit;
    }
}

Related

  1. setBit(int integer, int bit)
  2. setBit(int mask, int bit, boolean value)
  3. setBit(int n, int bitPosition)
  4. setBit(int position, boolean value, int meta)
  5. setBit(int position, byte[] array)
  6. setBit(int value, int bitIndex)
  7. setBit(int value, int bitmask, boolean set)
  8. setBit(int value, int flags)
  9. setBit(int value, int index)