Here you can find the source of toBinaryAddress(int index, int maxIndex)
index
as a binary string.
Parameter | Description |
---|---|
index | a parameter |
maxIndex | a parameter |
public static String toBinaryAddress(int index, int maxIndex)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from w w w. j av a 2 s .co m*/ * Returns <code>index</code> as a binary string. <br> * The string is padded with zeros to the left such that its length would equal the minimal * length of the binary string representing <code>maxIndex</code>. <br> * Both integers are interpreted as unsigned values, therefore negative <code>int</code> values * will always assume a width of 32 characters. * * @param index * @param maxIndex * @return */ public static String toBinaryAddress(int index, int maxIndex) { int digits = Math.max(32 - Integer.numberOfLeadingZeros(maxIndex), 1); char[] buffer = new char[digits]; int mask = 1; for (int i = digits - 1; i >= 0; i--) { buffer[i] = (index & mask) != 0 ? '1' : '0'; mask <<= 1; } return new String(buffer); } }