Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 位运算?_C_Bit Manipulation - Fatal编程技术网

C 位运算?

C 位运算?,c,bit-manipulation,C,Bit Manipulation,我试图编写一个宏来检查特定的位范围是开还是关,用户需要输入数字、起始位和结束位。因此,如果用户输入十进制数31,2,4,它必须检查从2到4的所有位是否都是1,如果它们都是1,则返回true。如果它们不都是1,则返回false。在本例中,它们都是1,因此将返回true #define ALLON(X, S, E) //(MACRO HERE) 如何制作宏来执行此操作 类似这样: #define GET_MASK(S, E) (((1UL << (E - S + 1)) -

我试图编写一个宏来检查特定的位范围是开还是关,用户需要输入数字、起始位和结束位。因此,如果用户输入十进制数31,2,4,它必须检查从2到4的所有位是否都是1,如果它们都是1,则返回true。如果它们不都是1,则返回false。在本例中,它们都是1,因此将返回true

#define ALLON(X, S, E) //(MACRO HERE)
如何制作宏来执行此操作

类似这样:

#define GET_MASK(S, E)       (((1UL << (E - S + 1)) - 1) << S)

#define ALLOCN(X, S, E)      (((GET_MASK(S,E) & X) == GET_MASK(S,E)) ? 1 : 0)

#define GET_MASK(S,E)((1UL您可以通过一些基本的位移位和按位逻辑操作来实现这一点。这将实现:

#include <stdio.h>

#define ALLON(X,S,E) ((X & (~((~0U << E) | ~(~0U << (S - 1))))) == (~((~0U << E) | ~(~0U << (S - 1)))))

/*
    Or, simplifying with two macros...

    #define ALLMASK(S,E) (~((~0U << E) | ~(~0U << (S - 1))))
    #define ALLON(X,S,E) ((X & ALLMASK(S,E)) == ALLMASK(S,E))
*/

char * convert_to_8bit(char * buffer, const unsigned int n)
{
    size_t k = 0;
    for ( unsigned int i = 128; i > 0; i >>= 1 ) {
        if ( n & i ) {
            buffer[k++] = '1';
        }
        else {
            buffer[k++] = '0';
        }
    }
    return buffer;
}

int main(void)
{
    char buffer[9] = {0};

    printf("1 in binary is %s\n", convert_to_8bit(buffer, 1));
    printf("ALLON(1, 2, 4) is %s\n", ALLON(1, 2, 4) ? "true" : "false");
    printf("ALLON(1, 1, 1) is %s\n", ALLON(1, 1, 1) ? "true" : "false");

    printf("30 in binary is %s\n", convert_to_8bit(buffer, 30));
    printf("ALLON(30, 2, 4) is %s\n", ALLON(30, 2, 4) ? "true" : "false");
    printf("ALLON(30, 1, 1) is %s\n", ALLON(30, 1, 1) ? "true" : "false");
    printf("ALLON(30, 5, 5) is %s\n", ALLON(30, 5, 5) ? "true" : "false");
    printf("ALLON(30, 5, 6) is %s\n", ALLON(30, 5, 6) ? "true" : "false");

    return 0;
}
简单地说,假设我们在做ALLON(31,2,4):


  • ~0U@DoxyLover的可能重复项:它不需要在某个范围内进行迭代。@2501-不同意这是它的重复项。您的链接问题涉及单个位。OP需要一个由起始位和结束位数字指定的位范围。@DoxyLover:使用位移位运算符。那么,嗯,这里有人能帮我吗?如果您只是按位和
    X
    使用掩码,如果设置了任何位,它的计算结果将为true,而不仅仅是在所有位都设置好的情况下。而且,这个算法不正确。用几个数字试试,你就会看到。很好,保罗!你是对的。我们必须检查掩码&X是否准确地为你提供了掩码!我相应地更新了答案
    paul@horus:~/src/sandbox$ ./bs
    1 in binary is 00000001
    ALLON(1, 2, 4) is false
    ALLON(1, 1, 1) is true
    30 in binary is 00011110
    ALLON(30, 2, 4) is true
    ALLON(30, 1, 1) is false
    ALLON(30, 5, 5) is true
    ALLON(30, 5, 6) is false
    paul@horus:~/src/sandbox$