Re: Convert 00010000 to 11110000......how?



farnaz.shahed@xxxxxxxxx writes:

This what I'm basically supposed to do. If i have a byte which looks
like this 00010000, i need to convert it to 11110000. That would mean
if any of the bit fields is 1....all the bit fields to the left of it
should be made 1.

If exactly one bit in x is set, and x is an unsigned int or
unsigned long, then
x = ~(x - 1);
should have that effect. But that'll set all the bits in x at or
to the left of the bit in question. If you only want that effect
for the low 8 bits, then you can do
x = (x - 1) ^ 0xff;
instead.

If more than one bit in x might be set, then I think the
following will work:
~((x ^ (x - 1)) >> 1)

I haven't tested any of this. They might not work. If they do,
there might be easier ways to do the same thing.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
.