On 14 Jul 2004 09:06:36 GMT, Daniel Fischer <spam@danny.homeunix.net>
wrote in comp.lang.c:
[color=blue]
> An @ 2004-07-14:[color=green]
> > I need to test the elements of an array of chars for non-negativity.
> > I've read the following: "On most architectures testing for
> > non-negativity can be done several bytes at a time." Is this possible
> > with C as well?[/color]
>
> I'd imagine you could map the sign bit of a couple of chars into a
> different type in order to check for any of them being one, which
> would mean one of the elements is negative.
>
> However the machine independent solution does not gain anything
> over a straight implementation, except if you're using a very
> intelligent compiler, but relying on a specific compiler wouldn't
> be independent anymore so...
>
>
>
> A machine dependent solution, which I'm posting for the purely
> academic reason of informing people what they shouldn't do, could
> look similar to this:
>
> if(*(unsigned short *)(a+i) & 0x8080)[/color]
No, it is not machine dependent as far as C is concerned. It is
undefined behavior to access objects with an lvalue of a different
type, with the exception of accessing any object as an array of
unsigned characters.
It could be doubly undefined if a+i does not have the proper alignment
for pointer to short.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html