Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cshift function in C?

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

I am trying to find an equivalent to Fortran Cshift function in C/C++ can someone please help?

Thanks!
 
I am not sure if the shift direction is correct but this is the basic idea to do a circular shift. Please keep in mind that the function assumes that the number of bits to shift will be in the range 0-31. You can allow any value but implimenting the % operator on shift and mod by 31 (i think).

Matt

Code:
int CShift(int value, int shift)
{
	int high = 0;
	int low = 0;
	if(shift<0)
	{
		 // left shift
		int sh = shift*-1;
		high = value<<sh;
		low = value>>(32-sh);
	}
	else
	{
		// right shift
		low = value>>shift;
		high =  value<<(32-shift);
	}
	return high | low;
}

Matt
 
Actually, looking at that, i dont think it works 100%. Please test the code.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top