SoftNet-Consult Java Utility Library

com.softnetConsult.utils.math
Class BitmapTools

java.lang.Object
  extended by com.softnetConsult.utils.math.BitmapTools

public final class BitmapTools
extends java.lang.Object

This class provides implementations for various bit-level operations of Java types that can be used as bit-maps.

This product includes software developed by the SoftNet-Consult Java Utility Library project and its contributors.
(http://java-tools.sourceforge.net)
Copyright (c) 2007-2008 SoftNet-Consult.
Copyright (c) 2007-2008 G. Paperin.
All rights reserved.

File: BitmapTools.java
Library API version: "2.02"
Java compliance version: "1.5"

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following terms and conditions are met:

1. Redistributions of source code must retain the above acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software or any derived software must display the following acknowledgement:
This product includes software developed by the SoftNet-Consult Java Utility Library project and its contributors.

THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS 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.

Version:
"2.02"
Author:
Greg Paperin (http://www.paperin.org)

Constructor Summary
private BitmapTools()
          Prevents instances of this class from being created as this class contains only static utility methods.
 
Method Summary
static boolean checkBit(byte val, int bitNum)
          Checks the state of a specified bit in the specified bit-map.
static boolean checkBit(int val, int bitNum)
          Checks the state of a specified bit in the specified bit-map.
static boolean checkBit(long val, int bitNum)
          Checks the state of a specified bit in the specified bit-map.
static boolean checkBit(short val, int bitNum)
          Checks the state of a specified bit in the specified bit-map.
static byte clearBit(byte val, int bitNum)
          Clears a specified bit in the specified bit-map to 0.
static int clearBit(int val, int bitNum)
          Clears a specified bit in the specified bit-map to 0.
static long clearBit(long val, int bitNum)
          Clears a specified bit in the specified bit-map to 0.
static short clearBit(short val, int bitNum)
          Clears a specified bit in the specified bit-map to 0.
static int hammingDistance(boolean[] a, boolean[] b)
          Computes the hamming distance between two boolean arrays of the same length.
static int hammingDistance(boolean[] a, int offsA, boolean[] b, int offsB, int len)
          Computes the hamming distance between specified sections of two boolean arrays.
static int hammingDistance(int a, int b)
          Computes the haming distance between the bit-patterns that represent a and b.
static int hammingDistance(int a, int b, int len)
          Computes the haming distance between the bit-patterns that represent a and b, only len rightmost bits are considered.
static int hammingDistance(long a, long b)
          Computes the haming distance between the bit-patterns that represent a and b.
static int hammingDistance(long a, long b, int len)
          Computes the haming distance between the bit-patterns that represent a and b, only len rightmost bits are considered.
static byte setBit(byte val, int bitNum)
          Sets a specified bit in the specified bit-map to 1.
static int setBit(int val, int bitNum)
          Sets a specified bit in the specified bit-map to 1.
static long setBit(long val, int bitNum)
          Sets a specified bit in the specified bit-map to 1.
static short setBit(short val, int bitNum)
          Sets a specified bit in the specified bit-map to 1.
static byte toggleBit(byte val, int bitNum)
          Toggles a specified bit in the specified bit-map from 1 to 0 or from 0 to 1 respectively.
static int toggleBit(int val, int bitNum)
          Toggles a specified bit in the specified bit-map from 1 to 0 or from 0 to 1 respectively.
static long toggleBit(long val, int bitNum)
          Toggles a specified bit in the specified bit-map from 1 to 0 or from 0 to 1 respectively.
static short toggleBit(short val, int bitNum)
          Toggles a specified bit in the specified bit-map from 1 to 0 or from 0 to 1 respectively.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BitmapTools

private BitmapTools()
Prevents instances of this class from being created as this class contains only static utility methods.

Method Detail

hammingDistance

public static int hammingDistance(int a,
                                  int b,
                                  int len)
Computes the haming distance between the bit-patterns that represent a and b, only len rightmost bits are considered.

Parameters:
a - Some bit-pattern.
b - Some bit-pattern.
len - Number of rightmost bits to consider (0 to 32).
Returns:
The hamming distance between the two specified patterns.

hammingDistance

public static int hammingDistance(int a,
                                  int b)
Computes the haming distance between the bit-patterns that represent a and b.

Parameters:
a - Some bit-pattern.
b - Some bit-pattern.
Returns:
The hamming distance between the two specified patterns.

hammingDistance

public static int hammingDistance(long a,
                                  long b,
                                  int len)
Computes the haming distance between the bit-patterns that represent a and b, only len rightmost bits are considered.

Parameters:
a - Some bit-pattern.
b - Some bit-pattern.
len - Number of rightmost bits to consider (0 to 64).
Returns:
The hamming distance between the two specified patterns.

hammingDistance

public static int hammingDistance(long a,
                                  long b)
Computes the haming distance between the bit-patterns that represent a and b.

Parameters:
a - Some bit-pattern.
b - Some bit-pattern.
Returns:
The hamming distance between the two specified patterns.

hammingDistance

public static int hammingDistance(boolean[] a,
                                  boolean[] b)
Computes the hamming distance between two boolean arrays of the same length.

Parameters:
a - A non-null array.
b - A non-null array.
Returns:
The number of positions in which the two specified arrays differ.
Throws:
java.lang.NullPointerException - if a null reference was passed.
java.lang.IllegalArgumentException - if the specified arrays have different lengths.

hammingDistance

public static int hammingDistance(boolean[] a,
                                  int offsA,
                                  boolean[] b,
                                  int offsB,
                                  int len)
Computes the hamming distance between specified sections of two boolean arrays.

Parameters:
a - A non-null array.
offsA - The position from which to start comparison in array a (from left).
b - A non-null array.
offsB - The position from which to start comparison in array b (from left).
len - The number of positions to compare.
Returns:
The number of positions in which the two specified arrays differ within the specified interval.
Throws:
java.lang.NullPointerException - if a null reference was passed.
java.lang.IllegalArgumentException - if any of the specified intervals does not fit the corresponding array.

setBit

public static byte setBit(byte val,
                          int bitNum)
Sets a specified bit in the specified bit-map to 1.

Parameters:
val - A bit-map.
bitNum - The index of the bit to set (rightmost bit has index 0).
Returns:
The resulting bit-map.

setBit

public static short setBit(short val,
                           int bitNum)
Sets a specified bit in the specified bit-map to 1.

Parameters:
val - A bit-map.
bitNum - The index of the bit to set (rightmost bit has index 0).
Returns:
The resulting bit-map.

setBit

public static int setBit(int val,
                         int bitNum)
Sets a specified bit in the specified bit-map to 1.

Parameters:
val - A bit-map.
bitNum - The index of the bit to set (rightmost bit has index 0).
Returns:
The resulting bit-map.

setBit

public static long setBit(long val,
                          int bitNum)
Sets a specified bit in the specified bit-map to 1.

Parameters:
val - A bit-map.
bitNum - The index of the bit to set (rightmost bit has index 0).
Returns:
The resulting bit-map.

clearBit

public static byte clearBit(byte val,
                            int bitNum)
Clears a specified bit in the specified bit-map to 0.

Parameters:
val - A bit-map.
bitNum - The index of the bit to clear (rightmost bit has index 0).
Returns:
The resulting bit-map.

clearBit

public static short clearBit(short val,
                             int bitNum)
Clears a specified bit in the specified bit-map to 0.

Parameters:
val - A bit-map.
bitNum - The index of the bit to clear (rightmost bit has index 0).
Returns:
The resulting bit-map.

clearBit

public static int clearBit(int val,
                           int bitNum)
Clears a specified bit in the specified bit-map to 0.

Parameters:
val - A bit-map.
bitNum - The index of the bit to clear (rightmost bit has index 0).
Returns:
The resulting bit-map.

clearBit

public static long clearBit(long val,
                            int bitNum)
Clears a specified bit in the specified bit-map to 0.

Parameters:
val - A bit-map.
bitNum - The index of the bit to clear (rightmost bit has index 0).
Returns:
The resulting bit-map.

toggleBit

public static byte toggleBit(byte val,
                             int bitNum)
Toggles a specified bit in the specified bit-map from 1 to 0 or from 0 to 1 respectively.

Parameters:
val - A bit-map.
bitNum - The index of the bit to toggle (rightmost bit has index 0).
Returns:
The resulting bit-map.

toggleBit

public static short toggleBit(short val,
                              int bitNum)
Toggles a specified bit in the specified bit-map from 1 to 0 or from 0 to 1 respectively.

Parameters:
val - A bit-map.
bitNum - The index of the bit to toggle (rightmost bit has index 0).
Returns:
The resulting bit-map.

toggleBit

public static int toggleBit(int val,
                            int bitNum)
Toggles a specified bit in the specified bit-map from 1 to 0 or from 0 to 1 respectively.

Parameters:
val - A bit-map.
bitNum - The index of the bit to toggle (rightmost bit has index 0).
Returns:
The resulting bit-map.

toggleBit

public static long toggleBit(long val,
                             int bitNum)
Toggles a specified bit in the specified bit-map from 1 to 0 or from 0 to 1 respectively.

Parameters:
val - A bit-map.
bitNum - The index of the bit to toggle (rightmost bit has index 0).
Returns:
The resulting bit-map.

checkBit

public static boolean checkBit(byte val,
                               int bitNum)
Checks the state of a specified bit in the specified bit-map.

Parameters:
val - A bit-map.
bitNum - The index of the bit to toggle (rightmost bit has index 0).
Returns:
true if the specified bit is 1; false if the specified bit is 0.

checkBit

public static boolean checkBit(short val,
                               int bitNum)
Checks the state of a specified bit in the specified bit-map.

Parameters:
val - A bit-map.
bitNum - The index of the bit to toggle (rightmost bit has index 0).
Returns:
true if the specified bit is 1; false if the specified bit is 0.

checkBit

public static boolean checkBit(int val,
                               int bitNum)
Checks the state of a specified bit in the specified bit-map.

Parameters:
val - A bit-map.
bitNum - The index of the bit to toggle (rightmost bit has index 0).
Returns:
true if the specified bit is 1; false if the specified bit is 0.

checkBit

public static boolean checkBit(long val,
                               int bitNum)
Checks the state of a specified bit in the specified bit-map.

Parameters:
val - A bit-map.
bitNum - The index of the bit to toggle (rightmost bit has index 0).
Returns:
true if the specified bit is 1; false if the specified bit is 0.

SoftNet-Consult Java Utility Library is a member of SourceForge.net