如果我为无符号变量赋值为负值会怎样?

时间:2022-07-05 16:07:50

I was curious to know what would happen if I assign a negative value to an unsigned variable.

我很想知道如果我给无符号变量赋一个负值会发生什么。

The code will look somewhat like this.

代码看起来有点像这样。

unsigned int nVal = 0;nVal = -5;

It didn't give me any compiler error. When I ran the program the nVal was assigned a strange value! Could it be that some 2's complement value gets assigned to nVal?

它没有给我任何编译器错误。当我运行程序时,nVal被分配了一个奇怪的值!是否可以将某些2的补码值分配给nVal?

5 个解决方案

#1


53  

For the official answer - Section 4.7 conv.integral

正式答案 - 第4.7节conv.integral

"If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type). [ 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 ]

“如果目标类型是无符号的,则结果值是与源整数一致的最小无符号整数(模2n,其中n是用于表示无符号类型的位数)。[注意:在二进制补码表示中,此转换是概念性的,并且位模式没有变化(如果没有截断)。-end note]

This essentially means that if the underlying architecture stores in a method that is not Two's Complement (like Signed Magnitude, or One's Complement), that the conversion to unsigned must behave as if it was Two's Complement.

这实质上意味着如果底层架构存储的方法不是Two's Complement(如Signed Magnitude,或One的Complement),那么转换为unsigned必须表现得好像是Two's Complement。

#2


23  

It will assign the bit pattern representing -5 (in 2's complement) to the unsigned int. Which will be a large unsigned value. For 32 bit ints this will be 2^32 - 5 or 4294967291

它将表示-5(以2的补码)表示的位模式分配给unsigned int。这将是一个很大的无符号值。对于32位整数,这将是2 ^ 32 - 5或4294967291

#3


4  

It will show as a positive integer of value of max unsigned integer - 4 (value depends on computer architecture and compiler).

它将显示为max无符号整数值的正整数-4(值取决于计算机体系结构和编译器)。

BTW
You can check this by writing a simple C++ "hello world" type program and see for yourself

顺便说一句你可以通过编写一个简单的C ++“hello world”类型程序来检查这一点并自己查看

#4


4  

You're right, the signed integer is stored in 2's complement form, and the unsigned integer is stored in the unsigned binary representation. C (and C++) doesn't distinguish between the two, so the value you end up with is simply the unsigned binary value of the 2's complement binary representation.

你是对的,有符号整数以2的补码形式存储,无符号整数存储在无符号二进制表示中。 C(和C ++)不区分这两者,因此您最终得到的值只是2的补码二进制表示的无符号二进制值。

#5


0  

Yes, you're correct. The actual value assigned is something like all bits set except the third. -1 is all bits set (hex: 0xFFFFFFFF), -2 is all bits except the first and so on. What you would see is probably the hex value 0xFFFFFFFB which in decimal corresponds to 4294967291.

是的,你是对的。分配的实际值类似于除第三个以外的所有位设置。 -1是所有位设置(十六进制:0xFFFFFFFF),-2是除第一个以外的所有位,依此类推。您将看到的可能是十六进制值0xFFFFFFFB,其十进制对应于4294967291。

#1


53  

For the official answer - Section 4.7 conv.integral

正式答案 - 第4.7节conv.integral

"If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type). [ 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 ]

“如果目标类型是无符号的,则结果值是与源整数一致的最小无符号整数(模2n,其中n是用于表示无符号类型的位数)。[注意:在二进制补码表示中,此转换是概念性的,并且位模式没有变化(如果没有截断)。-end note]

This essentially means that if the underlying architecture stores in a method that is not Two's Complement (like Signed Magnitude, or One's Complement), that the conversion to unsigned must behave as if it was Two's Complement.

这实质上意味着如果底层架构存储的方法不是Two's Complement(如Signed Magnitude,或One的Complement),那么转换为unsigned必须表现得好像是Two's Complement。

#2


23  

It will assign the bit pattern representing -5 (in 2's complement) to the unsigned int. Which will be a large unsigned value. For 32 bit ints this will be 2^32 - 5 or 4294967291

它将表示-5(以2的补码)表示的位模式分配给unsigned int。这将是一个很大的无符号值。对于32位整数,这将是2 ^ 32 - 5或4294967291

#3


4  

It will show as a positive integer of value of max unsigned integer - 4 (value depends on computer architecture and compiler).

它将显示为max无符号整数值的正整数-4(值取决于计算机体系结构和编译器)。

BTW
You can check this by writing a simple C++ "hello world" type program and see for yourself

顺便说一句你可以通过编写一个简单的C ++“hello world”类型程序来检查这一点并自己查看

#4


4  

You're right, the signed integer is stored in 2's complement form, and the unsigned integer is stored in the unsigned binary representation. C (and C++) doesn't distinguish between the two, so the value you end up with is simply the unsigned binary value of the 2's complement binary representation.

你是对的,有符号整数以2的补码形式存储,无符号整数存储在无符号二进制表示中。 C(和C ++)不区分这两者,因此您最终得到的值只是2的补码二进制表示的无符号二进制值。

#5


0  

Yes, you're correct. The actual value assigned is something like all bits set except the third. -1 is all bits set (hex: 0xFFFFFFFF), -2 is all bits except the first and so on. What you would see is probably the hex value 0xFFFFFFFB which in decimal corresponds to 4294967291.

是的,你是对的。分配的实际值类似于除第三个以外的所有位设置。 -1是所有位设置(十六进制:0xFFFFFFFF),-2是除第一个以外的所有位,依此类推。您将看到的可能是十六进制值0xFFFFFFFB,其十进制对应于4294967291。