Example usage for java.lang Integer toBinaryString

List of usage examples for java.lang Integer toBinaryString

Introduction

In this page you can find the example usage for java.lang Integer toBinaryString.

Prototype

public static String toBinaryString(int i) 

Source Link

Document

Returns a string representation of the integer argument as an unsigned integer in base 2.

Usage

From source file:CanvasSizeMIDlet.java

public void paint(Graphics g) {
    width = getWidth();/*from  w  w  w. j a va 2  s.  c  om*/
    height = getHeight();

    g.setGrayScale(255);
    g.fillRect(0, 0, width - 1, height - 1);
    g.setGrayScale(0);
    g.drawString("Stroke Style:" + g.getStrokeStyle(), 0, 60, Graphics.TOP | Graphics.LEFT);
    g.drawRect(0, 0, width - 1, height - 1);
    g.setStrokeStyle(Graphics.DOTTED);
    g.drawLine(00, 40, 60, 60);

    g.drawString(Integer.toBinaryString(width), 0, 0, Graphics.TOP | Graphics.LEFT);
    g.drawString(Integer.toBinaryString(height), 10, 20, Graphics.TOP | Graphics.LEFT);
}

From source file:com.vake.ArrayUtils.java

/**
 * ?radix<p/>/*from   ww w.j a v  a2s .c  o  m*/
 * bytesnull"null"bytes"[]"
 *
 * @param bytes                 
 * @param radix2?816
 * @return 
 */
public static String toString(byte[] bytes, int radix) {
    if (2 != radix && 8 != radix && 16 != radix) {
        final String msg = "radix must be 2, 8 or 16";
        throw new IllegalArgumentException(msg);
    }
    if (null == bytes) {
        return "null";
    }

    final StringBuilder builder = new StringBuilder("[");
    final int len = bytes.length;
    for (int i = 0; i < len; i++) {
        final int intValue = bytes[i] & 0xFF;
        final String string;
        final String padString;
        switch (radix) {
        case 2:
            string = Integer.toBinaryString(intValue);
            padString = StringUtils.leftPad(string, 8, '0');
            break;
        case 8:
            string = Integer.toOctalString(intValue);
            padString = StringUtils.leftPad(string, 3, '0');
            break;
        default:
            string = Integer.toHexString(intValue);
            padString = StringUtils.leftPad(string, 2, '0');
        }
        builder.append(padString.toUpperCase());
        if (i < len - 1) {
            builder.append(", ");
        }
    }
    builder.append("]");
    return builder.toString();
}

From source file:pl.dpbz.poid.zadanie4.utils.Fourier.java

public static Complex[] computeFastFourier(Complex[] sound, int bits) {
    double N = sound.length;

    Complex[] transformedSignal = new Complex[(int) N];

    for (int i = 0; i < transformedSignal.length; i++) {
        transformedSignal[i] = new Complex(0.0, 0.0);
    }/*w  w w .  ja v  a 2s.c o  m*/

    Complex signalTab[] = new Complex[(int) N];
    Complex[] localTR = new Complex[(int) N];
    int index = 0;
    for (int i = 0; i < sound.length; i++) {
        signalTab[index] = new Complex(sound[i].getReal(), sound[i].getImaginary());
        index++;
    }

    index = 0;
    for (Complex cv : signalTab) {
        //            System.out.println("x(" + index + ") = " + cv.getReal() + " IM: i" + cv.getImaginary());
        index++;
    }
    //Zmienna okrelajca na jakiej wielkoci ma operowa na tablicy
    int part = 2;
    //Ptla okrelajca cykl przechodzenia, przez kolejne kolumny
    for (int iteration = 1; iteration <= bits; iteration++) {
        //            System.out.println("PART "+part);
        //Ile razy ma si wykona
        for (int i = 0; i < part; i += 2) {

            int r = 0;
            for (int actualIndex = (signalTab.length / part) * i, counter = 0; counter < signalTab.length
                    / part; counter++, actualIndex++) {
                int secondIndex = (actualIndex + (signalTab.length / part));
                Complex a = signalTab[actualIndex].add(signalTab[secondIndex]);
                Complex b = signalTab[actualIndex].subtract(signalTab[secondIndex]);
                Complex W = new Complex(Math.cos((2.0 * Math.PI * r) / N), -Math.sin((2.0 * Math.PI * r) / N));
                b = b.multiply(W);
                signalTab[actualIndex] = a;
                signalTab[secondIndex] = b;
                r += part - (part / 2);
            }
        }
        part += part;
    }

    localTR[0] = signalTab[0];
    localTR[localTR.length - 1] = signalTab[signalTab.length - 1];
    for (int i = 1; i < signalTab.length - 1; i++) {
        String bitIndex = Integer.toBinaryString(i);
        if (bitIndex.length() < bits) {
            while (bitIndex.length() < bits) {
                bitIndex = "0" + bitIndex;
            }
        }
        char[] tab = bitIndex.toCharArray();

        for (int j = 0; j < tab.length / 2; j++) {
            char temp = tab[j];
            tab[j] = tab[tab.length - j - 1];
            tab[tab.length - j - 1] = temp;
        }
        bitIndex = new String(tab);
        localTR[Integer.parseInt(bitIndex, 2)] = signalTab[i];
    }
    for (int i = 0; i < localTR.length; i++) {
        transformedSignal[i] = new Complex(localTR[i].getReal(), localTR[i].getImaginary());
    }

    return transformedSignal;
}

