Java Array Shift ShiftAwayTrailingZerosTwoElements(int[] arr)

Here you can find the source of ShiftAwayTrailingZerosTwoElements(int[] arr)

Description

Shift Away Trailing Zeros Two Elements

License

Creative Commons License

Declaration

static int ShiftAwayTrailingZerosTwoElements(int[] arr) 

Method Source Code

//package com.java2s;

public class Main {
    static int ShiftAwayTrailingZerosTwoElements(int[] arr) {
        int a0 = arr[0];
        int a1 = arr[1];
        int tz = CountTrailingZeros(a0);
        if (tz == 0) {
            return 0;
        }//from w  w w  .ja  v a 2 s . co m
        {
            if (tz < 32) {
                int carry = a1 << (32 - tz);
                arr[0] = (int) ((a0 >> tz) & (0x7fffffff >> (tz - 1))) | (int) carry;
                arr[1] = (a1 >> tz) & (0x7fffffff >> (tz - 1));
                return tz;
            }
            tz = CountTrailingZeros(a1);
            if (tz == 32) {
                arr[0] = 0;
            } else if (tz > 0) {
                arr[0] = (a1 >> tz) & (0x7fffffff >> (tz - 1));
            } else {
                arr[0] = a1;
            }
            arr[1] = 0;
            return 32 + tz;
        }
    }

    private static int CountTrailingZeros(int numberValue) {
        if (numberValue == 0) {
            return 32;
        }
        int i = 0;
        {
            if ((numberValue << 16) == 0) {
                numberValue >>= 16;
                i += 16;
            }
            if ((numberValue << 24) == 0) {
                numberValue >>= 8;
                i += 8;
            }
            if ((numberValue << 28) == 0) {
                numberValue >>= 4;
                i += 4;
            }
            if ((numberValue << 30) == 0) {
                numberValue >>= 2;
                i += 2;
            }
            if ((numberValue << 31) == 0) {
                ++i;
            }
        }
        return i;
    }
}

Related

  1. shift(T[] array, int count)
  2. shiftArgs(final String[] args)
  3. shiftArgs(String[] args, int offset)
  4. shiftArray(String[] args, int shift)
  5. shiftArray(String[] array, int offset)
  6. shiftChroma(float[] chroma, int step)
  7. shiftEnum(E current, E[] values, int delta)
  8. shiftLeft(byte[] array)
  9. shiftLeft(byte[] b1, int bytes)