【问题标题】:What is the difference between unsigned char and unsigned short in this case?在这种情况下,unsigned char 和 unsigned short 有什么区别?
【发布时间】:2012-10-12 16:18:15
【问题描述】:

我们需要将使用 unsigned char 作为位掩码的程序升级到使用 unsigned short 作为位掩码的新版本。我相信它们之间存在一些差异,因为我们的程序使用相同的逻辑失败,将 unsigned char 更改为 unsigned short。 (那是我们买的外部库。库升级了,所以我们也要改程序)。

Old version:
typedef struct SomeStruct {
    unsigned char   bit_mask;
#       define      SomeStruct_a_present 0x80
#       define      SomeStruct_b_present 0x40
#       define      SomeStruct_c_present 0x20
    X          x;
    Y          y;
    A          a;  /* optional; set in bit_mask
                                * SomeStruct_a_present if
                                * present */

    B          b;  /* optional; set in bit_mask
                                * SomeStruct_b_present if
                                * present */

    C          c;  /* optional; set in bit_mask
                                * SomeStruct_c_present if
                                * present */
} SomeStruct;



New version:
typedef struct SomeStruct {
    unsigned short   bit_mask;
#       define      SomeStruct_x_present 0x8000
#       define      SomeStruct_y_present 0x4000
#       define      SomeStruct_a_present 0x2000
#       define      SomeStruct_b_present 0x1000
#       define      SomeStruct_c_present 0x0800
    X          x;/* optional; set in bit_mask
                                * SomeStruct_x_present if
                                * present */
    Y          y;/* optional; set in bit_mask
                                * SomeStruct_y_present if
                                * present */
    A          a;  /* optional; set in bit_mask
                                * SomeStruct_a_present if
                                * present */

    B          b;  /* optional; set in bit_mask
                                * SomeStruct_b_present if
                                * present */

    C          c;  /* optional; set in bit_mask
                                * SomeStruct_c_present if
                                * present */
} SomeStruct;

我认为我们当前的行有一些问题,因为程序崩溃了:

我们当前设置位掩码的方法:

someStruct.bit_mask = SomeStruct_a_present;
someStruct.bit_mask |= SomeStruct_b_present;
someStruct.bit_mask |= SomeStruct_c_present;

【问题讨论】:

  • 程序在哪里崩溃?

标签: c bit-manipulation


【解决方案1】:

此行无符号短

someStruct.bit_mask = SomeStruct_a_present;

将位掩码的值设置为8192,但对于无符号字符,该值将设置为128

原因:

unsigned short 长度为 2 个字节,位掩码为 0010000000000000 (0x2000),但是对于 unsigned char,此值将是 10000000 (0x80)。

【讨论】:

  • 但是会影响 OR 操作吗?
  • @lamwaiman1988:- 是的,位位置不同。尝试在计算器程序员模式下进行操作,您会看到 :)
【解决方案2】:
 I believe there is some difference between( unsigned short and unsigned char)

sizeof(unsigned char) = 1 个字节。

sizeof(unsigned short) = 2 个字节。

【讨论】:

    【解决方案3】:

    “unsigned char”的长度为1 Byte,“unsigned short”的长度为2

    【讨论】:

      【解决方案4】:

      MODIFIERS部分下的链接here,您可以看到,

      sizeof(unsigned char) = 1 Byte and Range = 0 -> +255.

      sizeof(unsigned short) = 2 字节和范围 = 0 -> +65,535。

      Ideone Output

      【讨论】:

        猜你喜欢
        • 2022-01-03
        • 1970-01-01
        • 1970-01-01
        • 2014-05-03
        • 2016-07-07
        • 1970-01-01
        • 2020-10-08
        • 2015-01-03
        • 2022-12-15
        相关资源
        最近更新 更多