Class Bit


  • public final class Bit
    extends Object
    The bit is a basic unit of information in information theory, computing. This package includes bit twiddling hacks by Sean Eron Anderson and many others.
    📦 Central, 📜 Releases, 😺 GitHub, 🐸 Bintray, 🦚 MvnRepository, 📰 Javadoc, 📘 Wiki.
    • Constructor Summary

      Constructors 
      Constructor Description
      Bit()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int count​(int x)
      Count bits set.
      static int count​(long x)
      Count bits set.
      static int get​(int x, int i)
      Get a bit.
      static int get​(long x, int i)
      Get a bit.
      static int getAs​(int x, int m)
      Get bits as per mask.
      static long getAs​(long x, long m)
      Get bits as per mask.
      static int interleave​(int x, int y)
      Interleave bits of two shorts.
      static long interleave​(long x, long y)
      Interleave bits of two ints.
      static int merge​(int x, int y, int m)
      Merge bits as per mask.
      static long merge​(long x, long y, long m)
      Merge bits as per mask.
      static int parity​(int x)
      Get 1-bit parity.
      static int parity​(int x, int n)
      Get n-bit parity.
      static int parity​(long x)
      Get 1-bit parity.
      static int parity​(long x, int n)
      Get n-bit parity.
      static int reverse​(int x)
      Reverse all bits.
      static long reverse​(long x)
      Reverse all bits.
      static int rotate​(int x, int n)
      Rotate bits.
      static long rotate​(long x, int n)
      Rotate bits.
      static int scan​(int x)
      Get index of first set bit from LSB.
      static int scan​(long x)
      Get index of first set bit from LSB.
      static int scanReverse​(int x)
      Get index of first set bit from MSB.
      static int scanReverse​(long x)
      Get index of first set bit from LSB.
      static int set​(int x, int i, int f)
      Set a bit.
      static long set​(long x, int i, int f)
      Set a bit.
      static int setAs​(int x, int m, int f)
      Set bits as per mask.
      static long setAs​(long x, long m, int f)
      Set bits as per mask.
      static int signExtend​(int x, int w)
      Sign extend variable bit-width integer.
      static long signExtend​(long x, int w)
      Sign extend variable bit-width integer.
      static int swap​(int x, int i, int j)
      Swap bits.
      static int swap​(int x, int i, int j, int n)
      Swap bit sequences.
      static long swap​(long x, int i, int j)
      Swap bits.
      static long swap​(long x, int i, int j, int n)
      Swap bit sequences.
      static int toggle​(int x, int i)
      Toggle a bit.
      static long toggle​(long x, int i)
      Toggle a bit.
      static int toggleAs​(int x, int m)
      Toggle bits as per mask.
      static long toggleAs​(long x, long m)
      Toggle bits as per mask.
    • Constructor Detail

      • Bit

        public Bit()
    • Method Detail

      • get

        public static int get​(int x,
                              int i)
        Get a bit. 📘
        Parameters:
        x - an int
        i - bit index
        Returns:
        bit
        
        get(6, 0) == 0  // 110,0 ⇒ 0
        get(6, 1) == 1  // 110,1 ⇒ 1
        get(6, 2) == 1  // 110,2 ⇒ 1
        
      • get

        public static int get​(long x,
                              int i)
        Get a bit. 📘
        Parameters:
        x - a long
        i - bit index
        Returns:
        bit
        
        get(6, 0) == 0  // 110,0 ⇒ 0
        get(6, 1) == 1  // 110,1 ⇒ 1
        get(6, 2) == 1  // 110,2 ⇒ 1
        
      • getAs

        public static int getAs​(int x,
                                int m)
        Get bits as per mask. 📘
        Parameters:
        x - an int
        m - bit mask
        Returns:
        bits
        
        getAs(6, 4) == 4  // 110,100 ⇒ 100
        getAs(6, 7) == 6  // 110,111 ⇒ 110
        getAs(6, 5) == 4  // 110,101 ⇒ 100
        
      • getAs

        public static long getAs​(long x,
                                 long m)
        Get bits as per mask. 📘
        Parameters:
        x - a long
        m - bit mask
        Returns:
        bits
        
        getAs(6, 4) == 4  // 110,100 ⇒ 100
        getAs(6, 7) == 6  // 110,111 ⇒ 110
        getAs(6, 5) == 4  // 110,101 ⇒ 100
        
      • set

        public static int set​(int x,
                              int i,
                              int f)
        Set a bit. 📘
        Parameters:
        x - an int
        i - bit index
        f - bit value (1)
        Returns:
        set int
        
        set(6, 0, 1) == 7  // 110,0,1 ⇒ 111
        set(6, 2, 1) == 6  // 110,2,1 ⇒ 110
        set(6, 2, 0) == 2  // 110,2,0 ⇒ 010
        
      • set

        public static long set​(long x,
                               int i,
                               int f)
        Set a bit. 📘
        Parameters:
        x - a long
        i - bit index
        f - bit value (1)
        Returns:
        set long
        
        set(6, 0, 1) == 7  // 110,0,1 ⇒ 111
        set(6, 2, 1) == 6  // 110,2,1 ⇒ 110
        set(6, 2, 0) == 2  // 110,2,0 ⇒ 010
        
      • setAs

        public static int setAs​(int x,
                                int m,
                                int f)
        Set bits as per mask. 📘
        Parameters:
        x - an int
        m - bit mask
        f - bit value (1)
        Returns:
        set int
        
        setAs(8, 2, 1)          == 10    // 0x8 set 0x2      ⇒ 0xA
        setAs(15, 3, 0)         == 12    // 0xF clear 0x3    ⇒ 0xC
        setAs(0x1234, 0x430, 1) == 5684  // 0x1234 set 0x430 ⇒ 0x1634
        
      • setAs

        public static long setAs​(long x,
                                 long m,
                                 int f)
        Set bits as per mask. 📘
        Parameters:
        x - a long
        m - bit mask
        f - bit value (1)
        Returns:
        set long
        
        setAs(8, 2, 1)          == 10    // 0x8 set 0x2      ⇒ 0xA
        setAs(15, 3, 0)         == 12    // 0xF clear 0x3    ⇒ 0xC
        setAs(0x1234, 0x430, 1) == 5684  // 0x1234 set 0x430 ⇒ 0x1634
        
      • toggle

        public static int toggle​(int x,
                                 int i)
        Toggle a bit. 📘
        Parameters:
        x - an int
        i - bit index
        Returns:
        toggled int
        
        toggle(6, 0) == 7  // 110,0 ⇒ 111
        toggle(6, 1) == 4  // 110,1 ⇒ 100
        toggle(6, 2) == 2  // 110,2 ⇒ 010
        
      • toggle

        public static long toggle​(long x,
                                  int i)
        Toggle a bit. 📘
        Parameters:
        x - a long
        i - bit index
        Returns:
        toggled long
        
        toggle(6, 0) == 7  // 110,0 ⇒ 111
        toggle(6, 1) == 4  // 110,1 ⇒ 100
        toggle(6, 2) == 2  // 110,2 ⇒ 010
        
      • toggleAs

        public static int toggleAs​(int x,
                                   int m)
        Toggle bits as per mask. 📘
        Parameters:
        x - an int
        m - bit mask
        Returns:
        toggled int
        
        toggleAs(6, 1) == 7  // 110,000 ⇒ 111
        toggleAs(6, 7) == 1  // 110,111 ⇒ 001
        toggleAs(6, 3) == 5  // 110,011 ⇒ 101
        
      • toggleAs

        public static long toggleAs​(long x,
                                    long m)
        Toggle bits as per mask. 📘
        Parameters:
        x - a long
        m - bit mask
        Returns:
        toggled long
        
        toggleAs(6, 1) == 7  // 110,000 ⇒ 111
        toggleAs(6, 7) == 1  // 110,111 ⇒ 001
        toggleAs(6, 3) == 5  // 110,011 ⇒ 101
        
      • swap

        public static int swap​(int x,
                               int i,
                               int j)
        Swap bits. 📘
        Parameters:
        x - an int
        i - first bit index
        j - second bit index
        Returns:
        swapped int
        
        swap(6, 1, 0, 1)      == 5      // 110    ⇒ 101
        swap(0x1234, 8, 4, 4) == 4900   // 0x1234 ⇒ 0x1324
        swap(0x4AAB, 8, 0, 8) == 43850  // 0x4AAB ⇒ 0xAB4A
        
      • swap

        public static long swap​(long x,
                                int i,
                                int j)
        Swap bits. 📘
        Parameters:
        x - a long
        i - first bit index
        j - second bit index
        Returns:
        swapped long
        
        swap(6, 1, 0, 1)      == 5      // 110    ⇒ 101
        swap(0x1234, 8, 4, 4) == 4900   // 0x1234 ⇒ 0x1324
        swap(0x4AAB, 8, 0, 8) == 43850  // 0x4AAB ⇒ 0xAB4A
        
      • swap

        public static int swap​(int x,
                               int i,
                               int j,
                               int n)
        Swap bit sequences. 📘
        Parameters:
        x - an int
        i - first bit index
        j - second bit index
        n - bit width (1)
        Returns:
        swapped int
        
        swap(6, 1, 0, 1)      == 5      // 110    ⇒ 101
        swap(0x1234, 8, 4, 4) == 4900   // 0x1234 ⇒ 0x1324
        swap(0x4AAB, 8, 0, 8) == 43850  // 0x4AAB ⇒ 0xAB4A
        
      • swap

        public static long swap​(long x,
                                int i,
                                int j,
                                int n)
        Swap bit sequences. 📘
        Parameters:
        x - a long
        i - first bit index
        j - second bit index
        n - bit width (1)
        Returns:
        swapped long
        
        swap(6, 1, 0, 1)      == 5      // 110    ⇒ 101
        swap(0x1234, 8, 4, 4) == 4900   // 0x1234 ⇒ 0x1324
        swap(0x4AAB, 8, 0, 8) == 43850  // 0x4AAB ⇒ 0xAB4A
        
      • count

        public static int count​(int x)
        Count bits set. 📘
        Parameters:
        x - an int
        Returns:
        count
        
        count(7)  == 3  // 111    ⇒ 3
        count(12) == 2  // 1100   ⇒ 2
        count(63) == 6  // 111111 ⇒ 6
        
      • count

        public static int count​(long x)
        Count bits set. 📘
        Parameters:
        x - a long
        Returns:
        count
        
        count(7)  == 3  // 111    ⇒ 3
        count(12) == 2  // 1100   ⇒ 2
        count(63) == 6  // 111111 ⇒ 6
        
      • parity

        public static int parity​(int x)
        Get 1-bit parity. 📘
        Parameters:
        x - an int
        Returns:
        parity
        
        parity(7, 1)  == 1   // 1,1,1   ⇒ 1
        parity(5, 1)  == 0   // 1,0,1   ⇒ 0
        parity(8, 2)  == 2   // 10,00   ⇒ 10
        parity(63, 4) == 12  // 11,1111 ⇒ 1100
        
      • parity

        public static int parity​(long x)
        Get 1-bit parity. 📘
        Parameters:
        x - a long
        Returns:
        parity
        
        parity(7, 1)  == 1   // 1,1,1   ⇒ 1
        parity(5, 1)  == 0   // 1,0,1   ⇒ 0
        parity(8, 2)  == 2   // 10,00   ⇒ 10
        parity(63, 4) == 12  // 11,1111 ⇒ 1100
        
      • parity

        public static int parity​(int x,
                                 int n)
        Get n-bit parity. 📘
        Parameters:
        x - an int
        n - number of bits (1)
        Returns:
        parity
        
        parity(7, 1)  == 1   // 1,1,1   ⇒ 1
        parity(5, 1)  == 0   // 1,0,1   ⇒ 0
        parity(8, 2)  == 2   // 10,00   ⇒ 10
        parity(63, 4) == 12  // 11,1111 ⇒ 1100
        
      • parity

        public static int parity​(long x,
                                 int n)
        Get n-bit parity. 📘
        Parameters:
        x - an int
        n - number of bits (1)
        Returns:
        parity
        
        parity(7, 1)  == 1   // 1,1,1   ⇒ 1
        parity(5, 1)  == 0   // 1,0,1   ⇒ 0
        parity(8, 2)  == 2   // 10,00   ⇒ 10
        parity(63, 4) == 12  // 11,1111 ⇒ 1100
        
      • scan

        public static int scan​(int x)
        Get index of first set bit from LSB. 📘
        Parameters:
        x - an int
        Returns:
        bit index
        
        scan(7)  == 0  // 111     ⇒ 0
        scan(12) == 2  // 1100    ⇒ 2
        scan(64) == 6  // 1000000 ⇒ 6
        
      • scan

        public static int scan​(long x)
        Get index of first set bit from LSB. 📘
        Parameters:
        x - a long
        Returns:
        bit index
        
        scan(7)  == 0  // 111     ⇒ 0
        scan(12) == 2  // 1100    ⇒ 2
        scan(64) == 6  // 1000000 ⇒ 6
        
      • scanReverse

        public static int scanReverse​(int x)
        Get index of first set bit from MSB. 📘
        Parameters:
        x - an int32
        Returns:
        bit index
        
        scanReverse(13) == 3  // 1101 ⇒ 3
        scanReverse(5)  == 2  // 101  ⇒ 2
        scanReverse(1)  == 0  // 1    ⇒ 0
        
      • scanReverse

        public static int scanReverse​(long x)
        Get index of first set bit from LSB. 📘
        Parameters:
        x - a long
        Returns:
        bit index
        
        scanReverse(13) == 3  // 1101 ⇒ 3
        scanReverse(5)  == 2  // 101  ⇒ 2
        scanReverse(1)  == 0  // 1    ⇒ 0
        
      • merge

        public static int merge​(int x,
                                int y,
                                int m)
        Merge bits as per mask. 📘
        Parameters:
        x - first int
        y - second int
        m - bit mask (0 ⇒ from x)
        Returns:
        merged int
        
        merge(0x12, 0x24, 0x0F)       == 20     // 0x14
        merge(0x1234, 0xABCD, 0x0F0F) == 6973   // 0x1B3D
        merge(0xAAAA, 0xBBBB, 0x3333) == 48059  // 0xBBBB
        
      • merge

        public static long merge​(long x,
                                 long y,
                                 long m)
        Merge bits as per mask. 📘
        Parameters:
        x - first long
        y - second long
        m - bit mask (0 ⇒ from x)
        Returns:
        merged long
        
        merge(0x12, 0x24, 0x0F)       == 20     // 0x14
        merge(0x1234, 0xABCD, 0x0F0F) == 6973   // 0x1B3D
        merge(0xAAAA, 0xBBBB, 0x3333) == 48059  // 0xBBBB
        
      • interleave

        public static int interleave​(int x,
                                     int y)
        Interleave bits of two shorts. 📘
        Parameters:
        x - first short
        y - second short
        Returns:
        int
        
        interleave(0x0000, 0xFFFF) == 1431655765  // 0x55555555
        interleave(0x1234, 0x1234) == 51121968    // 0x030C0F30
        interleave(0x1234, 0x4321) == 302845473   // 0x120D0E21
        
      • interleave

        public static long interleave​(long x,
                                      long y)
        Interleave bits of two ints. 📘
        Parameters:
        x - first int
        y - second int
        Returns:
        interleaved long
        
        interleave(0x0000, 0xFFFF) == 1431655765  // 0x55555555
        interleave(0x1234, 0x1234) == 51121968    // 0x030C0F30
        interleave(0x1234, 0x4321) == 302845473   // 0x120D0E21
        
      • rotate

        public static int rotate​(int x,
                                 int n)
        Rotate bits. 📘
        Parameters:
        x - an int
        n - rotate amount (+ve: left, -ve: right)
        Returns:
        rotated int
        
        rotate(0x11112222, 4)  == 286401057  // 0x11122221
        rotate(0x11112222, -4) == 554766882  // 0x21111222
        
      • rotate

        public static long rotate​(long x,
                                  int n)
        Rotate bits. 📘
        Parameters:
        x - a long
        n - rotate amount (+ve: left, -ve: right)
        Returns:
        rotated long
        
        rotate(0x11112222, 4)  == 286401057  // 0x11122221
        rotate(0x11112222, -4) == 554766882  // 0x21111222
        
      • reverse

        public static int reverse​(int x)
        Reverse all bits. 📘
        Parameters:
        x - an int
        Returns:
        reversed int
        
        reverse(0xFFFF0000) == 65535      // 0x0000FFFF
        reverse(0x00AABBCC) == 870143232  // 0x33DD5500
        reverse(0x1234)     == 742916096  // 0x2C480000
        
      • reverse

        public static long reverse​(long x)
        Reverse all bits. 📘
        Parameters:
        x - a long
        Returns:
        reversed long
        
        reverse(0xFFFF0000) == 65535      // 0x0000FFFF
        reverse(0x00AABBCC) == 870143232  // 0x33DD5500
        reverse(0x1234)     == 742916096  // 0x2C480000
        
      • signExtend

        public static int signExtend​(int x,
                                     int w)
        Sign extend variable bit-width integer. 📘
        Parameters:
        x - variable bit-width int
        w - bit width (32)
        Returns:
        sign-extended int
        
        signExtend(15, 4) == -1  // 1111 ⇒ -1
        signExtend(3, 3)  == 3   // 011  ⇒ 3
        signExtend(4, 3)  == -4  // 100  ⇒ -4
        
      • signExtend

        public static long signExtend​(long x,
                                      int w)
        Sign extend variable bit-width integer. 📘
        Parameters:
        x - variable bit-width long
        w - bit width (64)
        Returns:
        sign-extended long
        
        signExtend(15, 4) == -1  // 1111 ⇒ -1
        signExtend(3, 3)  == 3   // 011  ⇒ 3
        signExtend(4, 3)  == -4  // 100  ⇒ -4