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

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

Description

set Bit

License

LGPL

Declaration

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

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    public static void setBit(byte[] data, int pos, boolean val) {
        int posByte = pos / 8;
        int posBit = pos % 8;
        byte oldByte = data[posByte];
        if (val) {
            data[posByte] = (byte) (oldByte | (1 << 7 - posBit));
        } else {//from   w w  w. j  av a  2 s  .  c o  m
            data[posByte] = (byte) (oldByte | (0 << 7 - posBit));
        }
    }
}

Related

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