Class BitShiftUtil

java.lang.Object
com.seibel.distanthorizons.coreapi.util.BitShiftUtil

public class BitShiftUtil extends Object
A list of helper methods to make code easier to read.
Specifically written because bit shifts short circuit James' brain.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    divideByPowerOfTwo(int value, int power)
    Equivalent to:
    value >> power,
    value / 2^power

    Note: value / 2^power isn't identical for negative values
    static long
    divideByPowerOfTwo(long value, long power)
    see divideByPowerOfTwo(int, int) for documentation
    static int
    half(int value)
    Equivalent to:
    value >> 1,
    value / 2

    Note: value / 2 isn't identical for negative values
    static long
    half(long value)
    see half(int) for documentation
    static int
    pow(int value, int power)
    Equivalent to:
    value << power,
    value^power,
    Math.pow(value, power)

    Note: Math.pow() isn't identical for large values where bits would be lost in the shift, however for medium to small values they function the same.
    static long
    pow(long value, long power)
    see pow(int, int) for documentation
    static int
    powerOfTwo(int value)
    Equivalent to:
    1 << value,
    2^value,
    Math.pow(2, value)

    Note: Math.pow() isn't identical for large values where bits would be lost in the shift, however for medium to small values they function the same.
    static long
    powerOfTwo(long value)
    see powerOfTwo(int) for documentation
    static int
    square(int value)
    Equivalent to:
    value << 1,
    value^2,
    Math.pow(value, 2)

    Note: Math.pow() isn't identical for large values where bits would be lost in the shift, however for medium to small values they function the same.
    static long
    square(long value)
    see square(int) for documentation

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BitShiftUtil

      public BitShiftUtil()
  • Method Details

    • powerOfTwo

      public static int powerOfTwo(int value)
      Equivalent to:
      1 << value,
      2^value,
      Math.pow(2, value)

      Note: Math.pow() isn't identical for large values where bits would be lost in the shift, however for medium to small values they function the same.

      Can also be used to replace bit shifts in the format:
      multiplier << value;
      multiplier * powerOfTwo(value);
    • powerOfTwo

      public static long powerOfTwo(long value)
      see powerOfTwo(int) for documentation
    • half

      public static int half(int value)
      Equivalent to:
      value >> 1,
      value / 2

      Note: value / 2 isn't identical for negative values
    • half

      public static long half(long value)
      see half(int) for documentation
    • divideByPowerOfTwo

      public static int divideByPowerOfTwo(int value, int power)
      Equivalent to:
      value >> power,
      value / 2^power

      Note: value / 2^power isn't identical for negative values
    • divideByPowerOfTwo

      public static long divideByPowerOfTwo(long value, long power)
      see divideByPowerOfTwo(int, int) for documentation
    • square

      public static int square(int value)
      Equivalent to:
      value << 1,
      value^2,
      Math.pow(value, 2)

      Note: Math.pow() isn't identical for large values where bits would be lost in the shift, however for medium to small values they function the same.
    • square

      public static long square(long value)
      see square(int) for documentation
    • pow

      public static int pow(int value, int power)
      Equivalent to:
      value << power,
      value^power,
      Math.pow(value, power)

      Note: Math.pow() isn't identical for large values where bits would be lost in the shift, however for medium to small values they function the same.
    • pow

      public static long pow(long value, long power)
      see pow(int, int) for documentation