我正在做作业


asr-算术右移数字1,数字2的位向右(请参见下文)
rol-将数字1,数字2的位向左旋转(请参见下文)
ror-将数字1,数字2的位向右旋转


我在弄清楚如何做这部分时遇到了麻烦

这是我到目前为止所拥有的

int main()
{
    char n[90];
    char method[90];
    unsigned int num1;
    int methodlength;
    unsigned int num2;
    int d;
    unsigned int answer;

    printf("Type in an expression: ");

    gets(n);

    sscanf(n, "%x %s %x", &num1, method, &num2);



    if(strcmp(method, "asr") == 0)
    {
        printf("The answer is: %x", answer);
    }

    else if(strcmp(method, "rol") == 0)
    {
        printf("The answer is: %x", answer);
    }

    else if(strcmp(method, "ror") == 0)
    {
        printf("The answer is: %x", answer);
    }


}

最佳答案

您可以使用位屏蔽在位上播放。

您可能会发现以下链接很有用。
1. Wikipedia : Mask
2. Topcoder: Bitmasking

例如ROL可以通过

unsinged int arg = something;
unsigned int howmanytimes = some_other_thing;
unsigned int answer = (arg << howmanytimes) | (arg >> (sizeof(arg) * CHAR_BIT - howmanytimes));


在学习了一些有关屏蔽的知识之后,您应该能够理解该示例并自己编写其余的操作。不过,如果您不了解任何特定内容,请随时发表评论和提问。

关于c - 你如何旋转钻头,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25198600/

10-11 22:50
查看更多