Class Bitwise

java.lang.Object
mathvoyage.bitwise.Bitwise

public class Bitwise extends Object
The type Bitwise.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    addOK(int x, int y, boolean isUnsigned)
    returns true if the addition of x and y does not result in overflow, and false otherwise.
    int
    BitWiseAND(int a, int b)
    Return the bitwise And value
    int
    BitWiseNOT(int a)
    Return the NOT value
    int
    BitWiseOR(int a, int b)
    Return the OR value
    int
    BitWiseXOR(int a, int b)
    Return the XOR value
    bitwiseZeroFillRightShift(int binaryNumber, int shiftAmount)
    Return the zero fill right shift value
    int
    getByte(int x, int n, boolean isUnsigned)
    Returns the value of the n-th byte of the integer x
    static Bitwise
    Returns the singleton instance of this class.
    int
    leftShift(int num, int shiftBy, boolean isUnsigned)
    Return the LeftShift value
    int
    logicalShift(int x, int n, boolean isUnsigned)
    Returns an integer resulting from the logical right shift
    int
    rightShift(int num, int shiftBy, boolean isUnsigned)
    Return the rightshift value
    int
    rotateLeft(int x, int n, boolean isUnsigned)
    Returns an integer resulting from the left rotation
    int
    rotateRight(int x, int n, boolean isUnsigned)
    Returns an integer resulting from the right rotation
    int
    setBit0(int n, int i)
    Returns an integer where the bit at position i in n is set to 0, and all other bits remain unchanged.
    int
    setBit1(int n, int i)
    Returns an integer where the bit at position i in n is set to 1, and all other bits remain unchanged.
    int
    toggleBit(int n, int i)
    returns an integer where the bit at position i in n is flipped

    Methods inherited from class java.lang.Object

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

    • getInstance

      public static Bitwise getInstance()
      Returns the singleton instance of this class.
      Returns:
      The singleton instance
    • BitWiseOR

      public int BitWiseOR(int a, int b)
      Return the OR value
      Parameters:
      a - is the int value given
      b - is the int value given
      Returns:
      or value
    • BitWiseAND

      public int BitWiseAND(int a, int b)
      Return the bitwise And value
      Parameters:
      a - is the given value
      b - is the given value
      Returns:
      AND value
    • BitWiseNOT

      public int BitWiseNOT(int a)
      Return the NOT value
      Parameters:
      a - is the int value
      Returns:
      the NOT value
    • BitWiseXOR

      public int BitWiseXOR(int a, int b)
      Return the XOR value
      Parameters:
      a - is the int value
      b - is the int value
      Returns:
      the XOR value
    • rightShift

      public int rightShift(int num, int shiftBy, boolean isUnsigned)
      Return the rightshift value
      Parameters:
      num - the int value
      shiftBy - is the value of how much value need to be shifted
      isUnsigned - is the boolean value
      Returns:
      the rightshift value
    • leftShift

      public int leftShift(int num, int shiftBy, boolean isUnsigned)
      Return the LeftShift value
      Parameters:
      num - the int value
      shiftBy - is the value of how much value need to be shifted
      isUnsigned - is the boolean value
      Returns:
      the LeftShift value
    • bitwiseZeroFillRightShift

      public String bitwiseZeroFillRightShift(int binaryNumber, int shiftAmount)
      Return the zero fill right shift value
      Parameters:
      binaryNumber - the int value
      shiftAmount - is the value of how much value need to be shifted
      Returns:
      the zero fill right shift value`
    • getByte

      public int getByte(int x, int n, boolean isUnsigned)
      Returns the value of the n-th byte of the integer x
      Parameters:
      x - The integer from which to extract the byte.
      n - The byte position to extract
      isUnsigned - Whether the number is unsigned
      Returns:
      the value of the n-th byte of the integer x
    • setBit0

      public int setBit0(int n, int i)
      Returns an integer where the bit at position i in n is set to 0, and all other bits remain unchanged.
      Parameters:
      n - The integer on which to operate.
      i - The bit position to set to 0.
      Returns:
      an integer where the bit at position i in n is set to 0, and all other bits remain unchanged.
    • setBit1

      public int setBit1(int n, int i)
      Returns an integer where the bit at position i in n is set to 1, and all other bits remain unchanged.
      Parameters:
      n - The integer on which to operate.
      i - The bit position to set to 1.
      Returns:
      an integer where the bit at position i in n is set to 1, and all other bits remain unchanged.
    • toggleBit

      public int toggleBit(int n, int i)
      returns an integer where the bit at position i in n is flipped
      Parameters:
      n - The integer on which to operate.
      i - The bit position to toggle.
      Returns:
      an integer where the bit at position i in n is flipped
    • logicalShift

      public int logicalShift(int x, int n, boolean isUnsigned)
      Returns an integer resulting from the logical right shift
      Parameters:
      x - The integer on which to perform the shift.
      n - The number of positions to shift to the right.
      isUnsigned - Whether the number is unsigned.
      Returns:
      an integer resulting from the logical right shift
    • rotateRight

      public int rotateRight(int x, int n, boolean isUnsigned)
      Returns an integer resulting from the right rotation
      Parameters:
      x - The integer on which to perform the rotation.
      n - The number of positions to rotate to the right.
      isUnsigned - Whether the number is unsigned.
      Returns:
      an integer resulting from the right rotation
    • rotateLeft

      public int rotateLeft(int x, int n, boolean isUnsigned)
      Returns an integer resulting from the left rotation
      Parameters:
      x - The integer on which to perform the rotation
      n - The number of positions to rotate to the left
      isUnsigned - Whether the number is unsigned
      Returns:
      an integer resulting from the left rotation
    • addOK

      public boolean addOK(int x, int y, boolean isUnsigned)
      returns true if the addition of x and y does not result in overflow, and false otherwise.
      Parameters:
      x - The first integer operand.
      y - The second integer operand.
      isUnsigned - Whether the numbers are unsigned.
      Returns:
      true if the addition of x and y does not result in overflow, and false otherwise.