Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to shift right with only + – & | and tests for >, < ==

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • 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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading