为什么要写putc(s.i16& 0xff,fp); [英] Why write putc(s.i16 & 0xff, fp);
问题描述
在对C FAQ的问题12.42的回答中,我们有以下代码:
putc((unsigned)((s.i32> 24)& 0xff), fp);
putc((unsigned)((s.i32> 16)& 0xff),fp);
putc((unsigned)((s。) i32> 8)& 0xff),fp);
putc((无符号)(s.i32& 0xff),fp);
putc((s .i16> 8)& 0xff,fp);
putc(s.i16& 0xff,fp);
为什么& 0xff? putc函数将其参数转换为unsigned
char,因此除了低8位之外的任何内容都会被自动丢弃。并且
代码已经假设一个字符是8位。
In the answer to question 12.42 of the C FAQ, we have this code:
putc((unsigned)((s.i32 >24) & 0xff), fp);
putc((unsigned)((s.i32 >16) & 0xff), fp);
putc((unsigned)((s.i32 >8) & 0xff), fp);
putc((unsigned)(s.i32 & 0xff), fp);
putc((s.i16 >8) & 0xff, fp);
putc(s.i16 & 0xff, fp);
Why the & 0xff ? The putc function casts its argument to an unsigned
char, so anything but the 8 lower bits is automatically discarded. And
the code already assumes the a char is 8 bits.
推荐答案
Jorge Peixoto写道:
Jorge Peixoto wrote:
>
在C FAQ问题12.42的回答中,我们有以下代码:
putc((unsigned)((s.i32> 24)& 0xff),fp);
putc((unsigned)((s.i32> 16)& 0xff ),fp);
putc((unsigned)((s.i32> 8)& 0xff),fp);
putc((unsigned)(s .i32& 0xff),fp);
putc((s.i16> 8)& 0xff,fp);
putc(s .i16& 0xff,fp);
为什么& 0xff? putc函数将其参数转换为unsigned
char,因此除了低8位之外的任何内容都会被自动丢弃。并且
代码已经假定char是8位。
>
In the answer to question 12.42 of the C FAQ, we have this code:
putc((unsigned)((s.i32 >24) & 0xff), fp);
putc((unsigned)((s.i32 >16) & 0xff), fp);
putc((unsigned)((s.i32 >8) & 0xff), fp);
putc((unsigned)(s.i32 & 0xff), fp);
putc((s.i16 >8) & 0xff, fp);
putc(s.i16 & 0xff, fp);
Why the & 0xff ? The putc function casts its argument to an unsigned
char, so anything but the 8 lower bits is automatically discarded. And
the code already assumes the a char is 8 bits.
你在哪里得到了神奇的数字8?字节和字符可以是
任何大于或等于8的大小,由CHAR_BIT在limits.h中给出。
-
Chuck F(cinefalconer at maineline dot net)
可用于咨询/临时嵌入式和系统。
< http://cbfalconer.home.att.net>
-
通过的免费Usenet帐户发布http://www.teranews.com
Where did you get the magic number 8 there? Bytes and chars can be
any size greater or equal to 8, given by CHAR_BIT in limits.h.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com
10月26日下午5:58,CBFalconer< cbfalco ... @ yahoo .comwrote:
On Oct 26, 5:58 pm, CBFalconer <cbfalco...@yahoo.comwrote:
Jorge Peixoto写道:
Jorge Peixoto wrote:
在回答C FAQ的问题12.42时,我们有这个代码:
In the answer to question 12.42 of the C FAQ, we have this code:
putc((unsigned)((s.i32> 24)& 0xff),fp);
putc((unsigned)((s.i32> 16)& 0xff),fp);
putc((unsigned)((s.i32> 8)& ; 0xff),fp);
putc((unsigned)(s.i32& 0xff),fp);
putc((unsigned)((s.i32 >24) & 0xff), fp);
putc((unsigned)((s.i32 >16) & 0xff), fp);
putc((unsigned)((s.i32 >8) & 0xff), fp);
putc((unsigned)(s.i32 & 0xff), fp);
putc((s.i16> 8)& 0xff,fp);
putc(s.i16 & 0xff,fp);
putc((s.i16 >8) & 0xff, fp);
putc(s.i16 & 0xff, fp);
为什么& 0xff? putc函数将其参数转换为unsigned
char,因此除了低8位之外的任何内容都会被自动丢弃。并且
代码已经假定char是8位。
Why the & 0xff ? The putc function casts its argument to an unsigned
char, so anything but the 8 lower bits is automatically discarded. And
the code already assumes the a char is 8 bits.
你在哪里得到了神奇的数字8?字节和字符可以是
任何大小等于8,由CHAR_BIT在limits.h中给出。
Where did you get the magic number 8 there? Bytes and chars can be
any size greater or equal to 8, given by CHAR_BIT in limits.h
是的,但是此代码(这是来自C FAQ)已经假设char
是8位。阅读常见问题解答12.42的答案。
Yes, but this code (which is from the C FAQ) already assumes that char
is 8 bits. Read the answer to the question 12.42 of the FAQ.
Jorge Peixoto写道:
Jorge Peixoto wrote:
>
在C FAQ的问题12.42的答案中,我们有以下代码:
putc((unsigned)((s.i32> 24)& 0xff),fp);
putc((unsigned)((s.i32> 16)& 0xff),fp);
putc( (未签名)((s.i32> 8)& 0xff),fp);
putc((unsigned)(s.i32& 0xff),fp);
putc((s.i16> 8)& 0xff,fp);
putc(s.i16& 0xff,fp);
为什么& 0xff? putc函数将其参数转换为unsigned
char,因此除了低8位之外的任何内容都会被自动丢弃。并且
代码已经假定char是8位。
>
In the answer to question 12.42 of the C FAQ, we have this code:
putc((unsigned)((s.i32 >24) & 0xff), fp);
putc((unsigned)((s.i32 >16) & 0xff), fp);
putc((unsigned)((s.i32 >8) & 0xff), fp);
putc((unsigned)(s.i32 & 0xff), fp);
putc((s.i16 >8) & 0xff, fp);
putc(s.i16 & 0xff, fp);
Why the & 0xff ? The putc function casts its argument to an unsigned
char, so anything but the 8 lower bits is automatically discarded. And
the code already assumes the a char is 8 bits.
如果负整数的表示不是两个补码,那么
然后转换为unsigned char
与s.i16为负时丢弃位不同。
-
pete
If the representation of negative integers isn''t two''s complement,
then converting to unsigned char
is different from discarding bits when s.i16 is negative.
--
pete
这篇关于为什么要写putc(s.i16& 0xff,fp);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!