Java Bit Set setBitAt(byte flags, int pos, boolean value)

Here you can find the source of setBitAt(byte flags, int pos, boolean value)

Description

Modifies the value of the bit specified by its position in the given byte.
Note that the return value has to be used as the result of the operation, since the method operates on a copy of the input byte.

License

Open Source License

Parameter

Parameter Description
flags The byte to modify
pos The position of the bit within the passed byte. Can range between 0 and 7, other values are permitted but the return value is always 0.
value A boolean that indicates the value to set at the specified position. true means 1, false means 0.

Return

The resulting byte

Declaration

public static byte setBitAt(byte flags, int pos, boolean value) 

Method Source Code

//package com.java2s;
// modify it under  the terms of  the GNU General Public License

public class Main {
    /**// w w  w. j  ava2  s .c om
     * Modifies the value of the bit specified by
     * its position in the given byte.<br/>
     * Note that the return value has to be used
     * as the result of the operation, since the
     * method operates on a copy of the input byte.
     * This copy will be returned mofified.<br/>
     * A typical usage is the following:<br/>
     * <code>
     *    flags = BitUtility.setBitAt(flags, 5, true);
     * </code>
     * 
     * @param flags
     *          The byte to modify
     * @param pos
     *          The position of the bit within the
     *          passed byte. Can range between 0 and 7,
     *          other values are permitted but the return
     *          value is always 0.
     * @param value
     *          A boolean that indicates the value to
     *          set at the specified position. true means
     *          1, false means 0.
     * @return  The resulting byte
     */
    public static byte setBitAt(byte flags, int pos, boolean value) {
        if (value) {
            switch (pos) {
            case 0:
                flags |= (byte) 0x01;
                break;
            case 1:
                flags |= (byte) 0x02;
                break;
            case 2:
                flags |= (byte) 0x04;
                break;
            case 3:
                flags |= (byte) 0x08;
                break;
            case 4:
                flags |= (byte) 0x10;
                break;
            case 5:
                flags |= (byte) 0x20;
                break;
            case 6:
                flags |= (byte) 0x40;
                break;
            case 7:
                flags |= (byte) 0x80;
                break;
            }
        } else {
            switch (pos) {
            case 0:
                flags &= 0xFE;
                break;
            case 1:
                flags &= 0xFD;
                break;
            case 2:
                flags &= 0xFB;
                break;
            case 3:
                flags &= 0xF7;
                break;
            case 4:
                flags &= 0xEF;
                break;
            case 5:
                flags &= 0xDF;
                break;
            case 6:
                flags &= 0xBF;
                break;
            case 7:
                flags &= 0x7F;
                break;
            }
        }
        return flags;
    }
}

Related

  1. setBit(int value, int flags)
  2. setBit(int value, int index)
  3. setBit(int[] x, int i, int v)
  4. setBit(long[] data, int position, int bitWidth)
  5. setBit(short shortIn, int bitPos)
  6. setBitAt(int bitIndex, boolean value, byte b)
  7. setBitAt(int offset, boolean bitValue, byte aByte)
  8. setBitAt(int offset, boolean bitValue, byte aByte)
  9. setBitBiInt(int b0, int b1, int value, int original)