Bit Shifting Operators that Wrap

Jun 15, 2011 at 1:34am
01101001 << 3 = 01001011
01101001 >> 4 = 10010110
Sadly this is not true, the bits that go off the edge of the byte just get cut off. This is useful, though. However, wrapping around is also useful - your challenge is to come up with a use for shift left and shift right operators that wrap bits around, and then actually write functions for them that do it like above. Choose any variable type you prefer; char, short, long, long long, etc. and use any method you prefer, even if it involves strings - though using only bitwise operators would be quite interesting to do :)

I myself accept this challenge, so I will respond with my own code once I have it working.
Jun 15, 2011 at 2:34am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//Uncomment if the sizes of all unsigned types are powers of two.
//#define POWER_SIZES

template <typename T>
T rotate_left(T x,T amount){
	const unsigned bits=sizeof(T)*8;
#if POWER_SIZES
	amount&=bits-1;
#else
	amount%=bits;
#endif
	if (amount)
		return (x<<amount)|(x>>(bits-amount));
	return x;
}

template <typename T>
T rotate_right(T x,T amount){
	const unsigned bits=sizeof(T)*8;
#if POWER_SIZES
	amount&=bits-1;
#else
	amount%=bits;
#endif
	if (amount)
		return (x>>amount)|(x<<(bits-amount));
	return x;
}
As usual, bitwise operations exhibit wonky behavior when performed on signed operands.
Last edited on Jun 15, 2011 at 2:40am
Jun 15, 2011 at 2:45am
1
2
3
4
5
6
7
8
9
int rol( int x, unsigned char b ) {
	asm ( "roll %1, %0" : "=g"(x) : "cI"(b) : );
	return x;
}

int ror( int x, unsigned char b ) {
	asm ( "rorl %1, %0" : "=g"(x) : "cI"(b) : );
	return x;
}


Jun 15, 2011 at 3:11am
closed account (3hM2Nwbp)
Excellent example of when inline assembly is appropriate, jsmith!
Jun 15, 2011 at 4:01am
I use bitwise operators and tail recursion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <bitset>
#include <string>

typedef unsigned char Byte;
typedef unsigned int Number;

Byte rotate_left(Byte b, Number n)
{
    if (n == 0) return b;

    return rotate_left((b << 1) | (b >> 7), (n & 7) - 1);
}

Byte rotate_right(Byte b, Number n)
{
    if (n == 0) return b;

    return rotate_right((b >> 1) | (b << 7), (n & 7) - 1);
}

int main()
{
    Byte b = std::bitset<8>(std::string("10000001")).to_ulong();

    b = rotate_right(b, 1000000000 + 2);

    std::cout << std::bitset<8>(b).to_string() << std::endl;

    b = rotate_left(b, 1000000000 + 4);

    std::cout << std::bitset<8>(b).to_string() << std::endl;

    return 0;
}

EDIT: It's slower, but cooler :D
Last edited on Jun 15, 2011 at 5:26am
Jun 15, 2011 at 5:15am
Of all these cool methods, mine is quite clunky and is a very direct approach. It also doesn't work correctly when shifting left...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>

template<typename T>
class BitWrapper
{
	T v;
public:
	BitWrapper() : v(0) {}
	BitWrapper(const BitWrapper<T>& bw) : v(bw.v) {}
	BitWrapper(const T& V) : v(V) {}
	BitWrapper<T>& operator=(const BitWrapper<T>& bw){ v = bw.v; return(*this); }
	BitWrapper<T>& operator=(const T& V){ v = V; return(*this); }
	operator T()const{ return(v); }

