Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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.

Latest threads

New Articles From Microcontroller Tips

Back
Top