Java Bit Set setBit(byte v, int position, boolean value)

Here you can find the source of setBit(byte v, int position, boolean value)

Description

Returns v, with the bit at position set to 1 or 0 depending on value.

License

Apache License

Declaration

public static byte setBit(byte v, int position, boolean value) 

Method Source Code

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

public class Main {
    /**/*ww  w .ja v a  2  s. c om*/
     * Returns v, with the bit at position set to 1 or 0 depending on value.
     */
    public static byte setBit(byte v, int position, boolean value) {
        return (byte) setBit((int) v, position, value);
    }

    public static short setBit(short v, int position, boolean value) {
        return (short) setBit((int) v, position, value);
    }

    public static int setBit(int v, int position, boolean value) {
        if (value)
            return v | (1 << position);
        else
            return clearBit(v, position);
    }

    public static long setBit(long v, int position, boolean value) {
        if (value)
            return v | (1L << position);
        else
            return clearBit(v, position);
    }

    /**
     * Returns v, with the bit at position set to zero.
     */
    public static byte clearBit(byte v, int position) {
        return (byte) clearBit((int) v, position);
    }

    public static short clearBit(short v, int position) {
        return (short) clearBit((int) v, position);
    }

    public static int clearBit(int v, int position) {
        return v & ~(1 << position);
    }

    public static long clearBit(long v, int position) {
        return v & ~(1L << position);
    }
}

Related

  1. setBit(byte data, int pos, int val)
  2. setBit(byte in, int position, boolean value)
  3. setBit(byte input, int bit)
  4. setBit(byte original, int bitToSet)
  5. setBit(byte target, byte pos)
  6. setBit(byte value, int bit, boolean on)
  7. setBit(byte value, int bit, boolean state)
  8. setBit(byte value, int bit, boolean state)
  9. setBit(byte[] arr, int bit)