Does the code below make any non-portable assumptions other than
CHAR_BIT==8?
char buf[100];
/* do things with buf */
if( !(buf[0]^0xFF) ) /* assume CHAR_BIT is 8 - test for equality */
/* whatever */
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome. 7 1259
On Tue, 2 Mar 2004, Christopher Benson-Manica wrote: Does the code below make any non-portable assumptions other than CHAR_BIT==8?
char buf[100];
/* do things with buf */
if( !(buf[0]^0xFF) ) /* assume CHAR_BIT is 8 - test for equality */
On a sane implementation, I think this line would always be
equivalent to either
if (buf[0] == (char)0xFF)
or
if ((unsigned char)buf[0] == 0xFFu)
But on an unreasonable yet conforming implementation the evaluation
of (buf[0]^0xFF) could trap, invoking undefined behavior. (On such
implementations, 'char' would necessarily be signed and have one trap
representation.)
In short: I can't think of any use for this construct, and it's
more dangerous than it is helpful, so I would avoid it like the
plague.
-Arthur
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote: But on an unreasonable yet conforming implementation the evaluation of (buf[0]^0xFF) could trap, invoking undefined behavior. (On such implementations, 'char' would necessarily be signed and have one trap representation.)
I don't think char is allowed to have trap representations. Unsigned
char certainly isn't, but surely neither is signed char?
Richard
On Wed, 3 Mar 2004, Richard Bos wrote: "Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote: But on an unreasonable yet conforming implementation the evaluation of (buf[0]^0xFF) could trap, invoking undefined behavior. (On such implementations, 'char' would necessarily be signed and have one trap representation.)
I don't think char is allowed to have trap representations. Unsigned char certainly isn't, but surely neither is signed char?
I don't see why not. Consider the case of ones' complement signed
char, in which 11111111 ("negative zero") is a trap representation.
Now make plain char signed. :)
-Arthur
>On Tue, 2 Mar 2004, Christopher Benson-Manica wrote: Does the code below make any non-portable assumptions other than CHAR_BIT==8?
char buf[100];
/* do things with buf */
if( !(buf[0]^0xFF) ) /* assume CHAR_BIT is 8 - test for equality */
In article <news:Pi**********************************@unix48. andrew.cmu.edu>
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> wrote: On a sane implementation, I think this line would always be equivalent to either
if (buf[0] == (char)0xFF)
or
if ((unsigned char)buf[0] == 0xFFu)
Why would you think that?
The operands of the "^" operator are subject to the usual arithmetic
promotions (and constrained to be integers, hence really just the
integer promotions). If plain char is signed, then, and if buf[0]
holds 0xff with CHAR_BIT being 8, buf[0] must be signed and represents
the value -1. Of course, int is at least 16 bits, and a typical
implementation would wind up with the bit pattern 0xffff or 0xffffffff
on the left. The right is just the integer constant 0xff or (int)255,
and the result of the xor is likely to be 0xff00 or 0xffffff00.
But on an unreasonable yet conforming implementation the evaluation of (buf[0]^0xFF) could trap, invoking undefined behavior. (On such implementations, 'char' would necessarily be signed and have one trap representation.)
Yes, this is also possible. I think the fact that it does not
work on most real implementations is more important, though. :-)
In short: I can't think of any use for this construct, and it's more dangerous than it is helpful, so I would avoid it like the plague.
Indeed.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Arthur J. O'Dwyer wrote: On Wed, 3 Mar 2004, Richard Bos wrote: "Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote: But on an unreasonable yet conforming implementation the evaluation of (buf[0]^0xFF) could trap, invoking undefined behavior. (On such implementations, 'char' would necessarily be signed and have one trap representation.)
I don't think char is allowed to have trap representations. Unsigned char certainly isn't, but surely neither is signed char?
I don't see why not. Consider the case of ones' complement signed char, in which 11111111 ("negative zero") is a trap representation. Now make plain char signed. :)
I believe that it's also possible to have SCHAR_MAX equal 127
and SCHAR_MIN equal -127, when CHAR_BIT is greater than 8.
--
pete
In <c2*********@enews4.newsguy.com> Chris Torek <no****@torek.net> writes: The operands of the "^" operator are subject to the usual arithmetic promotions (and constrained to be integers, hence really just the integer promotions).
Chris, please use the C terminology correctly.
1. There is no such thing as "usual arithmetic promotions".
2. The integer promotions are limited to types shorter than int. Beyond
that, it is the "usual arithmetic conversions" that operate, even in a
purely integer expression.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
>In <c2*********@enews4.newsguy.com> Chris Torek <no****@torek.net> writes: The operands of the "^" operator are subject to the usual arithmetic promotions (and constrained to be integers, hence really just the integer promotions).
In article <news:c2**********@sunnews.cern.ch>
Dan Pop <Da*****@cern.ch> writes:Chris, please use the C terminology correctly.
1. There is no such thing as "usual arithmetic promotions".
2. The integer promotions are limited to types shorter than int. Beyond that, it is the "usual arithmetic conversions" that operate, even in a purely integer expression.
Sorry, have not been getting much sleep lately. I knew something was
off even as I wrote that -- but I figured it had been at least a day
and nobody had yet pointed out that the expression did not actually
*work*. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: lkrubner |
last post by:
I'm worried about idiot users that write long essays in Microsoft Word,
then log into their accounts and bring up an HTML form and copy and
paste the essay and hit submit. Or perhaps they do this...
|
by: Beeeeeves |
last post by:
Is there a quick way to find the index of the first character different in
two strings?
For instance, if I had the strings
"abcdefghijkl"
and
"abcdefxyz"
I would want the return value to be...
|
by: Joseph Lee |
last post by:
Hi All,
I am having problem when i am using hashtable to keep an array of bytes
value as keys.
Take a look at the code snippet below
---------------------------------------------------
...
|
by: james |
last post by:
Hi, I am loading a CSV file ( Comma Seperated Value) into a Richtext box. I have a routine that splits the data up when it hits
the "," and then copies the results into a listbox. The data also...
|
by: Marcel Saucier |
last post by:
Hello, I want to use the above characters codes chart but I dont know how to
set the typeface (documentation: The characters that appear in Windows above
127 depend on the selected typeface).
...
| |
by: Manuel |
last post by:
hi,
I have a problem, a stupid problem. I can't declare a variable of type
byte.
The g++ said that i have syntactic error in this line. The code is
this: byte * variable;
well, i think...
|
by: Kulgan |
last post by:
Hi
I am struggling to find definitive information on how IE 5.5, 6 and 7
handle character input (I am happy with the display of text).
I have two main questions:
1. Does IE automaticall...
|
by: arnuld |
last post by:
i have created a solutions myself. it compiles without any trouble
and runs but it prints some strange characters. i am not able to find
where is the trouble.
...
|
by: rajash |
last post by:
Hello everyone,
Thanks again for all the suggestions, though I think some people are a
bit fussy in their answers.
Here is a solution to Exercise 1.14. It deals well with control
characters...
|
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,...
|
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...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |