Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!princeton!allegra!alice!ark From: ark@alice.UUCP Newsgroups: comp.sys.cbm Subject: Re: C-Power bug ??? Message-ID: <6448@alice.uUCp> Date: Mon, 8-Dec-86 12:33:42 EST Article-I.D.: alice.6448 Posted: Mon Dec 8 12:33:42 1986 Date-Received: Mon, 8-Dec-86 21:28:55 EST References: <1688@i.cc.purdue.edu> Distribution: net Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 37 Keywords: C-Power, bug In article <1688@i.cc.purdue.edu>, aig@i.cc.purdue.edu.UUCP writes: > > I am writing a game in C-Power (please hold all applause until the end > of the article) and have run into a bit of a problem. The example given includes the following code: > if ((j&1 == 1) && ((*pl1->y) > 50)) (*pl1->y)--; /* up */ > if ((j&2 == 2) && ((*pl1->y) < 226)) (*pl1->y)++; /* down */ > if ((j&4 == 4) && ((*pl1->x) > 24)) (*pl1->x)--; /* left */ > if ((j&8 == 8) && ((*pl1->x) < 250)) (*pl1->x)++; /* right */ The trouble with this code is that the expression j&1 == 1 doesn't do what you think, because == binds more tightly than &. Thus, this is equivalent to j&(1 == 1) which, unfortunately, has the same effect for the wrong reason. The trouble is that we have actually written j&1 because 1==1 evaluates to 1, so when we write j&2 == 2 we have actually written EXACTLY THE SAME THING. No wonder the results are confusing! Might I suggest a rewrite: if ((j&1) && *pl1->y > 50) --*pl1->y; /* up */ if ((j&2) && *pl1->y < 226) ++*pl1->y; /* down */ if ((j&4) && *pl1->x > 24) --*pl1->x; /* left */ if ((j&8) && *pl1->x < 250) ++*pl1->x; /* right */