	BitWrapper<T>& operator<<=(const int& by) //Doesn't work correctly...
	{
		if(by < 0){ return((*this) >>= (0-by)); }
		else if(by != 0)
		{
			for(unsigned long i = 0; i < unsigned long(by); ++i)
			{
				T temp = v & (T(1) << (sizeof(T)*8 - 1));
				(v <<= 1) += (temp != 0 ? 1 : 0);
			}
		}
		return(*this);
	}
	BitWrapper<T> operator<<(const int& by)const
	{
		BitWrapper<T> temp (*this);
		return(temp <<= by);
	}
	BitWrapper<T>& operator>>=(const int& by)
	{
		if(by < 0){ return((*this) <<= (0-by)); }
		else if(by != 0)
		{
			for(unsigned long i = 0; i < unsigned long(by); ++i)
			{
				T temp = v & 1;
				(v >>= 1) += temp << (sizeof(T)*8 - 1);
			}
		}
		return(*this);
	}
	BitWrapper<T> operator>>(const int& by)const
	{
		BitWrapper<T> temp (*this);
		return(temp >>= by);
	}
};

template<typename T>
std::ostream& operator<<(std::ostream& out, const BitWrapper<T>& bw)
{
	for(unsigned long i = 0; i < sizeof(T)*8; ++i)
	{
		T on = T(bw) & (1 << i);
		out << (on != 0 ? '1' : '0');
	}
	return(out);
}

int main()
{
	const BitWrapper<unsigned char> bits = 150;
	std::cout << bits << " << 3 = " << (bits << 3) << std::endl;
	std::cout << bits << " >> 4 = " << (bits >> 4) << std::endl;

	std::cin.sync();
	std::cin.ignore();
}
01101001 << 3 = 00101101<--WRONG
01101001 >> 4 = 10010110
Jun 15, 2011 at 6:47am
I wanted to automatically generate the strings, but I don't really have the time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <bitset>

const char *transformations[]={
	"||||||||\n"
	"||||||||\n",

	"| | | | | | | |      \n"
	" \\ \\ \\ \\ \\ \\ x       \n"
	"  \\ \\ \\ \\ \\ x \\      \n"
	"   \\ \\ \\ \\ x \\ \\     \n"
	"    \\ \\ \\ x \\ \\ \\    \n"
	"     \\ \\ x \\ \\ \\ \\   \n"
	"      \\ x \\ \\ \\ \\ \\  \n"
	"       x \\ \\ \\ \\ \\ \\ \n"
	"      / \\ \\ \\ \\ \\ \\ \\\n"
	"      | | | | | | | |\n",

	"| | | | | | | |    \n"
	" \\ \\ \\ \\ \\ x /     \n"
	"  \\ \\ \\ \\ x x      \n"
	"   \\ \\ \\ x x \\     \n"
	"    \\ \\ x x \\ \\    \n"
	"     \\ x x \\ \\ \\   \n"
	"      x x \\ \\ \\ \\  \n"
	"     / x \\ \\ \\ \\ \\ \n"
	"    / / \\ \\ \\ \\ \\ \\\n"
	"    | | | | | | | |\n",

	"| | | | | | | |  \n"
	" \\ \\ \\ \\ x / /   \n"
	"  \\ \\ \\ x x /    \n"
	"   \\ \\ x x x     \n"
	"    \\ x x x \\    \n"
	"     x x x \\ \\   \n"
	"    / x x \\ \\ \\  \n"
	"   / / x \\ \\ \\ \\ \n"
	"  / / / \\ \\ \\ \\ \\\n"
	"  | | | | | | | |\n",

	"| | | | | | | |\n"
	" \\ \\ \\ x / / / \n"
	"  \\ \\ x x / /  \n"
	"   \\ x x x /   \n"
	"    x x x x    \n"
	"   / x x x \\   \n"
	"  / / x x \\ \\  \n"
	" / / / x \\ \\ \\ \n"
	"/ / / / \\ \\ \\ \\\n"
	"| | | | | | | |\n",

	"  | | | | | | | |\n"
	"   \\ \\ x / / / / \n"
	"    \\ x x / / /  \n"
	"     x x x / /   \n"
	"    / x x x /    \n"
	"   / / x x x     \n"
	"  / / / x x \\    \n"
	" / / / / x \\ \\   \n"
	"/ / / / / \\ \\ \\  \n"
	"| | | | | | | |  \n",

	"    | | | | | | | |\n"
	"     \\ x / / / / / \n"
	"      x x / / / /  \n"
	"     / x x / / /   \n"
	"    / / x x / /    \n"
	"   / / / x x /     \n"
	"  / / / / x x      \n"
	" / / / / / x \\     \n"
	"/ / / / / / \\ \\    \n"
	"| | | | | | | |    \n",

	"      | | | | | | | |\n"
	"       x / / / / / / \n"
	"      / x / / / / /  \n"
	"     / / x / / / /   \n"
	"    / / / x / / /    \n"
	"   / / / / x / /     \n"
	"  / / / / / x /      \n"
	" / / / / / / x       \n"
	"/ / / / / / / \\      \n"
	"| | | | | | | |      \n"
};

