Java Array Flip flipAllBitsInPlace(byte[] bytes)

Here you can find the source of flipAllBitsInPlace(byte[] bytes)

Description

This flips the bits and returns the same byte[].

License

Open Source License

Return

bytes

Declaration

public static byte[] flipAllBitsInPlace(byte[] bytes) 

Method Source Code

//package com.java2s;
/**//from w  ww  .  ja  v  a  2 s  .c  o m
 * Copyright 2015 Palantir Technologies
 *
 * Licensed under the BSD-3 License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://opensource.org/licenses/BSD-3-Clause
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * This flips the bits and returns the same byte[].
     * @return bytes
     */
    public static byte[] flipAllBitsInPlace(byte[] bytes) {
        return flipAllBitsInPlace(bytes, 0, bytes.length);
    }

    /**
     * This flips the bits starting at index and returns the same byte[].
     * @return bytes
     */
    public static byte[] flipAllBitsInPlace(byte[] bytes, int index) {
        return flipAllBitsInPlace(bytes, index, bytes.length - index);
    }

    /**
     * This flips the bits in the range [index, index + length) and returns the same byte[].
     * @return bytes
     */
    public static byte[] flipAllBitsInPlace(byte[] bytes, int index, int length) {
        int endIndex = Math.min(bytes.length, index + length);
        for (int i = index; i < endIndex; i++) {
            bytes[i] = (byte) (bytes[i] ^ 0xff);
        }
        return bytes;
    }
}

Related

  1. flip(String[][] data)
  2. flip(T[] array)
  3. flipAllBits(byte[] bytes)
  4. flipChessboardVertically(int[] chessboard)