Class BitUtil

java.lang.Object
fc.util.BitUtil

public class BitUtil extends Object
Misc Binary/bit operation related utility methods. All bit positions are 0-based. For example, to set a 8th logical bit (highest bit in a byte), say setBit(byte, 7).

Of course, since most methods take primitive types, the return value must be assigned back to the variable that is being set, i.e. int a = 0; a = BitSet.setBit(a, 1);

  • Constructor Details

  • Method Details

    • isBitSet

      public static boolean isBitSet(byte i, int num)
      All indexes are zero based (the right most bit is bit 0). returns true is specified bit in the specified byte is set. The num parameter is the bit position (0-based index). Set means the bit has value 1.
    • isBitSet

      public static boolean isBitSet(short i, int num)
    • isBitSet

      public static boolean isBitSet(char i, int num)
    • isBitSet

      public static boolean isBitSet(int i, int num)
    • isBitSet

      public static boolean isBitSet(long i, int num)
    • setBit

      public static byte setBit(byte i, int num)
    • setBit

      public static short setBit(short i, int num)
    • setBit

      public static char setBit(char i, int num)
    • setBit

      public static int setBit(int i, int num)
    • setBit

      public static long setBit(long i, long num)
    • clearBit

      public static byte clearBit(byte i, int num)
    • clearBit

      public static short clearBit(short i, int num)
    • clearBit

      public static char clearBit(char i, int num)
    • clearBit

      public static int clearBit(int i, int num)
    • clearBit

      public static long clearBit(long i, int num)
    • toBinaryString

      public static String toBinaryString(byte i)
    • toBinaryString

      public static String toBinaryString(short i)
      returns a 16 char binary digit string
    • toBinaryString

      public static String toBinaryString(char i)
      returns a 16 char binary digit string
    • toBinaryString

      public static String toBinaryString(int i)
      returns a 32 char binary digit string (Integer.toBinaryString is not padded, can be less than 32 chars)
    • toBinaryString

      public static String toBinaryString(long i)
      returns a 64 char binary digit string (Long.toBinaryString is not padded, can be less than 64 chars))
    • toBinaryString

      public static String toBinaryString(byte i, String format)
      returns a 8 char binary digit string.

      The format string has a number and separator, like "4 " which will print chunks of size with the specified . The number must be between [1-9] and separator must be a single character, like: | or space. Errors in the format string are ignored and default values are used instead (4 and space).

    • toBinaryString

      public static String toBinaryString(short i, String format)
      returns a 16 char binary digit string.

      The format string has a number and separator, like "4 " which will print chunks of size with the specified . The number must be between [1-9] and separator must be a single character, like: | or space. Errors in the format string are ignored and default values are used instead (4 and space).

    • toBinaryString

      public static String toBinaryString(char i, String format)
      returns a 16 char binary digit string.

      The format string has a number and separator, like "4 " which will print chunks of size num with the specified separator.

    • toBinaryString

      public static String toBinaryString(int i, String format)
      returns a 32char binary digit string (Long.toBinaryString is not padded, can be less than 32 chars).

      The format string has a number and separator, like "4 " which will print chunks of size with the specified . The number must be between [1-9] and separator must be a single character, like: | or space. Errors in the format string are ignored and default values are used instead (4 and space).

    • toBinaryString

      public static String toBinaryString(long i, String format)
      returns a 64 char binary digit string (Long.toBinaryString is not padded, can be less than 64 chars).

      The format string has a number and separator, like "4 " which will print chunks of size with the specified . The number must be between [1-9] and separator must be a single character, like: | or space. Errors in the format string are ignored and default values are used instead (4 and space).

    • main

      public static void main(String[] args)