Bug fix for Turbo C ?

Another casualty of applied metaphysics. eychaner at suncub.bbso.caltech.edu
Sun Jun 30 05:05:05 AEST 1991


mouse at thunder.mcrcim.mcgill.edu (der Mouse) writes:
>The New Testament says, on page 206 (A7.8, Shift Operators):
>
>	The value of E1>>E2 is E1 right-shifted E2 bit positions.  The
>	right shift is equivalent to division by 2^E2 if E1 is unsigned
>	or if it has a non-negative value; otherwise the result is
>	implementation-defined.

OK, I'm sorry, I just had to post on this one, since this, then, creates a
problem for me (someday my code will break).  I have the following piece of
code (Please understand that this is just a fragment; any unwanted errors
are probably my fault, and some things are the way they are for reasons I
don't care to go into.):

short a_250K_array[];				/* The big array */
int bit_shift,					/* Bit shift */
    array_size, i;				/* Array size and index */

for (i = 0; i < arraysize; i++) {
    a_250K_array[i] >>= bit_shift;
    /* I shouldn't index the array like this; but it makes the code clearer */
    }

What I want is to divide each member of the 250K short array by 2^bit_shift.
The 250K array contains both positive and negative numbers.

In Vax C 3.0 (it's not the greatest, but it's adequate, OK?) section 7.5.7
SEEMS to guarantee that this works by filling the vacated bits with a copy of
E1's sign bit (and indeed, from experience, it does work as expected). However,
someday (dream on) Vax C might become ANSI standard, and I will need to change
the way I do this.  (Here, portability isn't a big issue, but having a system
upgrade break code might be.)

One qualifier: this loop must be FAST.  And in my experience (so far),

    a_250K_array[i] /= (1 << bit_shift);

isn't as fast as the first method.  Is this the only ANSI legal way to do what
I want to do?  AARGH!

This particular section of the standard puzzles me; why impose this seemingly
unnecessary constraint on the shift operators?  (Five thousand people will now
write me explaining in great detail why it is necessary.)

-G.
******************************************************************************
Glenn Eychaner - Big Bear Solar Observatory - eychaner at suncub.bbso.caltech.edu
"There is no monopoly of common sense / On either side of the political fence"
                                                            -Sting, "Russians"



More information about the Comp.lang.c mailing list