Java Binary Encode toBinary(int val)

Here you can find the source of toBinary(int val)

Description

to Binary

License

Open Source License

Declaration

public static String toBinary(int val) 

Method Source Code

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

public class Main {
    public static String toBinary(int val) {
        StringBuilder builder = new StringBuilder();
        builder.append("0x").append(Integer.toHexString(val)).append(" = ");
        for (int i = 31; i >= 0; i--) {
            String digit = ((1 << i) & val) == 0 ? "0" : "1";
            builder.append(digit).append((i % 4 == 0) ? " " : "");
        }/*w w w  .  j  a va2 s  . c  o  m*/
        return builder.toString();
    }
}

Related

  1. toBinary(final double d)
  2. toBinary(final double d)
  3. toBinary(final Object o)
  4. toBinary(int nr, int bits)
  5. toBinary(int number, int length)
  6. toBinary(int value, int bits)
  7. toBinary(long l, int bits)
  8. toBinary(long v, int len)
  9. toBinary(short value)