473,804 Members | 3,123 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why write putc(s.i16 & 0xff, fp);

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.

Oct 26 '07
23 3076
Jorge wrote:
) 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.

Think about what would happen with this bit of code on a system where
a char is 9 bits. What would the result be ? What exactly would putc
be outputting ?
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Oct 27 '07 #11
Willem wrote:
Jorge wrote:
) 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.

Think about what would happen with this bit of code on a system where
a char is 9 bits. What would the result be ? What exactly would putc
be outputting ?
That char should be assumed to be 8 bits is part of the design
specifications for this code, so that can't be the justification for the
&0xff.
Oct 27 '07 #12
* Even positive numbers may be represented in a crazy way. On the
Deathstation, positive numbers are represented in base 1, that is,
the number is the number of 1s in the variable. So 0 is all bits
0, 1 is 1, two is 11, three is 111, etc.

Not so. The fundamental binary representation of positive integers
is specified in the standard.
So a machine that uses BCD would have to do emulation to be the target
of a C implementation?

In any event, I think this issue should be in the answer to the
question. People will be thinking why is that &0xff needed, and the
FAQ should be didactic.

Oct 27 '07 #13
Jorge Peixoto wrote:
....
So a machine that uses BCD would have to do emulation to be the target
of a C implementation?
Yes. Bitwise operators must be implemented as if operating on a binary
representation of the value, which would presumably not be a simple
operation on such a machine. Trinary machines (which have some
interesting advantages in certain contexts compared to binary machines)
would also be problematic.
Oct 27 '07 #14
On Oct 26, 4:44 pm, pete <pfil...@mindsp ring.comwrote:
Jorge Peixoto wrote:
On Oct 26, 8:46 pm, pete <pfil...@mindsp ring.comwrote:
Jorge Peixoto wrote:
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.
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
It doesn't matter.
The standard guarantees (according tohttp://c-faq.com/decl/inttypes.html)
that an unsigned char can hold any integer from 0 to 255. Since we are
already assuming that char is 8 bits, it can hold at most 256 numbers;
there are already 256 numbers between 0 and 255, so 0-255 is exactly
the range of numbers that an unsigned char can hold.
As far as I know (if I am wrong here please correct me), when you
convert from an integer to an unsigned, shorter one, the result is the
nonnegative remander in the division by a number that is 1 bigger
than the maximum number that can be represented by the smaller type.
In our case, this number is 256.
Taking the remainder modulo 256 is equivalent to bitwise and with
0xff, isn't it?

No.

There are three allowable ways to represent (-1) in 16 bits:
1111 1111 1111 1111
1111 1111 1111 1110
1000 0000 0000 0001

If s.i16 has a value of (-1),
then (s.i16 & 0xff) can have a value of either
255, or 254, or 1.

((unsigned char)-1) is always equal to UCHAR_MAX.

--
pete

Would 0xff on a 16 bit machine be represented as
0000 0000 1111 1111

In other words, would there be leading zero's before 1111 1111 ?

Oct 27 '07 #15
Yes. Bitwise operators must be implemented as if operating on a binary
representation of the value, which would presumably not be a simple
operation on such a machine. Trinary machines (which have some
interesting advantages in certain contexts compared to binary machines)
would also be problematic.
So the code here *is* redundant! This call
putc((unsigned) ((s.i32 >24) & 0xff), fp);

is equivalent to this

putc((unsigned) (s.i32 >24), fp);

In the second call, you cast to unsigned char, which means taking the
remainder modulo 256.
In the first call, you perform bitwise and with 0xff; you are telling
me that this will behave (for unsigned numbers, and this one is
unsigned) as if the number was represented in two's complement; so
this is equivalent to taking the remainder modulo 256.

And I think that even the cast to unsigned is unneeded. Can you get
different results from casting an integer to unsigned, then to
unsigned char, instead of directly casting from integer to unsigned
char?

Oct 27 '07 #16
On Oct 27, 4:03 pm, Jorge Peixoto <JorgePeixotoMo r...@gmail.com>
wrote:
Yes. Bitwise operators must be implemented as if operating on a binary
representation of the value, which would presumably not be a simple
operation on such a machine. Trinary machines (which have some
interesting advantages in certain contexts compared to binary machines)
would also be problematic.

So the code here *is* redundant! This call
putc((unsigned) ((s.i32 >24) & 0xff), fp);

is equivalent to this

putc((unsigned) (s.i32 >24), fp);

In the second call, you cast to unsigned char, which means taking the
remainder modulo 256.
In the first call, you perform bitwise and with 0xff; you are telling
me that this will behave (for unsigned numbers, and this one is
unsigned) as if the number was represented in two's complement; so
this is equivalent to taking the remainder modulo 256.
Oh wait, he casts to unsigned *after* the and.

In any event, that FAQ answer needs clarification !
Oct 27 '07 #17
"James Kuyper" <ja*********@ve rizon.neta écrit dans le message de news:
dgIUi.3555$eD3. 688@trnddc03...
Jorge Peixoto wrote:
...
>So a machine that uses BCD would have to do emulation to be the target
of a C implementation?

Yes. Bitwise operators must be implemented as if operating on a binary
representation of the value, which would presumably not be a simple
operation on such a machine. Trinary machines (which have some interesting
advantages in certain contexts compared to binary machines) would also be
problematic.
BCD representations would be problematic for integer types, but not for
floating point where they actually have interesting properties, especially
for financial and accounting applications.

--
Chqrlie.
Oct 27 '07 #18
"James Kuyper" <ja*********@ve rizon.neta écrit dans le message de news:
IUEUi.94$a01.65 @trnddc06...
Willem wrote:
>Jorge wrote:
) 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.

