Path: utzoo!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!ames!necntc!dandelion!ulowell!hawk.ulowell.edu!arya From: arya@hawk.ulowell.edu (Arun Arya) Newsgroups: comp.lang.c Subject: Re: Shifting question Message-ID: <8167@swan.ulowell.edu> Date: 20 Jul 88 03:34:57 GMT References: <705@bnr-rsc.UUCP> <11556@steinmetz.ge.com> <60290@sun.uucp> <1818@spar.SPAR.SLB.COM> Sender: news@swan.ulowell.edu Reply-To: arya@hawk.ulowell.edu (Arun Arya) Organization: University of Lowell, CS Dept. Lines: 41 In article <1818@spar.SPAR.SLB.COM> hunt@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.