签署VS用C无符号整型 [英] signed vs unsigned int in C
问题描述
我是一个C初学者,就需要一个整数快速澄清。
I am a C beginner and needed a quick clarification regarding ints.
当你施放的东西作为一个int,它是32位,该位的一半分配给负整数。因此,如果你知道你将永远有正整数,铸造的东西作为一个unsigned int应该最大限度地提高您的积极位,从而使更多的意义,当你知道输出将始终是正的。这是真的吗?
When you cast something as an int, which is 32 bits, half of the bits are allocated for negative integers. Thus, if you know that you will always have positive integers, casting something as an unsigned int should maximize your positive bits and thus make more sense when you know that output will always be positive. Is this true?
感谢您!
推荐答案
位的一半被分配为负整数
此说法不属实,一位被分配给签收普通整数。
half of the bits are allocated for negative integers
This statement is not true, one bit is allocated to the sign for regular ints.
不过,你在你的假设是正确的。如果你是正数为正数,使用 unsigned int类型
将允许您访问的范围是[0,2 ^ 32)的数量,而常规INT将只只允许[ - (2 ^ 31),2 ^ 31-1],因为你不需要负值,它让你用更少的正数
However, you are correct in your assumption. If you are positive the number is positive, using unsigned int
will allow you to access number in the range [0,2^32), while the regular int will only only allow [-(2^31),2^31-1], since you do not need the negative values, it leaves you with less positive numbers.
这篇关于签署VS用C无符号整型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!