繁体   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