From source file:de.uniwue.info2.numerics.prec.SinglePrecisionFloat.java

@Override
protected String floatValueToBinaryString(String value) {
    float f = Float.parseFloat(value);
    String binary;/*from  www . ja  v a  2  s .  c  o m*/
    if (f == 0) {
        binary = StringUtils.repeat("0", 32);
    } else {
        binary = Integer.toBinaryString(Float.floatToIntBits(f));
    }
    return binary;
}

From source file:com.codenjoy.dojo.tetris.model.TetrisFigure.java

private void parseRows(String... rows) {
    this.rows = rows;
    codes = new int[rows.length];
    uncoloredCodes = new int[rows.length];
    for (int i = 0; i < rows.length; i++) {
        String row = rows[i];//from w w w. java  2s  .co  m
        String colorCode = Integer.toBinaryString(type.getColor().ordinal() + 1);
        String paddedCode = StringUtils.leftPad(colorCode, 3, '0');
        codes[i] = Integer.parseInt(row.replace("#", paddedCode).replace(" ", "000"), 2);
        uncoloredCodes[i] = Integer.parseInt(row.replace("#", "111").replace(" ", "000"), 2);
    }
}

From source file:org.mabb.fontverter.woff.WoffOutputStream.java

public void writeFlagByte(int flag, int transform) throws IOException {
    String binary = Integer.toBinaryString(flag);
    String transBinary = Integer.toBinaryString(transform);
    if (transBinary.length() < 2)
        transBinary = StringUtils.repeat("0", 2 - transBinary.length()) + transBinary;
    if (binary.length() < 6)
        binary = StringUtils.repeat("0", 6 - binary.length()) + binary;

    binary = transBinary + binary;/* w  ww. j a va 2s.co m*/
    byte byteOn = (Byte.parseByte(binary, 2));
    write(byteOn);
}

From source file:ricecompression.RiceCompression.java

public String compress(int m, int n) {
    String riceCode;//from   w  ww  . j  a v  a 2s . c  o  m
    int nBitsM = (int) (Math.log10(m) / Math.log10(2));
    if (n < 0)
        riceCode = "0"; //Valor negatiu
    else
        riceCode = "1"; //Valor negatiu
    int q = Math.abs(n) / m;
    char[] array = new char[q];
    Arrays.fill(array, '1');
    if (array.length > 0)
        riceCode = riceCode.concat(String.valueOf(array)); //Si el quocient es major a 0
    riceCode = riceCode.concat("0");
    int r = Math.abs(n) % m;
    String rBinary = String.format("%" + nBitsM + "s", Integer.toBinaryString(r)).replace(' ', '0');
    riceCode = riceCode.concat(rBinary);
    return riceCode;
}

