簡體   English   中英

難以理解Java中的方法

[英]trouble in understanding a method in java

以下是在Java中用於消息填充的方法(公共靜態byte [] bitStuff(byte [] b))。 但是我不明白方法中注釋掉的行:

 public static byte set_1_at_position(int i, byte b) {
    b |= (128 >> i);
    return b;
}

public static byte set_0_at_position(int i, byte b) {
    b &= (byte) (~(128 >> i));
    return b;
}

public static int get_bit_at_position(int pos, byte b) {
    if ((b & (byte) (128 >> pos)) == 0) {
        return 0;
    }
    return 1;
}



    public static byte[] bitStuff(byte[] b){

            byte[] temp=new byte[b.length*2];
            int size=b.length*8;
            int count_of_one=0;
            int count_of_temp=0;
            for(int i=0;i<size;i++)
            {
                int bit=get_bit_at_position(i%8, b[i/8]); //what is being done here
                if(bit==1)
                {
                    count_of_one++;
                    temp[count_of_temp/8]=set_1_at_position(count_of_temp%8, temp[count_of_temp/8]);  //what is happening in this line
                }
                else
                {
                    count_of_one=0;
                    temp[count_of_temp/8]=set_0_at_position(count_of_temp%8, temp[count_of_temp/8]);
                }

                count_of_temp++;

                if(count_of_one==5)
                {
                    count_of_one=0;
                    //bit stuffing
                    temp[count_of_temp/8]=set_0_at_position(count_of_temp%8, temp[count_of_temp/8]);  // what is happening in this line
                    count_of_temp++;
                }
            }

            byte[] ret=new byte[(count_of_temp+7)/8];
            System.arraycopy(temp, 0, ret, 0, ret.length);

    return ret;

}

誰能解釋這些話?

請注意,“大小”和“ i”變量正在計算位數(不是字節)。 get_bit_at_position(i%8,b [i / 8]); 該行返回第“ i”位值。 第“ i”位值等於第“ i / 8”個字節處的“ i%8”位值。 set_0_at_position(count_of_temp%8,temp [count_of_temp / 8]); 這幾乎與上述直線相反。 它將第“ i”位設置為零。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM