C语言bit赋值

时间:2022-12-28 16:48:27
我在用C语言写编码的程序,当某个参数的值为P时,要向解码其传递两个比特的数11.
请问在C语言中如何产生这个两个比特的数。

7 个解决方案

#1


你可以传一个字节呀,然后取出你要的两位就好了呀。比如你想取出低两位,可以这样(0x67 & 0x03)

#2


仅供参考:
#include <stdio.h>

#define WIRTE_UINT(des ,x ,pos ,len)  (                                   \
    (des)                                                                 \
    &                                                                     \
       ~(                                /* 除要修改的位为0,其余都为1 */ \
            (                                                             \
                (1<<(len))-1             /* 长度为len位的1             */ \
            )                                                             \
            <<(32-(pos)-(len))           /* 左移32-(pos)-(len)位       */ \
        )                                                                 \
    |                                                                     \
        (                                                                 \
            (x)                                                           \
            <<(32-(pos)-(len))                                            \
        )                                                                 \
)
#define READ_UINT(src ,pos ,len)      (                                   \
    (                                                                     \
        (                                                                 \
            (unsigned)(src)                                               \
        )                                                                 \
        >>(32-(pos)-(len))               /* 逻辑右移32-(pos)-(len)位   */ \
    )                                                                     \
    &                                                                     \
    (                                                                     \
        (1<<(len))-1                     /* 长度为len位的1             */ \
    )                                                                     \
)

int main()                              //                1111111111222222222233
{                                       //bit   01234567890123456789012345678901
    unsigned int des = 0x661;           //      00000000000000000000011001100001
    unsigned int r;

    des = WIRTE_UINT(des ,3 ,28 ,2);    //      00000000000000000000011001101101
    printf("des==0x%08X\n",des);
r = READ_UINT(des ,28 ,2);
    printf("r==%d\n",r);
    return 0;
}
//des==0x0000066D
//r==3

#3


#include <stdio.h>

struct num {
    unsigned int a:2;
};
int main(void)
{

    struct num n;
    n.a = 3;

    printf("%x\n", n.a);
    return 0;
}

试试类似这样的。

#4


如果你是用VS2015,可以这样
int a = 0b11;

#5


传递
int a = 3; // 二进制11
解码
int a1 = a & 0x10; // 取高位
int a2 = a & 0x01;// 取低位

#6


&    |   操作可以提出你想要的任何一位上的值。

#7


用0x3&数就是两位。

#1


你可以传一个字节呀,然后取出你要的两位就好了呀。比如你想取出低两位,可以这样(0x67 & 0x03)

#2


仅供参考:
#include <stdio.h>

#define WIRTE_UINT(des ,x ,pos ,len)  (                                   \
    (des)                                                                 \
    &                                                                     \
       ~(                                /* 除要修改的位为0,其余都为1 */ \
            (                                                             \
                (1<<(len))-1             /* 长度为len位的1             */ \
            )                                                             \
            <<(32-(pos)-(len))           /* 左移32-(pos)-(len)位       */ \
        )                                                                 \
    |                                                                     \
        (                                                                 \
            (x)                                                           \
            <<(32-(pos)-(len))                                            \
        )                                                                 \
)
#define READ_UINT(src ,pos ,len)      (                                   \
    (                                                                     \
        (                                                                 \
            (unsigned)(src)                                               \
        )                                                                 \
        >>(32-(pos)-(len))               /* 逻辑右移32-(pos)-(len)位   */ \
    )                                                                     \
    &                                                                     \
    (                                                                     \
        (1<<(len))-1                     /* 长度为len位的1             */ \
    )                                                                     \
)

int main()                              //                1111111111222222222233
{                                       //bit   01234567890123456789012345678901
    unsigned int des = 0x661;           //      00000000000000000000011001100001
    unsigned int r;

    des = WIRTE_UINT(des ,3 ,28 ,2);    //      00000000000000000000011001101101
    printf("des==0x%08X\n",des);
r = READ_UINT(des ,28 ,2);
    printf("r==%d\n",r);
    return 0;
}
//des==0x0000066D
//r==3

#3


#include <stdio.h>

struct num {
    unsigned int a:2;
};
int main(void)
{

    struct num n;
    n.a = 3;

    printf("%x\n", n.a);
    return 0;
}

试试类似这样的。

#4


如果你是用VS2015,可以这样
int a = 0b11;

#5


传递
int a = 3; // 二进制11
解码
int a1 = a & 0x10; // 取高位
int a2 = a & 0x01;// 取低位

#6


&    |   操作可以提出你想要的任何一位上的值。

#7


用0x3&数就是两位。