Here you can find the source of setBit(final byte pData, final int pBitIndex, final boolean pOn)
Parameter | Description |
---|---|
pData | data to modify |
pBitIndex | index to set |
pOn | set bit at specified index to 1 or 0 |
public static byte setBit(final byte pData, final int pBitIndex, final boolean pOn)
//package com.java2s; //License from project: Apache License public class Main { /**//from w w w. j a va2 s . com * Method used to set a bit index to 1 or 0. * * @param pData * data to modify * @param pBitIndex * index to set * @param pOn * set bit at specified index to 1 or 0 * @return the modified byte */ public static byte setBit(final byte pData, final int pBitIndex, final boolean pOn) { if (pBitIndex < 0 || pBitIndex > 7) { throw new IllegalArgumentException( "parameter 'pBitIndex' must be between 0 and 7. pBitIndex=" + pBitIndex); } byte ret = pData; if (pOn) { // Set bit ret |= 1 << pBitIndex; } else { // Unset bit ret &= ~(1 << pBitIndex); } return ret; } }