List of usage examples for java.lang Integer numberOfTrailingZeros
@HotSpotIntrinsicCandidate public static int numberOfTrailingZeros(int i)
From source file:Main.java
public static void main(String args[]) { int n = 170; // 10101010 System.out.println("Value in binary: 10101010"); System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(n)); }
From source file:MainClass.java
public static void main(String args[]) { int n = 170;//ww w. ja v a2 s . c o m System.out.println("Lowest one bit: " + Integer.lowestOneBit(n)); System.out.println("Number of leading zeros : " + Integer.numberOfLeadingZeros(n)); System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(n)); System.out.println("\nBeginning with the value 1, " + "rotate left 16 times."); }
From source file:Bits.java
public static void main(String args[]) { int n = 170; // 10101010 System.out.println("Value in binary: 10101010"); System.out.println("Number of one bits: " + Integer.bitCount(n)); System.out.println("Highest one bit: " + Integer.highestOneBit(n)); System.out.println("Lowest one bit: " + Integer.lowestOneBit(n)); System.out.println("Number of leading zeros : " + Integer.numberOfLeadingZeros(n)); System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(n)); System.out.println("\nBeginning with the value 1, " + "rotate left 16 times."); n = 1;//from w ww . j a v a 2s . c o m for (int i = 0; i < 16; i++) { n = Integer.rotateLeft(n, 1); System.out.println(n); } }
From source file:MainClass.java
public static void main(String args[]) throws IOException { int n = 170; // 10101010 System.out.println("Value in binary: 10101010"); System.out.println("Number of one bits: " + Integer.bitCount(n)); System.out.println("Highest one bit: " + Integer.highestOneBit(n)); System.out.println("Lowest one bit: " + Integer.lowestOneBit(n)); System.out.println("Number of leading zeros : " + Integer.numberOfLeadingZeros(n)); System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(n)); System.out.println("\nBeginning with the value 1, " + "rotate left 16 times."); n = 1;//from w w w.j a v a2s .co m for (int i = 0; i < 16; i++) { n = Integer.rotateLeft(n, 1); System.out.println(n); } }
From source file:org.kontalk.util.EncodingUtils.java
/** * Get an enum set by parsing an integer which represents a bit array. * Source: http://stackoverflow.com/questions/2199399/storing-enumset-in-a-database * @param <T> type of elements in enum set * @param enumClass enum class to determine the type * @param decoded integer decoded as/* ww w. ja va 2s . c o m*/ * @return an enum set containing the enums specified by the integer */ public static <T extends Enum<T>> EnumSet<T> intToEnumSet(Class<T> enumClass, int decoded) { EnumSet<T> enumSet = EnumSet.noneOf(enumClass); T[] enums = enumClass.getEnumConstants(); while (decoded != 0) { int ordinal = Integer.numberOfTrailingZeros(decoded); enumSet.add(enums[ordinal]); decoded -= Integer.lowestOneBit(decoded); } return enumSet; }
From source file:org.apache.shindig.common.util.StringEncoding.java
/** Creates a new encoding based on the supplied set of digits. */ public StringEncoding(final char[] userDigits) { TreeSet<Character> t = Sets.newTreeSet(); for (char c : userDigits) { t.add(c);/* w w w. j a v a 2 s .c o m*/ } char[] digits = new char[t.size()]; int i = 0; for (char c : t) { digits[i++] = c; } this.DIGITS = digits; this.MASK = digits.length - 1; this.SHIFT = Integer.numberOfTrailingZeros(MASK + 1); if ((MASK + 1) != (1 << SHIFT) || digits.length >= 256) { throw new AssertionError(Arrays.toString(digits)); } }
From source file:io.oceanos.shaderbox.dialog.PropertiesDialogFragment.java
@Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); final Shader shader = (Shader) getArguments().getSerializable("shader"); View view = inflater.inflate(R.layout.dialog_properties, null); final EditText nameView = (EditText) view.findViewById(R.id.name); Switch vrMode = (Switch) view.findViewById(R.id.vr_mode); Switch preview = (Switch) view.findViewById(R.id.preview); Spinner resolutionFactor = (Spinner) view.findViewById(R.id.resolution_factor); resolutionFactor.setSelection(Integer.numberOfTrailingZeros(shader.getResolution())); resolutionFactor.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override/*from w ww .jav a 2 s .c o m*/ public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) { shader.setResolution(1 << pos); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); nameView.setText(shader.getName()); vrMode.setChecked(shader.getVrMode() == 1); vrMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { shader.setVrMode(isChecked ? 1 : 0); } }); preview.setChecked(shader.getPreviewMode() == 1); preview.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { shader.setPreviewMode(isChecked ? 1 : 0); } }); builder.setTitle(R.string.properties).setView(view) .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { shader.setName(nameView.getText().toString()); mListener.onSave(shader); } }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { mListener.onCancel(shader); } }); return builder.create(); }
From source file:net.ripe.ipresource.Ipv4Address.java
@Override public boolean isValidNetmask() { int leadingOnesCount = Integer.numberOfLeadingZeros(~(int) value()); int trailingZeroesCount = Integer.numberOfTrailingZeros((int) value()); return leadingOnesCount > 0 && (leadingOnesCount + trailingZeroesCount) == NUMBER_OF_BITS; }
From source file:org.deegree.tools.rendering.dem.builder.DEMDatasetGenerator.java
/** * Creates a new <code>PatchGenerator</code> instance. * //from w w w. j a v a 2 s . c o m * @param raster * the dem raster * @param options * containing information on the given raster. * @param levels * number of levels in the generated (layered) DAG * @param rowsPerTile * number of rows per macro triangle (tile) * @param maxZ * the clipping z value. * @throws SQLException * @throws IOException */ public DEMDatasetGenerator(AbstractRaster raster, RasterIOOptions options, int levels, int rowsPerTile, float maxZ) throws SQLException, IOException { this.dataBuffer = buildGrid(raster, options); if (Float.isNaN(maxZ)) { this.maxZ = getAsFloatSample(-1, -1, 0); System.out.println("Setting max height value to no data value: " + this.maxZ); } else { this.maxZ = maxZ; } // don't use dataBuffer.getWidth() here, as it seems to be happen that // it gets bigger than the input // raster (e.g. 2048 -> 2049) this.inputX = raster.getColumns(); this.inputY = raster.getRows(); this.rowsPerFragment = rowsPerTile; RasterGeoReference rRef = raster.getRasterReference(); sampleSizeX = rRef.getResolutionX(); sampleSizeY = rRef.getResolutionY(); this.geoReference = new RasterGeoReference(OriginLocation.CENTER, rRef.getResolutionX(), rRef.getResolutionY(), rRef.getRotationX(), rRef.getRotationY(), 0, raster.getEnvelope().getSpan1(), raster.getCoordinateSystem()); // calculate the best size int numSamples = Math.max(inputX, inputY); int nextPowerOfTwo = MathUtils.nextPowerOfTwoValue(numSamples); this.outputX = nextPowerOfTwo; this.outputY = nextPowerOfTwo; Envelope env = raster.getRasterReference().getEnvelope(new RasterRect(0, 0, outputX, outputY), null); Point2f p0 = new Point2f(0, (float) env.getSpan1()); Point2f p1 = new Point2f(0, 0); Point2f p2 = new Point2f((float) env.getSpan0(), (float) env.getSpan1()); int lowestLevel = Integer.numberOfTrailingZeros(rowsPerTile); int heighestLevel = Integer.numberOfTrailingZeros(outputX); int tL = (heighestLevel - lowestLevel) * 2; if (levels == -1) { System.out .println("Setting number of levels for " + rowsPerTile + " rows per macro triangle to: " + tL); this.levels = tL; } else { this.levels = levels; } if (tL != this.levels) { System.out.println("++++WARN++++\nThe best number of levels (fitting your data) for " + rowsPerTile + " rows per macro triangle is " + tL + ". You provided: " + levels + ", this will result in " + ((levels < tL) ? "under" : "over") + " sampling your input data.\n++++++++++++"); } /** * rb: a macro triangle will consist of all inner vertices + the 'half-way' vertices which make sure that two * triangles will fit together. for example: <code> * rowsPerTile = 2; * verticersPerTile = 4 * 4 - 3 = 13 * (dots are vertices) * . * :/.\: * :/...\: * </code> * */ this.verticesPerFragment = (rowsPerTile + 2) * (rowsPerTile + 2) - 3; // rb: draw it out, it is working. this.trianglesPerFragment = (4 * rowsPerTile) + (2 * (rowsPerTile - 1) * rowsPerTile); if (getVerticesPerFragment() > 65536) { throw new RuntimeException(Messages.getMessage("DEMDSGEN_TOO_MANY_VERTICES")); } int bytesPerMacroTriangle = (4 + 4 * 3 * getVerticesPerFragment()); // normal vectors this.bytesPerTile = (bytesPerMacroTriangle + (BYTES_PER_NORMAL_COMPONENT * 3 * getVerticesPerFragment())); long fs = 0; int level = 0; while (level < this.levels) { fs += bytesPerTile * (2l << level++); } fileSize = fs; double minX = raster.getEnvelope().getMin().get0(); double minY = raster.getEnvelope().getMin().get1(); double maxX = raster.getEnvelope().getMax().get0(); double maxY = raster.getEnvelope().getMax().get1(); System.out.println("\nInitializing DEMDatasetGenerator"); System.out.println("--------------------------------\n"); // System.out.println( "- input file: " + inputFileName ); System.out.println("- envelope: (" + minX + "," + minY + ")-(" + maxX + "," + maxY + ")"); System.out.println("- raster sample size: x=" + sampleSizeX + ", y=" + sampleSizeY); System.out.println("- bintritree levels: " + levels); System.out.println("- rows per tile: " + rowsPerTile); System.out.println("- vertices per tile: " + getVerticesPerFragment()); System.out.println("- triangles per tile: " + getTrianglesPerFragment()); System.out.println("- bytes per tile: " + getBytesPerTile()); System.out.println("- filesize will be: " + fileSize + " bytes (" + Math.round((fileSize / (1024 * 1024d)) * 100d) / 100d + " Mb)"); System.out.println("- WPVS translationvector should be: <TranslationToLocalCRS x=\"-" + minX + "\" y=\"-" + minY + "\"/>"); outputTriangleHeights(p0, p1, p2, this.getLevels()); }
From source file:com.android.nobug.view.pattern.PatternView.java
/** * Returns the greatest common divisor of {@code a, b}. Returns {@code 0} if * {@code a == 0 && b == 0}.//from ww w . j a v a2 s.c o m * * @throws IllegalArgumentException if {@code a < 0} or {@code b < 0} */ private static int gcd(int a, int b) { /* * The reason we require both arguments to be >= 0 is because otherwise, what do you return * on gcd(0, Integer.MIN_VALUE)? BigInteger.gcd would return positive 2^31, but positive * 2^31 isn't an int. */ //checkNonNegative("a", a); if (a < 0) { throw new IllegalArgumentException("a (" + a + ") must be >= 0"); } //checkNonNegative("b", b); if (b < 0) { throw new IllegalArgumentException("b (" + b + ") must be >= 0"); } if (a == 0) { // 0 % b == 0, so b divides a, but the converse doesn't hold. // BigInteger.gcd is consistent with this decision. return b; } else if (b == 0) { return a; // similar logic } /* * Uses the binary GCD algorithm; see http://en.wikipedia.org/wiki/Binary_GCD_algorithm. * This is >40% faster than the Euclidean algorithm in benchmarks. */ int aTwos = Integer.numberOfTrailingZeros(a); a >>= aTwos; // divide out all 2s int bTwos = Integer.numberOfTrailingZeros(b); b >>= bTwos; // divide out all 2s while (a != b) { // both a, b are odd // The key to the binary GCD algorithm is as follows: // Both a and b are odd. Assume a > b; then gcd(a - b, b) = gcd(a, b). // But in gcd(a - b, b), a - b is even and b is odd, so we can divide out powers of two. // We bend over backwards to avoid branching, adapting a technique from // http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax int delta = a - b; // can't overflow, since a and b are nonnegative int minDeltaOrZero = delta & (delta >> (Integer.SIZE - 1)); // equivalent to Math.min(delta, 0) a = delta - minDeltaOrZero - minDeltaOrZero; // sets a to Math.abs(a - b) // a is now nonnegative and even b += minDeltaOrZero; // sets b to min(old a, b) a >>= Integer.numberOfTrailingZeros(a); // divide out all 2s, since 2 doesn't divide b } return a << Math.min(aTwos, bTwos); }