Java Bit Set setBit(byte[] ba, int bit_offset, boolean on)

Here you can find the source of setBit(byte[] ba, int bit_offset, boolean on)

Description

set Bit

License

Apache License

Declaration

public static void setBit(byte[] ba, int bit_offset, boolean on) 

Method Source Code

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

public class Main {
    public static void setBit(byte[] ba, int bit_offset, boolean on) {
        int byte_idx = bit_offset / 8;
        int bit_remainder = bit_offset % 8;
        byte bit_mask = (byte) ((0x1 << (7 - bit_remainder)) & 0xFF);

        // System.out.println("byte_idx="+byte_idx+"
        // bit_mask="+Long.toHexString((long)bit_mask));

        if (on) {
            ba[byte_idx] |= bit_mask;
        } else {/*  w  ww  .  ja v  a  2  s . co m*/
            ba[byte_idx] &= ~bit_mask;
        }
    }
}

Related

  1. setBit(byte value, int bit, boolean on)
  2. setBit(byte value, int bit, boolean state)
  3. setBit(byte value, int bit, boolean state)
  4. setBit(byte[] arr, int bit)
  5. setBit(byte[] b, int index)
  6. setBit(byte[] byteArray, int index)
  7. setBit(byte[] bytes, int bitNr, int bit)
  8. setBit(byte[] bytes, int off, boolean v)
  9. setBit(byte[] data, int index, boolean value)