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

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

Description

Sets a bit in the byte

License

Open Source License

Parameter

Parameter Description
_byte Byte where the bit will be changed
position Position of bit in Byte, i.e. 1 to 8 to specified value
value Value to be set as (True=1,False=0)

Return

Byte with changed bit at specified position

Declaration

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

Method Source Code

//package com.java2s;
/*/*from   w ww  .  j ava  2 s  .  com*/
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * Sets a bit in the byte
     *
     * @param _byte    Byte where the bit will be changed
     * @param position Position of bit in Byte, i.e. 1 to 8 to specified value
     * @param value    Value to be set as (True=1,False=0)
     * @return Byte with changed bit at specified position
     */
    public static byte setBit(byte _byte, int position, boolean value) {
        position--;
        return (byte) (value ? _byte | 1 << position : _byte & ~(1 << position));
    }
}

Related

  1. SetBit(byte _bitset, byte bit)
  2. setBit(byte a, int pos, boolean set)
  3. setBit(byte b, int i)
  4. setBit(byte b, int pos)
  5. setBit(byte data, byte bit, boolean value)