delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/02/04/02:14:35.1

Sender: nate AT cartsys DOT com
Message-ID: <36B8BF51.B6DC9E1B@cartsys.com>
Date: Wed, 03 Feb 1999 13:27:45 -0800
From: Nate Eldredge <nate AT cartsys DOT com>
X-Mailer: Mozilla 4.08 [en] (X11; I; Linux 2.0.36 i586)
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: what is wrong with this?
References: <Pine DOT LNX DOT 3 DOT 91 DOT 990201164419 DOT 5366B-100000 AT aditya DOT unigoa DOT ernet DOT in> <15708 DOT 990203 AT flashback DOT net>
Reply-To: djgpp AT delorie DOT com

anarko wrote:
> 
> Hello everybody, i have a little problem,
> i wrote a little routine that resets a specific
> bit in a char, and it worked fine. then i just wanted
> to reduce the lines of code it was written in
> to one line or something and it doesnt work correct,
> 
> i reduced it to:
> bittmp = tmp << 2;
> bittmp = ((tmp >> 7) << 7) | (bittmp >> 2);
> 
> without problems, but when i wrote it to:
> bittmp = ((tmp >> 7) << 7) | ((tmp << 2) >> 2);
				^^^^^^^^^^
Although your variables are unsigned char, intermediate calculations are
still done as `int', as the language requires.  Thus, in `tmp << 2', the
high bits are not shifted off the end, and come right back when you
right-shift.

Either insert a cast to `unsigned char' (ugly), or mask this result with
0xff (better).
-- 

Nate Eldredge
nate AT cartsys DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019