ru.sstu.math.util
Class BitsUtil

java.lang.Object
  extended by ru.sstu.math.util.BitsUtil

public final class BitsUtil
extends Object

BitsUtil class contains of utility methods for working with bits in integer numbers.

Since:
Math 1.0
Author:
Denis A. Murashev

Method Summary
static int clearLeadingBits(int number, int base)
          Clears leading bits.
static long clearLeadingBits(long number, int base)
          Clears leading bits.
static int getMostSignificantBit(int number)
          Provides most significant bit for given number.
static long getMostSignificantBit(long number)
          Provides most significant bit for given number.
static boolean isPowerOf2(int number)
          Checks if number is power of 2.
static boolean isPowerOf2(long number)
           
static boolean testBit(int number, int bit)
          Test if given bit is set.
static boolean testBit(long number, int bit)
          Test if given bit is set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isPowerOf2

public static boolean isPowerOf2(int number)
Checks if number is power of 2.

Parameters:
number - integer number to be tested
Returns:
true if given number is rower of 2

getMostSignificantBit

public static int getMostSignificantBit(int number)
Provides most significant bit for given number.

Parameters:
number - integer number
Returns:
most significant bit

testBit

public static boolean testBit(int number,
                              int bit)
Test if given bit is set.

Parameters:
number - integer number to be tested
bit - index of bit to be tested beginning from 0
Returns:
true if bit is set

clearLeadingBits

public static int clearLeadingBits(int number,
                                   int base)
Clears leading bits. Only base bits remains unchanged.

Parameters:
number - initial number
base - number of bits that won't be changed
Returns:
number with dropped leading bits

isPowerOf2

public static boolean isPowerOf2(long number)
Parameters:
number - integer number to be tested
Returns:
true if given number is rower of 2

getMostSignificantBit

public static long getMostSignificantBit(long number)
Provides most significant bit for given number.

Parameters:
number - integer number
Returns:
most significant bit

testBit

public static boolean testBit(long number,
                              int bit)
Test if given bit is set.

Parameters:
number - integer number to be tested
bit - index of bit to be tested beginning from 0
Returns:
true if bit is set

clearLeadingBits

public static long clearLeadingBits(long number,
                                    int base)
Clears leading bits. Only base bits remains unchanged.

Parameters:
number - initial number
base - number of bits that won't be changed
Returns:
number with dropped leading bits


Copyright © 2011 TFI branch of Engels Instutute of Technology. All Rights Reserved.