首页 新闻 会员 周边

求助:这个函数是什么意思?我怎么看不懂

0
[已解决问题] 解决于 2010-12-27 20:01

v

 

如下,只能看懂>>是右移位的意思,i>>3的意思是i/8,剩下的就看不懂了

 

void myDES::ByteToBit(bool *Out, const char *In, int bits)
{
    for(int i=0; i<bits; ++i)
        Out[i] = (In[i>>3]>>(7 - i&7)) & 1;
}

 

finallyly的主页 finallyly | 初学一级 | 园豆:100
提问于:2010-12-27 19:34
< >
分享
最佳答案
0

我不能确定bool到底占几位。所以,献给出我的解释,如果bool变量只占1为,而char占8位

 

void myDES::ByteToBit(bool *Out, const char *In, int bits)
{
union char2byte{
bool b[8];
char c;
}char2byte;

for(int i=0; i<bits; ++i){
char2byte
.c=In[i];
memcpy(Out,
8,char2byte.b);
Out
+= 8;
}
}

 

wales.guo | 菜鸟二级 |园豆:205 | 2010-12-27 19:52
谢谢你。我明白了。在C语言里bool==0或1,所以也占1个byte,8个bit。这段代码的意思是进行进制转换的,我没写全,刚才看明白了
finallyly | 园豆:100 (初学一级) | 2010-12-27 20:01
其他回答(1)
0

C语言里面89年的标准里面是没有bool类型的, C标准到了99年的版本才加入bool类型的。微软的VS系列编译器都不支持C99标准,也就是说没有bool类型的。

C++里面是有bool类型的。0为false, 非0为true.

 

还有,这位同学的C or C++学的不精,操作位移的时候,要使用unsigned type, 标准是这样说的

The result of E1 >> E2 is E1 right-shifted E2 bit positions 。。。。If E1 has asigned type and a negative value, the resulting value is implementation-defined.

嗷嗷 | 园豆:757 (小虾三级) | 2010-12-27 21:00
晕,你怎么对C语言知道的这么细致? The result of E1 >> E2 is E1 right-shifted E2 bit positions 。。。。If E1 has asigned type and a negative value, the resulting value is implementation-defined. 相同的内容我在谭浩强的《C语言程序设计》中看到了。估计是这样:这是从密码加密程序中抽出的一个子函数。由于是对ASCII加密,而ASCII的范围是0-255,所以一字节的最高位不可能会出现1吧
支持(0) 反对(0) finallyly | 园豆:100 (初学一级) | 2010-12-27 21:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册