Package mars.util

Class Binary

java.lang.Object
mars.util.Binary

public class Binary extends Object
Some utility methods for working with binary representations.
Version:
July 2005
Author:
Pete Sanderson, Ken Vollmar, and Jason Bumgarner
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Translate String consisting of '1's and '0's into String equivalent of the corresponding hexadecimal value.
    static int
    Translate String consisting of '1's and '0's into an int value having that binary representation.
    static long
    Translate String consisting of '1's and '0's into a long value having that binary representation.
    static int
    bitValue(int value, int bit)
    Returns the bit value of the given bit position of the given int value.
    static int
    bitValue(long value, int bit)
    Returns the bit value of the given bit position of the given long value.
    static int
    clearBit(int value, int bit)
    Sets the specified bit of the specified value to 0, and returns the result.
    static int
    Convert the given string to a 32 bit integer, accounting for radix prefixes.
    static long
    Convert the given string to a 64 bit integer, accounting for radix prefixes.
    static int
    getByte(int value, int position)
    Gets the specified byte of the specified value.
    static String
    Translate String consisting of hexadecimal digits into String consisting of corresponding binary digits ('1's and '0's).
    static short
    highOrderIntToShort(int intValue)
    Returns a 16-bit integer representing the high-order 16 bits of a given 32-bit integer.
    static int
    highOrderLongToInt(long longValue)
    Returns a 32-bit integer representing the high-order 32 bits of a given 64-bit integer.
    static String
    intToAscii(int value)
    Produce ASCII string equivalent of integer value, interpreting it as 4 one-byte characters.
    static String
    intToBinaryString(int value)
    Translate int value into a String consisting of '1's and '0's.
    static String
    intToBinaryString(int value, int length)
    Translate int value into a String consisting of '1's and '0's.
    static String
    intToHalfHexString(int value)
    Returns a 6 character string representing the 16-bit hexadecimal equivalent of the given integer value.
    static String
    intToHexString(int value)
    Prefix a hexadecimal-indicating string "0x" to the string which is returned by the method Integer.toHexString(int).
    static boolean
    isHex(String string)
    Parsing method to see if a string represents a hex number.
    static boolean
    isOctal(String string)
    Parsing method to see if a string represents an octal number.
    static String
    longToHexString(long value)
    Prefix a hexadecimal-indicating string "0x" to the string equivalent to the hexadecimal value in the long parameter.
    static short
    lowOrderIntToShort(int intValue)
    Returns a 16-bit integer representing the low-order 16 bits of a given 32-bit integer.
    static int
    lowOrderLongToInt(long longValue)
    Returns a 32-bit integer representing the low-order 32 bits of a given 64-bit integer.
    static int
    setBit(int value, int bit)
    Sets the specified bit of the specified value to 1, and returns the result.
    static int
    setByte(int value, int position, int replace)
    Sets the specified byte of the specified value to the low order 8 bits of specified replacement value, and returns the result.
    static long
    twoIntsToLong(int highOrder, int lowOrder)
    Returns a 64-bit integer representing the concatenation of two given 32-bit integers.
    static int
    twoShortsToInt(short highOrder, short lowOrder)
    Returns a 32-bit integer representing the concatenation of two given 16-bit integers.

    Methods inherited from class java.lang.Object

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

    • Binary

      public Binary()
  • Method Details

    • intToBinaryString

      public static String intToBinaryString(int value, int length)
      Translate int value into a String consisting of '1's and '0's.
      Parameters:
      value - The int value to convert.
      length - The number of bit positions, starting at least significant, to process.
      Returns:
      String consisting of '1' and '0' characters corresponding to the requested binary sequence.
    • intToBinaryString

      public static String intToBinaryString(int value)
      Translate int value into a String consisting of '1's and '0's. Assumes all 32 bits are to be translated.
      Parameters:
      value - The int value to convert.
      Returns:
      String consisting of '1' and '0' characters corresponding to the requested binary sequence.
    • binaryStringToInt

      public static int binaryStringToInt(String string)
      Translate String consisting of '1's and '0's into an int value having that binary representation. The String is assumed to be at most 32 characters long. No error checking is performed. String position 0 has most-significant bit, position length-1 has least-significant.
      Parameters:
      string - The String value to convert.
      Returns:
      int whose binary value corresponds to decoded String.
    • binaryStringToLong

      public static long binaryStringToLong(String string)
      Translate String consisting of '1's and '0's into a long value having that binary representation. The String is assumed to be at most 64 characters long. No error checking is performed. String position 0 has most-significant bit, position length-1 has least-significant.
      Parameters:
      string - The String value to convert.
      Returns:
      long whose binary value corresponds to decoded String.
    • binaryStringToHexString

      public static String binaryStringToHexString(String value)
      Translate String consisting of '1's and '0's into String equivalent of the corresponding hexadecimal value. No length limit. String position 0 has most-significant bit, position length-1 has least-significant.
      Parameters:
      value - The String value to convert.
      Returns:
      String containing '0', '1', ...'F' characters which form hexadecimal equivalent of decoded String.
    • hexStringToBinaryString

      public static String hexStringToBinaryString(String value)
      Translate String consisting of hexadecimal digits into String consisting of corresponding binary digits ('1's and '0's). No length limit. String position 0 will have most-significant bit, position length-1 has least-significant.
      Parameters:
      value - String containing '0', '1', ...'f' characters which form hexadecimal. Letters may be either upper or lower case. Works either with or without leading "Ox".
      Returns:
      String with equivalent value in binary.
    • intToHalfHexString

      public static String intToHalfHexString(int value)
      Returns a 6 character string representing the 16-bit hexadecimal equivalent of the given integer value. First two characters are "0x". It assumes value will "fit" in 16 bits. If non-negative, prepend leading zeroes to that string as necessary to make it always four hexadecimal digits. If negative, chop off the first four 'f' digits so result is always four hexadecimal digits
      Parameters:
      value - The int value to convert.
      Returns:
      String containing '0', '1', ...'f' which form hexadecimal equivalent of int.
    • intToHexString

      public static String intToHexString(int value)
      Prefix a hexadecimal-indicating string "0x" to the string which is returned by the method Integer.toHexString(int). Prepend leading zeroes to that string as necessary to make it always eight hexadecimal digits.
      Parameters:
      value - The int value to convert.
      Returns:
      String containing '0', '1', ...'f' which form hexadecimal equivalent of int.
    • longToHexString

      public static String longToHexString(long value)
      Prefix a hexadecimal-indicating string "0x" to the string equivalent to the hexadecimal value in the long parameter. Prepend leading zeroes to that string as necessary to make it always sixteen hexadecimal digits.
      Parameters:
      value - The long value to convert.
      Returns:
      String containing '0', '1', ...'F' which form hexadecimal equivalent of long.
    • intToAscii

      public static String intToAscii(int value)
      Produce ASCII string equivalent of integer value, interpreting it as 4 one-byte characters. If the value in a given byte does not correspond to a printable character, it will be assigned a default character (defined in config.properties) for a placeholder.
      Parameters:
      value - The int value to interpret
      Returns:
      String that represents ASCII equivalent
    • decodeInteger

      public static int decodeInteger(String string) throws NumberFormatException
      Convert the given string to a 32 bit integer, accounting for radix prefixes. Allows optional negative (-) sign, but no spaces. May be unsigned.
      Parameters:
      string - The string to decode.
      Returns:
      Integer value represented by given string.
      Throws:
      NumberFormatException - Thrown if string cannot be translated into an integer.
      See Also:
    • decodeLong

      public static long decodeLong(String string) throws NumberFormatException
      Convert the given string to a 64 bit integer, accounting for radix prefixes. Allows optional negative (-) sign, but no spaces. May be unsigned.
      Parameters:
      string - The string to decode.
      Returns:
      Integer value represented by given string.
      Throws:
      NumberFormatException - Thrown if string cannot be translated into an integer.
      See Also:
    • highOrderLongToInt

      public static int highOrderLongToInt(long longValue)
      Returns a 32-bit integer representing the high-order 32 bits of a given 64-bit integer.
      Parameters:
      longValue - The value from which to extract bits.
      Returns:
      The high-order 32 bits of the given value.
    • lowOrderLongToInt

      public static int lowOrderLongToInt(long longValue)
      Returns a 32-bit integer representing the low-order 32 bits of a given 64-bit integer.
      Parameters:
      longValue - The value from which to extract bits.
      Returns:
      The low-order 32 bits of the given value.
    • twoIntsToLong

      public static long twoIntsToLong(int highOrder, int lowOrder)
      Returns a 64-bit integer representing the concatenation of two given 32-bit integers.
      Parameters:
      highOrder - The high-order 32 bits of the result.
      lowOrder - The low-order 32 bits of the result.
      Returns:
      The 64-bit concatenation of the two arguments.
    • highOrderIntToShort

      public static short highOrderIntToShort(int intValue)
      Returns a 16-bit integer representing the high-order 16 bits of a given 32-bit integer.
      Parameters:
      intValue - The value from which to extract bits.
      Returns:
      The high-order 16 bits of the given value.
    • lowOrderIntToShort

      public static short lowOrderIntToShort(int intValue)
      Returns a 16-bit integer representing the low-order 16 bits of a given 32-bit integer.
      Parameters:
      intValue - The value from which to extract bits.
      Returns:
      The low-order 16 bits of the given value.
    • twoShortsToInt

      public static int twoShortsToInt(short highOrder, short lowOrder)
      Returns a 32-bit integer representing the concatenation of two given 16-bit integers.
      Parameters:
      highOrder - The high-order 16 bits of the result.
      lowOrder - The low-order 16 bits of the result.
      Returns:
      The 32-bit concatenation of the two arguments.
    • bitValue

      public static int bitValue(int value, int bit)
      Returns the bit value of the given bit position of the given int value.
      Parameters:
      value - The value to read the bit from.
      bit - bit position in range 0 (least significant) to 31 (most)
      Returns:
      0 if the bit position contains 0, and 1 otherwise.
    • bitValue

      public static int bitValue(long value, int bit)
      Returns the bit value of the given bit position of the given long value.
      Parameters:
      value - The value to read the bit from.
      bit - bit position in range 0 (least significant) to 63 (most)
      Returns:
      0 if the bit position contains 0, and 1 otherwise.
    • setBit

      public static int setBit(int value, int bit)
      Sets the specified bit of the specified value to 1, and returns the result.
      Parameters:
      value - The value in which the bit is to be set.
      bit - bit position in range 0 (least significant) to 31 (most)
      Returns:
      value possibly modified with given bit set to 1.
    • clearBit

      public static int clearBit(int value, int bit)
      Sets the specified bit of the specified value to 0, and returns the result.
      Parameters:
      value - The value in which the bit is to be set.
      bit - bit position in range 0 (least significant) to 31 (most)
      Returns:
      value possibly modified with given bit set to 0.
    • setByte

      public static int setByte(int value, int position, int replace)
      Sets the specified byte of the specified value to the low order 8 bits of specified replacement value, and returns the result.
      Parameters:
      value - The value in which the byte is to be set.
      position - Byte position in range 0 (least significant) to 3 (most).
      replace - Value to place into that byte position - use low order 8 bits.
      Returns:
      Modified value.
    • getByte

      public static int getByte(int value, int position)
      Gets the specified byte of the specified value.
      Parameters:
      value - The value in which the byte is to be retrieved.
      position - Byte position in range 0 (least significant) to 3 (most significant).
      Returns:
      Zero-extended byte value in low order byte.
    • isHex

      public static boolean isHex(String string)
      Parsing method to see if a string represents a hex number. As per Integer.decode(String), a string represents a hex number if the string is in the forms:
      • Signopt "0x" HexDigits
      • Signopt "0X" HexDigits
      • Signopt "#" HexDigits (not allowed here, as "#" starts a MIPS comment)
      Parameters:
      string - String containing numeric digits (could be decimal, octal, or hex)
      Returns:
      Returns true if string represents a hex number, else returns false.
    • isOctal

      public static boolean isOctal(String string)
      Parsing method to see if a string represents an octal number. As per Integer.decode(String), a string represents an octal number if the string is in the forms:
      • Signopt "0" OctalDigits
      Parameters:
      string - String containing numeric digits (could be decimal, octal, or hex)
      Returns:
      Returns true if string represents an octal number, else returns false.