本文介绍了OR运算符用C不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么最后的printf 在下面的code未打印255。

 字符℃;
C = C&安培; 0;
输出(C的值为%d,(int)的三);
INT J = 255;
C =(C |百灵);
输出(C的值为%d,(int)的三);


解决方案

在大多数实现了字符类型有符号,所以从的取值范围 - 128 127

这意味着, 11111111 (这是 255 写成二进制)等于 -1 。 (由于这是psented存储在一个价值重估$ P $)

要得到你所期望的,你需要声明 C unsigned char型,就像这样:

  unsigned char型C = 0;
INT J = 255;
C =(C |百灵);
输出(C的值为%d,(int)的三);

I don't understand why the final printf in the code below is not printing 255.

char c;
c = c & 0;
printf("The value of c is %d", (int)c);
int j = 255;
c = (c | j);
printf("The value of c is %d", (int)c);
解决方案

In most implementations the char type is signed, so it ranges from -128 to 127.

This means, 11111111 (which is 255 written in binary) is equal to -1. (As it's represented as a value stored in two's complement)

To get what you expect, you need to declare c as a unsigned char, like so:

unsigned char c = 0;
int j = 255;
c = (c | j);
printf("The value of c is %d", (int)c);

这篇关于OR运算符用C不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 00:39