有符号整数和无符号整数之间的区别是:
Unsigned值可以是一个较大的正数,而不能是负数。 Unsigned版本使用前导位作为值的一部分,而signed版本使用最左边的位来识别数字是正还是负。 有符号整数可以同时包含正数和负数。
还有其他区别吗?
有符号整数和无符号整数之间的区别是:
Unsigned值可以是一个较大的正数,而不能是负数。 Unsigned版本使用前导位作为值的一部分,而signed版本使用最左边的位来识别数字是正还是负。 有符号整数可以同时包含正数和负数。
还有其他区别吗?
当前回答
在嵌入式系统上编程时,必须使用无符号整数。在循环中,当不需要有符号整数时,使用无符号整数将节省设计此类系统所需的安全。
其他回答
他只问了签过名和没签过名的。不知道为什么人们要在里面加额外的东西。让我来告诉你答案。
Unsigned:它只包含非负值,即0到255。 Signed:由正负值组成,但格式不同,如 0 ~ +127 -1 ~ -128
这个解释是关于8位数字系统的。
除此之外,在C语言中,你不能溢出一个无符号整数;行为被定义为模算术。您可以溢出一个有符号整数,并且在理论上(尽管在当前主流系统上没有实践),溢出可能会触发一个错误(可能类似于除零错误)。
是的,无符号整数可以存储大的值。 不,有不同的方式来表示正数和负数。 是的,有符号整数可以包含正数和负数。
在嵌入式系统上编程时,必须使用无符号整数。在循环中,当不需要有符号整数时,使用无符号整数将节省设计此类系统所需的安全。
Signed integers in C represent numbers. If a and b are variables of signed integer types, the standard will never require that a compiler make the expression a+=b store into a anything other than the arithmetic sum of their respective values. To be sure, if the arithmetic sum would not fit into a, the processor might not be able to put it there, but the standard would not require the compiler to truncate or wrap the value, or do anything else for that matter if values that exceed the limits for their types. Note that while the standard does not require it, C implementations are allowed to trap arithmetic overflows with signed values.
Unsigned integers in C behave as abstract algebraic rings of integers which are congruent modulo some power of two, except in scenarios involving conversions to, or operations with, larger types. Converting an integer of any size to a 32-bit unsigned type will yield the member corresponding to things which are congruent to that integer mod 4,294,967,296. The reason subtracting 3 from 2 yields 4,294,967,295 is that adding something congruent to 3 to something congruent to 4,294,967,295 will yield something congruent to 2.
Abstract algebraic rings types are often handy things to have; unfortunately, C uses signedness as the deciding factor for whether a type should behave as a ring. Worse, unsigned values are treated as numbers rather than ring members when converted to larger types, and unsigned values smaller than int get converted to numbers when any arithmetic is performed upon them. If v is a uint32_t which equals 4,294,967,294, then v*=v; should make v=4. Unfortunately, if int is 64 bits, then there's no telling what v*=v; could do.
鉴于标准的现状,我建议在需要与代数环相关的行为时使用无符号类型,在需要表示数字时使用有符号类型。不幸的是,C以这种方式进行了区分,但它们就是它们。