Mail Archives: djgpp/1999/02/04/02:14:35.1
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 -