Here you can find the source of setBitAt(byte flags, int pos, boolean value)
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. |
public static byte setBitAt(byte flags, int pos, boolean value)
//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; } }