Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Compiler bug?

Author: Dieter Buerssner

Date: 17:51:43 07/05/02

Go up one level in this thread


On July 05, 2002 at 05:23:41, Sune Fischer wrote:

>On July 04, 2002 at 19:04:19, Dieter Buerssner wrote:
>
>>On July 04, 2002 at 04:14:00, Sune Fischer wrote:
>>
>>>I'm curious, is 0xff then used as an 4 byte integer and not as a char?
>>
>>Your question cannot be answered for Standard C - it can only be answered for
>>specific implementations. Also, I think, in most cases, the answer will not be
>>needed anyway.
>
>It is very important to know when doing shift operations.
>This one must be another broken example: ((0xff<<x)>>x) (with the intent of
>clearing the first x bits in a signed byte), or simply (0xff<<x).

I believe, that your statement cannot be easily made portable. Perhaps it can be
done by clever usage of some constants in limits.h.

Why would one want to clear bits from a *signed* type? It seems your are
misusing a signed type here. You cannot do it, because the C-Standard does not
force 2-complements signed types. Also see Dann's notes about right shifting
signed types. (All compilers I know will use an arithmetic shift, however)

For unsigned types (say unsigned char) I would use something like:

  uc & (~((1U<<n)-1))

Or your method slightly modified and portable (and possibly calculating x
differently):

  ((uc<<x)&0xffff)>>x

If you assume 32 bit unsinged and native word length

  ((uc<<x)&0xffffffff)>>x

would probably produce the same code as your statement (the and will be
optimized away).

No, I didn't test it :-)

Regards,
Dieter




This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.