inline int read_left(int x){
	if (x<10)
		return x;
	x/=10;
	return x/10;
}

inline int read_right(int x){
	if (x<10)
		return x;
	x/=10;
	return x%10;
}

inline int cross_and_combine(int left,int right){
	//std::cout <<"cross_and_combine("<<right<<","<<left<<")="<<right*100+left*10<<"\n";
	return right*100+left*10;
}

void print_step(const std::vector<int> &v){
	for (size_t a=0;a<v.size();a++){
		int i=v[a];
		if (i<0)
			std::cout <<' ';
		else if (i<10)
			std::cout <<i;
		else if (i==10)
			std::cout <<'A';
		else if (i==100)
			std::cout <<'B';
		else if (i==110)
			std::cout <<'C';
		else
			std::cout <<' ';
		std::cout <<':';
	}
	std::cout <<'\n';
}

unsigned char rotate(unsigned char x,int amount){
	bool left;
	if (amount<0){
		left=0;
		amount=-amount;
	}else
		left=1;
	amount&=7;
	std::vector<std::string> strings;
	{
		std::stringstream stream(transformations[amount]);
		std::string line;
		while (std::getline(stream,line))
			strings.push_back(line);
	}
	if (!left){
		for (size_t a=0;a<strings.size();a++){
			std::string &i=strings[a];
			std::reverse(i.begin(),i.end());
			for (size_t b=0;b<i.size();b++){
				if (i[b]=='\\')
					i[b]='/';
				else if (i[b]=='/')
					i[b]='\\';
			}
		}
	}
	std::vector<int> step,
		next_step;
	for (size_t a=0;a<strings[0].size();a++){
		if (strings[0][a]=='|'){
			step.push_back(!!(x&0x80));
			x<<=1;
		}else
			step.push_back(-1);
	}
	print_step(step);
	for (size_t a=1;a<strings.size()-1;a++){
		std::string &s=strings[a];
		for (size_t z=0;z<s.size();z++)
			std::cout <<s[z]<<' ';
		std::cout <<std::endl;
		next_step.clear();
		for (size_t b=0;b<s.size();b++){
			switch (s[b]){
				case '\\':
					next_step.push_back(read_right(step[b-1]));
					break;
				case '/':
					next_step.push_back(read_left(step[b+1]));
					break;
				case 'x':
					next_step.push_back(cross_and_combine(read_right(step[b-1]),read_left(step[b+1])));
					break;
				default:
					next_step.push_back(-1);
			}
		}
		step=next_step;
		print_step(step);
	}
	x=0;
	{
		std::string &s=strings.back();
		for (size_t a=0;a<s.size();a++){
			if (s[a]=='|'){
				x<<=1;
				x|=step[a];
			}
		}
	}
	return x;
}

int main(){
	unsigned char n=172;
	std::cout <<std::bitset<8>(n).to_string()<<std::endl;
	n=rotate(n,-3);
	std::cout <<std::bitset<8>(n).to_string()<<std::endl;
	return 0;
}
Last edited on Jun 15, 2011 at 6:49am
Jun 15, 2011 at 6:56am
I hate you.
Jun 15, 2011 at 7:06am
:-)
I also thought about simulating a small electronic circuit with two sets of flip-flops arranged into two shift registers, but it seemed messy and I wasn't sure how to terminate it. The above would be the wiring connecting the two shift registers.
Topic archived. No new replies allowed.