org.xnio.Bits Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xnio;
/**
* General bit-affecting utility methods.
*
* @author David M. Lloyd
*/
public final class Bits {
private Bits() {}
//--- Bit mask methods
/**
* Get an integer bit mask consisting of 1 bits in the given range. The returned {@code int}
* will have bits {@code low} through {@code high} set, and all other bits clear.
*
* @param low the low bit value
* @param high the high bit value
* @return the bit mask
*/
public static int intBitMask(int low, int high) {
assert low >= 0;
assert low <= high;
assert high < 32;
return (high == 31 ? 0 : (1 << high + 1)) - (1 << low);
}
/**
* Get a long bit mask consisting of 1 bits in the given range. The returned {@code long}
* will have bits {@code low} through {@code high} set, and all other bits clear.
*
* @param low the low bit value
* @param high the high bit value
* @return the bit mask
*/
public static long longBitMask(int low, int high) {
assert low >= 0;
assert low <= high;
assert high < 64;
return (high == 63 ? 0L : (1L << (long) high + 1L)) - (1L << (long) low);
}
//--- Flags methods
/**
* Determine if any of the {@code flags} in the given {@code var} are set.
*
* @param var the value to test
* @param flags the flags to test for
* @return {@code true} if any of {@code flags} are in {@code var}, {@code false} otherwise
*/
public static boolean anyAreSet(int var, int flags) {
return (var & flags) != 0;
}
/**
* Determine if all of the {@code flags} in the given {@code var} are set.
*
* @param var the value to test
* @param flags the flags to test for
* @return {@code true} if all of {@code flags} are in {@code var}, {@code false} otherwise
*/
public static boolean allAreSet(int var, int flags) {
return (var & flags) == flags;
}
/**
* Determine if any of the {@code flags} in the given {@code var} are clear.
*
* @param var the value to test
* @param flags the flags to test for
* @return {@code true} if not all of {@code flags} are in {@code var}, {@code false} otherwise
*/
public static boolean anyAreClear(int var, int flags) {
return (var & flags) != flags;
}
/**
* Determine if all of the {@code flags} in the given {@code var} are clear.
*
* @param var the value to test
* @param flags the flags to test for
* @return {@code true} if none of {@code flags} are in {@code var}, {@code false} otherwise
*/
public static boolean allAreClear(int var, int flags) {
return (var & flags) == 0;
}
/**
* Determine if any of the {@code flags} in the given {@code var} are set.
*
* @param var the value to test
* @param flags the flags to test for
* @return {@code true} if any of {@code flags} are in {@code var}, {@code false} otherwise
*/
public static boolean anyAreSet(long var, long flags) {
return (var & flags) != 0;
}
/**
* Determine if all of the {@code flags} in the given {@code var} are set.
*
* @param var the value to test
* @param flags the flags to test for
* @return {@code true} if all of {@code flags} are in {@code var}, {@code false} otherwise
*/
public static boolean allAreSet(long var, long flags) {
return (var & flags) == flags;
}
/**
* Determine if any of the {@code flags} in the given {@code var} are clear.
*
* @param var the value to test
* @param flags the flags to test for
* @return {@code true} if not all of {@code flags} are in {@code var}, {@code false} otherwise
*/
public static boolean anyAreClear(long var, long flags) {
return (var & flags) != flags;
}
/**
* Determine if all of the {@code flags} in the given {@code var} are clear.
*
* @param var the value to test
* @param flags the flags to test for
* @return {@code true} if none of {@code flags} are in {@code var}, {@code false} otherwise
*/
public static boolean allAreClear(long var, long flags) {
return (var & flags) == 0;
}
//--- Signed/unsigned methods
/**
* Convert a signed value to unsigned.
*
* @param v the signed byte
* @return the unsigned byte, as an int
*/
public static int unsigned(byte v) {
return v & 0xff;
}
/**
* Convert a signed value to unsigned.
*
* @param v the signed short
* @return the unsigned short, as an int
*/
public static int unsigned(short v) {
return v & 0xffff;
}
/**
* Convert a signed value to unsigned.
*
* @param v the signed int
* @return the unsigned int, as a long
*/
public static long unsigned(int v) {
return v & 0xffffffffL;
}
//--- Byte array read methods
/**
* Get a 16-bit signed little-endian short value from a byte array.
*
* @param b the byte array
* @param off the offset in the array
* @return the signed short value
*/
public static short shortFromBytesLE(byte[] b, int off) {
return (short) (b[off + 1] << 8 | b[off] & 0xff);
}
/**
* Get a 16-bit signed big-endian short value from a byte array.
*
* @param b the byte array
* @param off the offset in the array
* @return the signed short value
*/
public static short shortFromBytesBE(byte[] b, int off) {
return (short) (b[off] << 8 | b[off + 1] & 0xff);
}
/**
* Get a 16-bit signed little-endian char value from a byte array.
*
* @param b the byte array
* @param off the offset in the array
* @return the signed char value
*/
public static char charFromBytesLE(byte[] b, int off) {
return (char) (b[off + 1] << 8 | b[off] & 0xff);
}
/**
* Get a 16-bit signed big-endian char value from a byte array.
*
* @param b the byte array
* @param off the offset in the array
* @return the signed char value
*/
public static char charFromBytesBE(byte[] b, int off) {
return (char) (b[off] << 8 | b[off + 1] & 0xff);
}
/**
* Get a 24-bit signed little-endian int value from a byte array.
*
* @param b the byte array
* @param off the offset in the array
* @return the signed medium value as an int
*/
public static int mediumFromBytesLE(byte[] b, int off) {
return (b[off + 2] & 0xff) << 16 | (b[off + 1] & 0xff) << 8 | b[off] & 0xff;
}
/**
* Get a 24-bit signed big-endian int value from a byte array.
*
* @param b the byte array
* @param off the offset in the array
* @return the signed medium value as an int
*/
public static int mediumFromBytesBE(byte[] b, int off) {
return (b[off] & 0xff) << 16 | (b[off + 1] & 0xff) << 8 | b[off + 2] & 0xff;
}
/**
* Get a 32-bit signed little-endian int value from a byte array.
*
* @param b the byte array
* @param off the offset in the array
* @return the signed int value
*/
public static int intFromBytesLE(byte[] b, int off) {
return b[off + 3] << 24 | (b[off + 2] & 0xff) << 16 | (b[off + 1] & 0xff) << 8 | b[off] & 0xff;
}
/**
* Get a 32-bit signed big-endian int value from a byte array.
*
* @param b the byte array
* @param off the offset in the array
* @return the signed int value
*/
public static int intFromBytesBE(byte[] b, int off) {
return b[off] << 24 | (b[off + 1] & 0xff) << 16 | (b[off + 2] & 0xff) << 8 | b[off + 3] & 0xff;
}
/**
* Get a 64-bit signed little-endian long value from a byte array.
*
* @param b the byte array
* @param off the offset in the array
* @return the signed long value
*/
public static long longFromBytesLE(byte[] b, int off) {
return (b[off + 7] & 0xffL) << 56L | (b[off + 6] & 0xffL) << 48L | (b[off + 5] & 0xffL) << 40L | (b[off + 4] & 0xffL) << 32L | (b[off + 3] & 0xffL) << 24L | (b[off + 2] & 0xffL) << 16L | (b[off + 1] & 0xffL) << 8L | b[off] & 0xffL;
}
/**
* Get a 64-bit signed big-endian long value from a byte array.
*
* @param b the byte array
* @param off the offset in the array
* @return the signed long value
*/
public static long longFromBytesBE(byte[] b, int off) {
return (b[off] & 0xffL) << 56L | (b[off + 1] & 0xffL) << 48L | (b[off + 2] & 0xffL) << 40L | (b[off + 3] & 0xffL) << 32L | (b[off + 4] & 0xffL) << 24L | (b[off + 5] & 0xffL) << 16L | (b[off + 6] & 0xffL) << 8L | b[off + 7] & 0xffL;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy