Java Integer Convert To fromInt(final int value, final int length)

Here you can find the source of fromInt(final int value, final int length)

Description

from Int

License

Open Source License

Declaration

public static boolean[] fromInt(final int value, final int length) 

Method Source Code

//package com.java2s;

public class Main {
    public static boolean[] fromInt(final int value, final int length) {
        boolean[] result = new boolean[length];
        final boolean isNegative = (value < 0);

        int divResult = isNegative ? -value : value;
        int index = length - 1;
        // Calc value
        while (divResult > 0) {
            final int remains = divResult % 2;
            result[index--] = (remains == 1);

            divResult /= 2;//from www .  ja v a2s.c  o  m
        }
        if (isNegative) {
            result = invert(result);
            result = inc(result);
        }

        return result;
    }

    private static boolean[] invert(boolean[] result) {
        for (int i = 0; i < result.length; i++)
            result[i] = !result[i];
        return result;
    }

    public static boolean[] inc(boolean[] input) {
        boolean[] result = new boolean[input.length];
        // boolean flag = input[input.length-1];
        boolean add = true;
        for (int i = input.length - 1; i >= 0; i--) {
            result[i] = input[i] ^ add; // XOR
            add = input[i] && add;

        }
        return result;
    }
}

Related

  1. convertIntToLong(Integer intId)
  2. convertInttoMultiByte(int val)
  3. convertIntToTwoChar(int n)
  4. convertIntToUsignedBytes(int integer)
  5. fromInt(byte[] buffer, int pos, int i)
  6. fromInt(int i)
  7. fromint(int i)
  8. fromInt(int input)
  9. fromInt(int input)