Asked before but got closed due to lack of detail. So here is more info
I am working on cp without shift or rotate.
It supports
- add & subtract
- logical & and logical or
- conditional jump on > < == or any combination of those (>=…)
I can see how to shift left using repeated add to self. I can do the rotate by testing the leftmost bit before shift and oring it onto the rightmost after shift.
I cant figure out a right shift mechanism though
>Solution :
Here’s an example in C++ using only + and -:
template<class T>
T rightshift(T value, int steps) {
T div = 1;
while(steps--) div += div; // steps=1 => 2, 2=>4 etc.
T res = 0;
while(value >= div) { // subtract div until it's less than div
value -= div;
++res; // and count the number of times we subtract
}
return res;
}