Mail Archives: djgpp/1999/02/03/11:03:31
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);
it did no longer work (it returned 11111111 binary, as i inputted in
it)
the code is below if someone wants to take a peek at it:
#include <stdio.h>
#include <stdlib.h>
void ToBin8(unsigned char numb);
int main ()
{
unsigned char tmp;
unsigned char bittmp;
tmp=0xFF; //11111111 in binary
ToBin8(tmp);
printf("\n");
//reset bit 6
bittmp = tmp << 2;
bittmp = ((tmp >> 7) << 7) | (bittmp >> 2);
// bittmp = ((tmp >> 7) << 7) | ((tmp << 2) >> 2); //this
line isnt working
ToBin8(bittmp);
printf("\n");
return 0;
}
void ToBin8(unsigned char numb)
{
unsigned char cnt,a;
int tmp;
cnt = 128;
for(a=0; a<8;a++)
{
tmp = numb - cnt;
if (tmp >= 0)
{
printf("1");
numb = tmp;
}
else printf("0");
cnt = cnt / 2;
}
} /* ToBin8(uchar numb) */
- Raw text -