Java Bit Set setBit(byte[] b, int index)

Here you can find the source of setBit(byte[] b, int index)

Description

Sets the bit at the specified index of b to 1.

License

Open Source License

Parameter

Parameter Description
b the byte array
index the index of the bit to set to 1

Exception

Parameter Description
IndexOutOfBoundsException an exception

Return

the byte array with the bit at the specified index set to 1

Declaration

public static byte[] setBit(byte[] b, int index) throws IndexOutOfBoundsException 

Method Source Code

//package com.java2s;
/*/* ww  w .j  a  va 2s .c o  m*/
 * The MIT License
 *
 * Copyright 2013 Georgios Migdos <cyberpython@gmail.com>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class Main {
    /**
     * Sets the bit at the specified index of b to 1. The index is calculated
     * from right to left - i.e. the least significant bit is considered to be
     * the rightmost (index = 0), while the most significant bit is considered
     * to be the leftmost (index = b.length*8 -1) if the whole array is treated
     * as a series of consecutive bits.
     *
     * @param b the byte array
     * @param index the index of the bit to set to 1
     *
     * @return the byte array with the bit at the specified index set to 1
     *
     * @throws IndexOutOfBoundsException
     */
    public static byte[] setBit(byte[] b, int index) throws IndexOutOfBoundsException {
        int maxIndex = b.length * 8 - 1;
        if ((index < 0) || (index > maxIndex)) {
            throw new IndexOutOfBoundsException(
                    "Invalid bit index: " + index + " for " + b.length + "-byte array.");
        }
        int byteOffset = b.length - (index / 8) - 1;
        int bitOffset = index % 8;
        byte mask = (byte) (0xFF & (1 << bitOffset));
        b[byteOffset] = (byte) (0xFF & (b[byteOffset] | mask));
        return b;
    }
}

Related

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