Bitwise sequence inversion

Status
Not open for further replies.

checkmate

New Member
Just wondering, what's the most efficient C algorithm to do a bitwise sequence inversion of an 8-bit value?
 
what do you mean ?

Take the complement ? (00000001 => 11111110)
Code:
i = ~i;

or Reverse it ? (11001010 => 01010011)
Code:
unsigned char	BitReverse(unsigned char In)
{	
	int				Loop = 0;	
	unsigned char	Out = 0;
	
	while (1)
	{
		Out |= (In & 1);						//AND In with 00000001, and OR the result with Out
			if (Loop > 6)						//if we did all 8 bits then break
				break;
		In  >>= 1;								//shift bits in In 1 position to the right
		Out <<= 1;								//shift bits in Out 1 pos to the left
		Loop++;									//increase loop for next pass
	}
	return	Out;								//return the result	
}
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…