GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Jan-2008, 05:46
ajbharani's Avatar
ajbharani ajbharani is offline
New Member
 
Join Date: Dec 2007
Posts: 24
ajbharani has a little shameless behaviour in the past
Smile

Rotate bits!!


hey frenz

I'm looking for an efficient code for rotating bits using >> and << operators.. ( not just shift but rotate!!) Cud u help!!

thnx

Bharani
  #2  
Old 04-Jan-2008, 08:03
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Rotate bits!!


Maybe something like this:

CPP / C++ / C Code:
#include <iostream>
#include <string>
using namespace std;


unsigned char RotateBits( unsigned char input )
{	int i; 
	unsigned char output = 0;
  
	for(i=0;i<8;++i)
	{	output <<= 1;
		if(input%2) output++;
		input >>= 1;
	} 

	return output;
}

string ByteToString( unsigned char byte )
{	int i;
	string result = "";
	unsigned char mask = 0x80;

	for(i=0;i<8;i++)
	{	result = result + ((byte&mask)? "1" : "0");
		mask >>= 1;
		if(i == 3) result = result + " ";
	}
	return result;
}

int main()
{	cout << endl;
	
	
	
	int i;
	for(i=0;i<256;i++)
	{	cout << "RotateBits( " << ByteToString(i) << " ) = " << ByteToString(RotateBits(i)).c_str() << endl;
	}
	


	cout << endl;
	getchar();
	return 0;
}
  #3  
Old 04-Jan-2008, 10:58
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Rotate bits!!


Quote:
Originally Posted by ajbharani
hey frenz

I'm looking for an efficient code for rotating bits using >> and << operators.. ( not just shift but rotate!!)

CPP / C++ / C Code:
unsigned char RotateLeft(unsigned char c)
{
    return (c << 1) | ((c >> 7) & 1);
}

unsigned char RotateRight(unsigned char c)
{
    return (c >> 1) | ((c & 1) << 7);
}

Output for a few values:

Code:
Original Left Right ------------------------------- 0000 0001 0000 0010 1000 0000 0000 0010 0000 0100 0000 0001 0000 0011 0000 0110 1000 0001 0110 1101 1101 1010 1011 0110 0110 1110 1101 1100 0011 0111 0110 1111 1101 1110 1011 0111 0111 0000 1110 0000 0011 1000 0111 0001 1110 0010 1011 1000 0111 1110 1111 1100 0011 1111 0111 1111 1111 1110 1011 1111 1000 0000 0000 0001 0100 0000 1000 0001 0000 0011 1100 0000 1000 0010 0000 0101 0100 0001 1000 0011 0000 0111 1100 0001

Regards,

Dave

Regards,

Dave
  #4  
Old 04-Jan-2008, 14:45
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Rotate bits!!


Whoops, the example I gave was for reversing bits. Sorry.
  #5  
Old 04-Jan-2008, 14:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Rotate bits!!


Quote:
Originally Posted by fakepoo
the example I gave...
Not a bad example for bit-reversing using shift operators. The subject does come up from time to time.

Regards,

Dave
  #6  
Old 04-Jan-2008, 21:05
ajbharani's Avatar
ajbharani ajbharani is offline
New Member
 
Join Date: Dec 2007
Posts: 24
ajbharani has a little shameless behaviour in the past

Re: Rotate bits!!


hey,,,

thnx
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
BMP image on matrix load nemo C++ Forum 21 17-Dec-2007 23:04
Rotate Array cable_guy_67 C++ Forum 2 11-Jun-2007 06:21
Binary number systems: BCD, twos complement, ones complement, etc. machinated Miscellaneous Programming Forum 6 08-Feb-2006 10:51
link error? pablowablo C++ Forum 14 19-Jun-2004 10:00
problems typecasting an int to a string holbrook C++ Forum 9 07-May-2004 18:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:44.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.