Description
Sets the bit at the specified index of b to 0.
License
Open Source License
Parameter
Parameter | Description |
---|
b | the byte array |
index | the index of the bit to set to 0 |
Exception
Parameter | Description |
---|
IndexOutOfBoundsException | an exception |
Return
the byte array with the bit at the specified index set to 0
Declaration
public static byte[] unsetBit(byte[] b, int index)
Method Source Code
//package com.java2s;
/*/* w w w. j a v a 2 s .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 0. 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 0
*
* @return the byte array with the bit at the specified index set to 0
*
* @throws IndexOutOfBoundsException
*/
public static byte[] unsetBit(byte[] b, int index) {
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
- unset(byte[] bytes)
- unsetb(int num, int bitmask)
- UnsetBit(byte _bitset, byte bit)
- unsetBit(byte b, int pos)
- unsetBit(byte original, int bitToUnSet)
- unsetBit(int f, int i)
- unSetBit(int integer, int bit)
- unsetBit(int value, int flags)
- unsetBit(long flags, long bit)