From source file:de.thischwa.pmcms.view.context.object.GalleryTool.java

/**
 * Prepare a {@link List} of {@link Image images} used to construct the thumbnail page of a {@link Gallery}. The
 * list contains a list per row. All images are in the requested order.
 *//*from   w  w  w. j  a  v a  2s . c o  m*/
public List<List<Image>> getThumbnailMatrix(final Gallery gallery, final int maxCol) {
    if (gallery == null || CollectionUtils.isEmpty(gallery.getImages()))
        return new ArrayList<List<Image>>();
    int rows = Math.round(((float) gallery.getImages().size() / (float) maxCol) + 0.5f);
    logger.info("Dimension of the thumbnail matrix: ".concat(Integer.toString(maxCol)).concat(" x ")
            .concat(Integer.toBinaryString(rows)));

    List<List<Image>> matrix = new ArrayList<List<Image>>(rows);
    List<Image> matrixCol = new ArrayList<Image>(maxCol);
    int colIndex = 0;
    for (Image image : gallery.getImages()) {
        colIndex++;
        if (colIndex > maxCol) {
            matrix.add(matrixCol);
            matrixCol = new ArrayList<Image>(maxCol);
            colIndex = 1;
        }
        matrixCol.add(image);
    }
    if (!CollectionUtils.isEmpty(matrixCol))
        matrix.add(matrixCol);

    return matrix;
}

From source file:org.finra.herd.dao.StorageFileDaoTestHelper.java

/**
 * Creates and persists storage file entities per specified parameters.
 *
 * @param storageUnitEntity the storage unit entity
 * @param s3KeyPrefix the S3 key prefix//from  w ww . j  ava  2  s. c om
 * @param partitionColumns the list of partition columns
 * @param subPartitionValues the list of sub-partition values
 * @param replaceUnderscoresWithHyphens specifies to replace UnderscoresWithHyphens when buidling a path
 */
public void createStorageFileEntities(StorageUnitEntity storageUnitEntity, String s3KeyPrefix,
        List<SchemaColumn> partitionColumns, List<String> subPartitionValues,
        boolean replaceUnderscoresWithHyphens) {
    int discoverableSubPartitionsCount = partitionColumns != null
            ? partitionColumns.size() - subPartitionValues.size() - 1
            : 0;
    int storageFilesCount = (int) Math.pow(2, discoverableSubPartitionsCount);

    for (int i = 0; i < storageFilesCount; i++) {
        // Build a relative sub-directory path.
        StringBuilder subDirectory = new StringBuilder();
        String binaryString = StringUtils.leftPad(Integer.toBinaryString(i), discoverableSubPartitionsCount,
                "0");
        for (int j = 0; j < discoverableSubPartitionsCount; j++) {
            String subpartitionKey = partitionColumns.get(j + subPartitionValues.size() + 1).getName()
                    .toLowerCase();
            if (replaceUnderscoresWithHyphens) {
                subpartitionKey = subpartitionKey.replace("_", "-");
            }
            subDirectory.append(String.format("/%s=%s", subpartitionKey, binaryString.substring(j, j + 1)));
        }
        // Create a storage file entity.
        createStorageFileEntity(storageUnitEntity,
                String.format("%s%s/data.dat", s3KeyPrefix, subDirectory.toString()),
                AbstractDaoTest.FILE_SIZE_1_KB, AbstractDaoTest.ROW_COUNT_1000);
    }
}

From source file:org.terrier.realtime.compression.MemBitSetBuffer.java

/**
 * Gamma encoding.// www  .j  a v a 2  s  .co  m
 * Take n in binary.
 * Pad left nbits-1 0s.
 */
public int writeGamma(int n) {
    String binary = Integer.toBinaryString(n);
    String gamma = StringUtils.repeat("0", binary.length() - 1) + binary;
    for (int bit = 0; bit < gamma.length(); bit++)
        buffer.set(pointer++, gamma.charAt(bit) == '0' ? false : true);
    return gamma.length();
}