473,480 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

character/byte equality, again

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.
Nov 14 '05 #1
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

Nov 14 '05 #2
"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
Nov 14 '05 #3

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
Nov 14 '05 #4
>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.
Nov 14 '05 #5
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
Nov 14 '05 #6
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
Nov 14 '05 #7
>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.
Nov 14 '05 #8

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

Similar topics

5
1609
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...
15
2257
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...
7
6831
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 --------------------------------------------------- ...
18
4577
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...
18
8712
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). ...
20
224937
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...
44
9407
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...
8
4738
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. ...
35
4203
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...
0
7041
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,...
0
6908
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...
0
7084
jinu1996
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...
1
6739
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...
0
5337
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,...
1
4779
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...
0
4481
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...
0
2995
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...
1
563
muto222
php
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.