Java Bit Set setBit(byte value, int bit, boolean state)

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

Description

Sets a single bit in a byte value

License

Open Source License

Parameter

Parameter Description
value to set a bit of
bit to set
state to enable or disable the bit

Return

the resulting value

Declaration

public static byte setBit(byte value, int bit, boolean state) 

Method Source Code

//package com.java2s;
/*/*  ww w  .j  ava  2  s  .c o  m*/
 * This file is part of SpoutAPI.
 *
 * Copyright (c) 2011-2012, Spout LLC <http://www.spout.org/>
 * SpoutAPI is licensed under the Spout License Version 1.
 *
 * SpoutAPI is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * In addition, 180 days after any changes are published, you can use the
 * software, incorporating those changes, under the terms of the MIT license,
 * as described in the Spout License Version 1.
 *
 * SpoutAPI is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License,
 * the MIT license and the Spout License Version 1 along with this program.
 * If not, see <http://www.gnu.org/licenses/> for the GNU Lesser General Public
 * License and see <http://spout.in/licensev1> for the full license, including
 * the MIT license.
 */

public class Main {
    /**
     * Sets a single bit in a byte value
     * @param value to set a bit of
     * @param bit to set
     * @param state to enable or disable the bit
     * @return the resulting value
     */
    public static byte setBit(byte value, int bit, boolean state) {
        return (byte) setBit((int) value, bit, state);
    }

    /**
     * Sets a single bit in a short value
     * @param value to set a bit of
     * @param bit to set
     * @param state to enable or disable the bit
     * @return the resulting value
     */
    public static short setBit(short value, int bit, boolean state) {
        return (short) setBit((int) value, bit, state);
    }

    /**
     * Sets a single bit in an integer value
     * @param value to set a bit of
     * @param bit to set
     * @param state to enable or disable the bit
     * @return the resulting value
     */
    public static int setBit(int value, int bit, boolean state) {
        return state ? value | bit : value & ~bit;
    }
}

Related

  1. setBit(byte input, int bit)
  2. setBit(byte original, int bitToSet)
  3. setBit(byte target, byte pos)
  4. setBit(byte v, int position, boolean value)
  5. setBit(byte value, int bit, boolean on)
  6. setBit(byte value, int bit, boolean state)
  7. setBit(byte[] arr, int bit)
  8. setBit(byte[] b, int index)
  9. setBit(byte[] ba, int bit_offset, boolean on)