whose cc recognises byte moves

[...]

But if you shift unsigned integers, you *know* what you'll get on

*any* (conforming) implementation. "It works on *my* system" is generally regarded as a poor argument. ;-)

The two types of applications I can think of (off the top of my head) that require shifting and have no excuse for not being portable are communications, cryptography and numeric translation. Other than that, I guess it's used mostly for I/O, which doesn't always have to be portable.

"Undefined behavior" can be *anything*. It can format your hard drive, start WWIII, make demons fly from your nose, or do exactly what you expect.

[...]

I don't think I snipped anything...?

Even better: strncat is guaranteed to terminate the destination string, but strncpy is not. And strncat may actually append n+1 characters to the destination string, while strncpy will never write more than n.

Regards,

-=Dave

--
Change is inevitable, progress is not.
Reply to
Dave Hansen
Loading thread data ...

Again, my point is that it isn't signed vs. unsigned that's the major concern here. Shifting of /any/ type is hazardous...

[snip]

I recall making a remark that a char is a char unless it's an int...

Argh!

Reply to
Everett M. Greene

Could you please provide some examples of computer architectures and compilers, in which shifting of _unsigned_ quantities would produce "unexpected" results.

The only example I can think of are the decimal (BCD) computers, but I have not seen those since the punched card era and I very much doubt that any of these would have a C-compiler (COBOL more likely).

However, shifting signed quantities is much more risky situation, with

2's complement, 1's complement and sign/magnitude representation.

Paul

Reply to
Paul Keinanen

int x;

x = 1

Reply to
Everett M. Greene

If int 16 bits, then the direct assignment would not work any better, since result can not be used anyway. The problem is that the int data type itself is not portable. Thus it is not much sense to blame the shift optation for portability problems.

Paul

Reply to
Paul Keinanen

... snip ...

Try strlcpy and strlcat, as defined by BSD. You can find an implementation at:

which also has references to the original analysis and reasonings. These routines are designed to avoid silly errors on the part of the user.

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
     USE worldnet address!
Reply to
CBFalconer

How about: long int x;

x = 1

Reply to
David Brown

... and of completely lacking all the others. And Java's (16-bit) "char" is not really a uint16_t --- unless memory betrays me, it completely lacks arithmetic operators working on it. If you can't multiply it by 17, it doesn't qualify as an integer data type.

Care to enlighten what could possibly be "two's-complement" about an

*unsigned* integer type?
--
Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
Reply to
Hans-Bernhard Broeker

... snip ...

The // is also undefined unless you have a C99 compiler. At any rate, the following is *always* defined, and is also a compile time constant:

x = 1L

Reply to
CBFalconer

If you ask this question in comp.lang.c. I am sure they will give you quite a number of examples. I once asked why there was no rotate instruction in C, because all CPUs I have worked with had rotate. They came back with some very odd computer architectures for which C is used where rotate is impossible to impliment.

Regards Anton Erasmus

Reply to
Anton Erasmus

Java compiles { char ch = 13; ch *= 17; ... } without error. Lossless conversion from and lossy conversion to int occurs implicitly, much as in C.

Perhaps you're reminding us that an expression like ((ch < 31) * 17) is a compile-time error in Java. Promotion to int from boolean in Java is not implicit: in Java that intent has to appear explicitly e.g. (((ch < 31) ? 1 : 0) * 17). In this sense, the Java boolean type is not a one-bit unsigned integral type.

"All the nonnegative encodings, of course", is my first thought. I wonder if I have misunderstood the question. In review, I think the JLS says what I meant better than I did e.g. in JLS2 4.2:

"The integral types are byte, short, int, and long, whose values are

8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing Unicode characters."

That text precisely avoids restating that "everyone knows" the values of "a 16-bit unsigned integer" are x0000 thru xFFFF inclusive.

Aye, apparently on purpose.

I see Google doesn't immediately remind me who first said, in design, sometimes less is more.

Pat LaVarre

Reply to
Pat LaVarre

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.