Shifting question

Arun Arya arya at hawk.ulowell.edu
Wed Jul 20 13:34:57 AEST 1988


In article <1818 at spar.SPAR.SLB.COM> hunt at spar.UUCP (Neil Hunt) writes:
>This has been a pet peeve of mine for a long time: does the following function
>work for applying a shift (left or right) to all pixels in a large image ?
>
>shift_pixel(image, count) struct image *image; int count;
>{
>	int i, j;
>
>	for(j = 0; j < image->rows; j++)
>		for(i = 0; i < image->cols; i++)
> ...
>			if(count > 0)
>				image->pixels[i][j] >>= count;
>			else
>				image->pixels[i][j] <<= -count;
>
>because of the stupid undefined rule about shifts.
>
>An alternative solution is 
> ...

What about a simple

shift_pixel(image, count) struct image *image; int count;
{
	int i, j;
	if(count > 0)      shift_right(image,count);
	else if(count < 0) shift_left (image,-count);
}

where the two shift routines are identical but for the fact one has a <<, other
a >>. It is often the case that attempts to merge code leads to slow code.
The fact that separate left and right shifts exist is useful in that
better code can be generated on machines that do them differently -
signed left shift coincides with unsigned left shift, but not right shifts.

>What a pain !!

Hardly! But for a decent preprocessor, the effort would be even smaller.
C is ugly, but for far more important reasons! Now if your count were a
two dimensional array ..., but such a case in practice would be surprising
to me.



More information about the Comp.lang.c mailing list