类型转换和位操作之谜

ayu*_*pta 2 c casting bit-manipulation

#include <stdio.h>
#include <stdint.h>
typedef unsigned char       uint8_t;
typedef short               int16_t;
typedef unsigned short      uint16_t;
typedef int                 int32_t;
typedef unsigned int        uint32_t;
int main(){
    uint8_t ball; 
    uint8_t fool;
    ball=((unsigned char)13);
    fool=((unsigned char)14);
    uint16_t combined_value1=((uint16_t)ball)<<12+((uint16_t)fool)<<8; // WRONG ONE
    uint16_t combined_value2=((uint16_t)ball<<12)+((uint16_t)fool<<8);
    printf("%hu\n",(uint16_t)combined_value1);
    printf("%hu\n",(uint16_t)combined_value2);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么"combined_value1"的值错了?这里球和傻瓜从0到15取值,我试图将combined_value连接为{ball [4 bits]:傻瓜[4位]:[8个零位]}.

Ale*_*exD 5

+具有更高的优先级<<,所以

((uint16_t)ball)<<12+((uint16_t)fool)<<8;
Run Code Online (Sandbox Code Playgroud)

被评估为

((uint16_t)ball) << (12+((uint16_t)fool)) << 8;
Run Code Online (Sandbox Code Playgroud)

请注意,(uint16_t)在此促销活动int发生之后,铸造在这方面没有多大意义.相反,考虑一下

uint16_t combined_value = (uint16_t)((ball<<12) + (fool<<8));
Run Code Online (Sandbox Code Playgroud)

还有其他一些多余的铸件.正如@StoryTeller建议的那样,包括更好stdint.h.