Java Bit Set setBit(byte[] data, int pos, int val)

Here you can find the source of setBit(byte[] data, int pos, int val)

Description

set Bit

License

Open Source License

Declaration

public static byte[] setBit(byte[] data, int pos, int val) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static byte[] setBit(byte[] data, int pos, int val) {

        if ((data.length * 8) - 1 < pos)
            throw new Error("outside byte array limit, pos: " + pos);

        int posByte = data.length - 1 - (pos) / 8;
        int posBit = (pos) % 8;
        byte setter = (byte) (1 << (posBit));
        byte toBeSet = data[posByte];
        byte result;
        if (val == 1)
            result = (byte) (toBeSet | setter);
        else/*from ww w. ja v  a  2  s.co m*/
            result = (byte) (toBeSet & ~setter);

        data[posByte] = result;
        return data;
    }
}

Related

  1. setBit(byte[] byteArray, int index)
  2. setBit(byte[] bytes, int bitNr, int bit)
  3. setBit(byte[] bytes, int off, boolean v)
  4. setBit(byte[] data, int index, boolean value)
  5. setBit(byte[] data, int pos, boolean val)
  6. setBit(byte[] data, int pos, int val)
  7. setBit(byte[] data, long pos, byte val)
  8. setBit(final byte input, final int bit, final boolean status)
  9. setBit(final byte pData, final int pBitIndex, final boolean pOn)