Java Bit Flip flipBits(byte[] bytes, int start, int bitLength)

Here you can find the source of flipBits(byte[] bytes, int start, int bitLength)

Description

flip Bits

License

Open Source License

Declaration

public static void flipBits(byte[] bytes, int start, int bitLength) 

Method Source Code

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

public class Main {
    public static void flipBits(byte[] bytes, int start, int bitLength) {
        for (int i = 0; i < bitLength / 2; i++) {
            boolean leftBit = getBit(bytes, start * 8 + i);
            boolean rightBit = getBit(bytes, start * 8 + bitLength - i - 1);
            setBit(bytes, start * 8 + i, rightBit);
            setBit(bytes, start * 8 + bitLength - i - 1, leftBit);
        }//from   w  w  w.j  av a2  s .  co  m
    }

    public static boolean getBit(byte[] bytes, int off) {
        byte b = bytes[off / 8];
        return (b & (0x01 << (off % 8))) != 0;
    }

    public static void setBit(byte[] bytes, int off, boolean v) {
        if (v)
            bytes[off / 8] |= (0x01 << (off % 8));
        else
            bytes[off / 8] &= ~(0x01 << (off % 8));
    }
}

Related

  1. flip(int i)
  2. flip(int value)
  3. flip(long a)
  4. flipBitAsBinaryString(int flipBit)
  5. flipBitAt(int bitIndex, byte b)
  6. flipBits(int n)
  7. flipBits(int value)
  8. flipC(long v, int off)
  9. FLIPENDIAN_INT32(int x)