以下是用于在java中对消息进行位填充的方法(public static 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;
}
任何人都可以解释这些界限吗?
答案 0 :(得分:1)
请注意,“size”和“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”位设置为零。