简体   繁体   中英

What is the best way to implement a circular array byte shift in java?

If the shifting is not always the same, ie I may have to use the same function to resize 2 or 4 characters, what would be a good way to circular shift the values of an array of bytes 2 positions * a parameter? This is what I have so far

for(int j=0; j<param; j++){
        if(j == 0){
            for(int i=0; i<myArray.length;i++){
                result[i] = (byte) (myArray[i]<<2); 
            }
        } else{
            for(int i=0; i<result.length;i++){
                if((result.length-i) > 2){
                    result[i] = (byte) (result[i]<<2);
                }
            }   
        }
    }

Summing up, I have to circular shift the values of myArray twice times param and return the result in the array 'result'. I don't get how to do this when the parameter 'param' is not fixed.

First: If possible, use java.util.BitSet for tasks like that.

I am not sure, but somehow BitSet itself does not have shift, but this source looks it implemented it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM