Java Integer to Bit convertIntToBitsWithPadding(int num)

Here you can find the source of convertIntToBitsWithPadding(int num)

Description

convert Int To Bits With Padding

License

Open Source License

Declaration

public static String convertIntToBitsWithPadding(int num) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static int INT_BIT_COUNT = 1 << 5;

    public static String convertIntToBitsWithPadding(int num) {
        StringBuilder builder = new StringBuilder(convertIntToBits(num));
        int length = INT_BIT_COUNT - builder.length();
        for (int i = 0; i < length; i++)
            builder.insert(0, '0');

        return builder.toString();
    }//ww w  . j ava  2 s.  c  o  m

    public static String convertIntToBits(int num) {
        StringBuilder builder = new StringBuilder();
        while (num != 0) {
            if ((num & 0x1) == 1)
                builder.insert(0, '1');
            else
                builder.insert(0, '0');
            num >>>= 1;
        }
        return builder.toString();
    }
}

Related

  1. convertIntToBits(int num)
  2. intToBits(int in, int min, int max, int numBits)
  3. intToBits(int n, int size)
  4. intToBits(int no)
  5. intToBits(int pInt, boolean[] pBits)