Think about what would happen with this bit of code on a system where
a char is 9 bits. What would the result be ? What exactly would putc
be outputting ?

That char should be assumed to be 8 bits is part of the design
specifications for this code, so that can't be the justification for the
&0xff.
That's the beauty of this code, it does not even assume char to be eight bit
wide. It is not fully portable to weird obsolete non 2's complement
architectures, but it does produce 4 octets on the stream even if CHAR_BIT
!= 8.

If CHAR_BIT == 8, the mask is not necessary.

--
Chqrlie.
Oct 27 '07 #19
Chad wrote:
>
On Oct 26, 4:44 pm, pete <pfil...@mindsp ring.comwrote:
There are three allowable ways to represent (-1) in 16 bits:
1111 1111 1111 1111
1111 1111 1111 1110
1000 0000 0000 0001
Would 0xff on a 16 bit machine be represented as
0000 0000 1111 1111
In the manner of notation that I was using above, yes.

--
pete
Oct 28 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
11766
by: Geoff Wilkins | last post by:
I must confess I only come here when I have a problem - so my apologies if this has been raised before: Using my IE v.6 browser, document.write doesn't convert HTML entities (e.g. &apos;, &amp;) to the appropriate character (though NS 6.2 works fine). Obviously I can get round this for particular entities by writing some code to do the conversion before using document.write - but I need a more general solution that will catch any of the...
2
2519
by: Unemployed VC++ guy | last post by:
It seems that there is basically no difference between the Console.Write() & String.Format(), other than the output being sent to the console output stream instead of a string. Is this accurate? String formatter = "(whatever)"; Console.Write(formatter,object1,object2, ... ); String formattedString = String.Format(formatter,object1,object2, ... );
4
2306
by: clintonG | last post by:
Anybody know how to dynamically write the meta tags using code so they are formatted on a separate line in the HTML source? Preferred or optimal framework classes that may be used in this regard? <meta... /> <meta... /> <meta... /> <%= Clinton Gallagher
6
1230
by: Basil Fawlty | last post by:
Hi everyone, I have VB.NET 2003 SE, I understand that I can write and compile C and C++ code in this tool. Is that right? If so, how would I go about doing something like that? It won't be anything fancy what-so-ever. Thanks, Basil
1
1739
by: Lialie | last post by:
Hello,all I found it easy to read configures from a config file. But how can I set a special value to an item or write it into the original file? I have tried this: import ConfigParser config = ConfigParser.ConfigParser() config.read('a.conf') config.get('Main', 'Something') # That is OK.
4
4142
by: ateale | last post by:
Hi Guys, Is it possible to give a folder permissions so that files can be written to it and read from it, just not deleted? Thanks! Adam
65
5109
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but got errors of Failed to open file ./outdir/mytestout.txt. Below is the code: #include <stdio.h>
2
2784
by: bruceturek | last post by:
I'm having an issue that I'm sure there is a simple fix for! I'm creating a java script that will dynamically create a URL. In the process I need to include a URL parameter '&timestamp=' followed by an actual timestamp. I noticed that when I use the combination &timestamp under IE 7 the string gets converted to xtamp. This even occurs if I just use document.write('&timestamp'); to write the string out. It results in xtamp! Under a firefox...
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9587
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.