比特和东西 [英] bits and stuff
问题描述
#include< stdio.h>
int main()
{
unsigned long num;
unsigned int a = 10,b = 20,c = 30,d = 40;
/ * num = 0; * /
num | = a<< 24;
num | = b<< 16;
num | = c<< 8;
num | = d<< 0;
printf(" a =%02.2x \ nb =%02.2x \ nc =%02.2x \\\ =%02.2x \ n",a ,b,c,d);
printf(" num =%08.8x \ n",num);
返回0;
}
我在这里要做的是将a,b,c和d打包到num。
如果我设置num = 0,它会起作用,但是如果我把它留下来它怎么会不起作用
(如上所述)?我知道当声明num时,它的内存内容已经满了
垃圾,但是我认为通过将int包装进去就可以了。
覆盖了所有的垃圾? (希望有道理)
谢谢,
乔
Joe Laughlin写道:#include< stdio.h>
int main()
{
无符号long num;
unsigned int a = 10,b = 20,c = 30,d = 40;
/ * num = 0; * /
num | = a<< 24;
这与写作相同:
num = num | a<< 24;
即你在'num''和'a''中做了一些'或''''或'
左移24位。看看问题是什么?
num | = b<< 16;
num | = c<< 8;
num | = d<< 0;
printf(" a =%02.2x \ nb =%02.2x \ nc =%02.2x \\\ =%02.2x \ n,a,b,c ,d);
printf(" num =%08.8x \ n",num);
返回0;
}
什么我想在这里做的是将a,b,c和d打包到num。
如果我设置num = 0它会起作用,但是如果我离开它怎么会不起作用它出来
(如上所述)?我知道当声明num时,它的内存内容已经满了垃圾,但是我认为通过将int包装进去就可以覆盖所有的垃圾? (希望有意义)
见上文。
HTH,
--ag
-
Artie Gold - 德克萨斯州奥斯汀
他们指责你 - 是他们的计划。
2004年6月11日星期五23:01:49 GMT,comp.lang.c,Joe Laughlin
< Jo***************@boeing.com>写道:
#include< stdio.h>
int main()
{
unsigned long num;
这是一个未初始化的变量。它包含垃圾。
num | = a<< 24;
在这里你或者一些垃圾。
GIGO。
我在这里要做的是将a,b,c和d打包成num。
这不能保证完全有效 - 你假设
sizeof(long)== 4这不一定是真的。
我以为通过将int填入其中会覆盖掉所有的垃圾吗? (希望是有道理的)
你正在把垃圾与它混合在一起。
-
Mark McIntyre
CLC常见问题< http://www.eskimo.com/~scs/C-faq/top.html>
CLC自述文件: < http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
---- ==通过Newsfeed.Com发布 - 无限制 - 未经审查 - 安全Usenet新闻== - ---
http://www.newsfeed.com #1新闻组服务于世界! > 100,000新闻组
--- = 19东/西海岸专业服务器 - 通过加密的总隐私= ---
---- ==通过新闻发布.Com - Unlimited-Uncensored-Secure Usenet News == ----
http:// www.newsfeed.com 世界排名第一的新闻组服务! > 100,000新闻组
--- = 19东/西海岸专业服务器 - 通过加密的总隐私= ---
周六,2004年6月12日00:27:04 +0100,Mark McIntyre
< ma ********** @ spamcop.net>在comp.lang.c中写道:
2004年6月11日星期五23:01:49 GMT,comp.lang.c,Joe Laughlin
< ;乔*************** @ boeing.com>写道:
#include< stdio.h>
int main()
{
unsigned long num;
这是一个未初始化的变量。它包含垃圾。
num | = a<< 24;
在这里你或者垃圾桶。
GIGO。
我想做什么这里是打包a,b,c和d到num。
这根本不能保证 - 你假设
sizeof(long)= = 4这不一定是真的。
不,他不是。他假设unsigned long包含至少32
值位,这是由标准保证的。
在工作中使用的编译器上今天,unsigned long正好有32个值b $ b值。而sizeof(unsigned long)是2,而不是4.
我认为通过将int包装进去就会被覆盖所有的垃圾? (希望这很有意义)
你正在用垃圾进行ORING。
嗯,这是'是的。
-
Jack Klein
主页: http://JK-Technology.Com
常见问题解答
comp.lang.c http://www.eskimo.com/~scs/C- faq / top.html
comp.lang.c ++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu .edu / ~a ... FAQ-acllc.html
#include <stdio.h>
int main()
{
unsigned long num;
unsigned int a = 10, b = 20, c = 30, d = 40;
/* num = 0; */
num |= a << 24;
num |= b << 16;
num |= c << 8;
num |= d << 0;
printf("a = %02.2x\nb = %02.2x\nc = %02.2x\nd = %02.2x\n", a, b, c, d);
printf("num = %08.8x\n", num);
return 0;
}
What I''m trying to do here is pack a, b, c, and d into num.
It works if I set num = 0, but how come it''s not working if I leave it out
(like above)? I know that when num is declared, its memory contents is full
of garbage, but I thought that by packing the ints into it would''ve
overwritten all the garbage? (hope that makes sense)
Thanks,
Joe
Joe Laughlin wrote:#include <stdio.h>
int main()
{
unsigned long num;
unsigned int a = 10, b = 20, c = 30, d = 40;
/* num = 0; */
num |= a << 24;
This is the same as writing:
num = num | a << 24;
i.e. you''re doing a bitwise `or'' of whatever was in `num'' and `a''
left-shifted 24 bits. See what the problem is?
num |= b << 16;
num |= c << 8;
num |= d << 0;
printf("a = %02.2x\nb = %02.2x\nc = %02.2x\nd = %02.2x\n", a, b, c, d);
printf("num = %08.8x\n", num);
return 0;
}
What I''m trying to do here is pack a, b, c, and d into num.
It works if I set num = 0, but how come it''s not working if I leave it out
(like above)? I know that when num is declared, its memory contents is full
of garbage, but I thought that by packing the ints into it would''ve
overwritten all the garbage? (hope that makes sense)
See above.
HTH,
--ag
--
Artie Gold -- Austin, Texas
"What they accuse you of -- is what they have planned."
On Fri, 11 Jun 2004 23:01:49 GMT, in comp.lang.c , "Joe Laughlin"
<Jo***************@boeing.com> wrote:
#include <stdio.h>
int main()
{
unsigned long num;
this is an uninitialised variable. It contains garbage.
num |= a << 24;
here you OR the bits of a with garbage.
GIGO.
What I''m trying to do here is pack a, b, c, and d into num.
This is not guaranteed to work at all - you''re assuming that
sizeof(long)==4 which need not be true.
I thought that by packing the ints into it would''ve
overwritten all the garbage? (hope that makes sense)
You''re ORing it with the garbage.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
On Sat, 12 Jun 2004 00:27:04 +0100, Mark McIntyre
<ma**********@spamcop.net> wrote in comp.lang.c:
On Fri, 11 Jun 2004 23:01:49 GMT, in comp.lang.c , "Joe Laughlin"
<Jo***************@boeing.com> wrote:#include <stdio.h>
int main()
{
unsigned long num;
this is an uninitialised variable. It contains garbage.num |= a << 24;
here you OR the bits of a with garbage.
GIGO.What I''m trying to do here is pack a, b, c, and d into num.
This is not guaranteed to work at all - you''re assuming that
sizeof(long)==4 which need not be true.
No he''s not. He''s assuming that unsigned long contains at least 32
value bits, and that is guaranteed by the standard.
On the compiler I used at work today, unsigned long has exactly 32
value bits. And sizeof(unsigned long) is 2, not 4.
I thought that by packing the ints into it would''ve
overwritten all the garbage? (hope that makes sense)
You''re ORing it with the garbage.
Well, that''s true.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
这篇关于比特和东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!