简体   繁体   English

a + b和char类型的值

[英]Value of a+b and char type

I am working in C++ and I had (as an exercise) to write on paper 2 answers. 我在C ++中工作,并且(作为练习)不得不在纸上写下2个答案。 The first question: if we have the following declarations and initialisations of variables: 第一个问题:我们是否具有以下变量的声明和初始化:

unsigned char x=250, z=x+7, a='8';

What is the value of the expression? 表达式的价值是什么?

z|(a-'0') // (here | is bitwise disjunction)

We have unsigned char, so the number z=x+7 is reduced mod 256, thus, after writing the numbers in binary, the answer is 9. 我们具有未签名的char,因此将z=x+7的数字减少为256,因此,在以二进制形式写入数字之后,答案为9。


The next question: a and b are int variables, a=1 and b=32767 . 下一个问题:a和b是int变量, a=1b=32767

The range of int is [-32768, 32767] . int的范围是[-32768, 32767] We don't have an unsigned type here. 我们这里没有未签名的类型。 My question is: what is the value of a+b ? 我的问题是: a+b的值是多少? How does this work with signed data types if the value of a certain variable is greater than the range of that data type? 如果某个变量的值大于该数据类型的范围,则如何处理带符号的数据类型?

The next question: a and b are int variables, a=1 and b=32767 . 下一个问题:a和b是int变量, a=1b=32767

[...]My question is: what is the value of a+b ? [...]我的问题是: a+b的值是多少?

Its undefined behavior . 不确定的行为 We cant tell you what it will be. 我们无法告诉您它将是什么。 We could make a reasonable guess but as far as C++ is concerned signed integer overflow is undefined behavior. 我们可以做出合理的猜测,但是就C ++而言,有符号整数溢出是未定义的行为。

There is no operator+(unsigned char, unsigned char) in C++, it first promotes these unsigned char arguments to int and only then does the addition, so that the type of the expression is int . C ++中没有operator+(unsigned char, unsigned char) ,它首先将这些unsigned char参数提升为int ,然后再进行加法运算,因此表达式的类型为int

And then that int whose value is too big to fit in unsigned char gets converted to unsigned char . 然后将该int以适应其值太大, unsigned char被转换为unsigned char

The standard says: 该标准说:

A prvalue of an integer type can be converted to a prvalue of another integer type. 整数类型的prvalue可以转换为另一种整数类型的prvalue。 A prvalue of an unscoped enumeration type can be converted to a prvalue of an integer type. 可以将无作用域枚举类型的prvalue转换为整数类型的prvalue。 If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2**n where n is the number of bits used to represent the unsigned type). 如果目标类型是无符号的,则结果值是与源整数一致的最小无符号整数(取模2 ** n,其中n是用于表示无符号类型的位数)。 [ Note: In a two's complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). [注意:在二进制补码表示中,此转换是概念性的,并且位模式没有任何变化(如果没有截断)。 — end note ] —尾注]

For the second question, the answer is undetermined. 对于第二个问题,答案是不确定的。

You can verify it yourself like this : 您可以这样验证自己:

#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 32767;
    int c = a+b;
    cout << c << endl;
}

The result will depend on your machine. 结果将取决于您的计算机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM