On Fri, Feb 20, 2009 at 2:43 PM, rodavilaca <[email protected]> wrote:
> Hi,
>
> First, sorry for my English.
>
> I want to use operator << or >> to run a bit all positions of a char
> variable. My problem is when the bit reaches the end of the variable.
> When I use the operator again, the variable char clear all bits of the
> variable.
> Example:
> c = 1 (00000001)
> c = c <<1;
> then c = 2 (00000010)
> c = c <<1;
> then c = 4 (00000100)
> ...
> then c = 128 (10000000)
> c <<1;
> then c = 0 (00000000)
>
> I want in this time that the variable c is not zero but return c = 1,
> to begin the process ...
> How to do this without having to check the condition c == 128 and then
> do c = 1?
> Is there some automated way?

Even with your example, it's not clear what you're trying to do; is it
a loop? Keep going until you've moved the last bit out?

c = 1; // or whatever
while ( c <<= 1){
   // do whatever with c
}
// reaches this point when the last bit is cleared.

Keep going until there's a 1 in the leftmost position? (i.e. same as
before but one less iteration):
while ( (c <<= 1) && (c << 1)){
//...
}

-- 
PJH

http://shabbleland.myminicity.com/ind
http://www.chavgangs.com/register.php?referer=9375

Reply via email to