当前位置:   article > 正文

C语言:移位操作注意事项_c语言 移位操作 不加 强转

c语言 移位操作 不加 强转

 移位操作:因为运算符优先级的原因,注意加括号。还有没必要在移位的时候进行(uint32_t)转换。

测试程序如下: 

  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. int main()
  6. {
  7. uint8_t buffer[4] = {0x11, 0x22, 0x33, 0x44};
  8. uint32_t value = buffer[0] + (buffer[1] << 8) + (buffer[2] << 16) + (buffer[3] << 24);
  9. printf("value = %x\n", value);
  10. return 0;
  11. }

 

  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. int main()
  6. {
  7. uint8_t buffer[4] = {0x11, 0x22, 0x33, 0x44};
  8. uint32_t value = buffer[0] + buffer[1] << 8 + buffer[2] << 16 + buffer[3] << 24;
  9. printf("value = %x\n", value);
  10. return 0;
  11. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/602833
推荐阅读
相关标签
  

闽ICP备14008679号