/*
Quote from: C++: A Beginner's Guide, Second Edition
# Publisher: McGraw-Hill Osborne Media; 2 edition (December 3, 2003)
# Language: English
# ISBN-10: 0072232153
# ISBN-13: 978-0072232158
*/
#include <iostream>
using namespace std;
unsigned char rrotate(unsigned char val, int n);
void show_binary(unsigned int u);
int main()
{
char ch = 'T';
cout << "Original value in binary:\n";
show_binary(ch);
cout << "Rotating right 8 times:\n";
for(int i=0; i < 8; i++) {
ch = rrotate(ch, 1);
show_binary(ch);
}
return 0;
}
// Right-rotate a byte n places.
unsigned char rrotate(unsigned char val, int n)
{
unsigned int t;
t = val;
// First, move the value 8 bits higher.
t = t << 8;
for(int i=0; i < n; i++) {
t = t >> 1;
/* If a bit shifts out, it will be in bit 7
of the integer t. If this is the case,
put that bit on the left side. */
if(t & 128)
t = t | 32768; // put a 1 on left end
}
/* Finally, move the result back to the
lower 8 bits of t. */
t = t >> 8;
return t;
}
// Display the bits within a byte.
void show_binary(unsigned int u)
{
int t;
for(t=128; t>0; t = t/2)
if(u & t) cout << "1 ";
else cout << "0 ";
cout << "\n";
}
Original value in binary:
0 1 0 1 0 1 0 0
Rotating right 8 times:
0 0 1 0 1 0 1 0
0 0 0 1 0 1 0 1
1 0 0 0 1 0 1 0
0 1 0 0 0 1 0 1
1 0 1 0 0 0 1 0
0 1 0 1 0 0 0 1
1 0 1 0 1 0 0 0
0 1 0 1 0 1 0 0