473,795 Members | 2,974 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 #1
23 3072
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.
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

Oct 26 '07 #2
On Oct 26, 5:58 pm, CBFalconer <cbfalco...@yah oo.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.

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
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.

Oct 26 '07 #3
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
Oct 26 '07 #4
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 to http://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?

Oct 26 '07 #5
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 to http://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
Oct 26 '07 #6
CBFalconer wrote:
>
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.

Where did you get the magic number 8 there?
It says so, here: http://c-faq.com/stdio/extconform.html

--
pete
Oct 26 '07 #7
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
I didn't know that. So the bitwise operators are machine-dependent?
Are you telling me that the following simple code

#include <stdio.h>

int main (void) {
unsigned t = 256;
t &= 0xff;
printf ("%u\n", t);
return 0;
}

is machine dependent*? I thought that the bitwise operators would
behave as if the number is in two's complement, in any machine. It is
amazing if the simple code above is machine dependent.

* 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.

Oct 27 '07 #8
On Fri, 26 Oct 2007 14:17:56 -0700, Jorge Peixoto
<Jo************ ****@gmail.comw rote:
>On Oct 26, 5:58 pm, CBFalconer <cbfalco...@yah oo.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.

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

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.
Indeed. And the FAQ explicitly says so ("This code assumes that getc
reads 8-bit characters").

--
jay

http://c-faq.com/
http://msdn2.microsoft.com/en-us/express/default.aspx
http://www.gimpel.com/
http://www.ubuntu.com/
http://www.embedded.com/

Oct 27 '07 #9
Jorge Peixoto wrote:
>
.... snip ...
>
* 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.

--
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

Oct 27 '07 #10

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

Similar topics

4
11765
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
2518
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
2304
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
1229
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
1738
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
4141
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
5106
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
2782
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
9672
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
9519
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,...
1
10165
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
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9043
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...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4113
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
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.