32 位无符号、有符号、64 位差异的实际示例
我是一个相当新的程序员,我想知道是否有人可以给我一个关于签名、未签名和 32 位与 64 位之间的差异和用途的实用解释/示例?
例如,我去年读过一篇关于 Twitter 如何让开发人员切换到 64 位的文章,但我不确定其原因和具体性质。
谢谢你!
I'm a fairly new programmer and I was wondering if someone could give me a practical explanation/example on the differences and uses between working with signed, unsigned and 32 bit vs 64 bit?
i.e. I read an article about how Twitter had developers switch to 64 bit last year but I wasn't sure the reasoning and the specific nature to it.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 n 位,您可以用这些位表示 2^n 个不同的数字。因此,32 位无符号数从 0 到 4,294,967,295(2^32-1,-1 是因为 0 是有效数字)。有符号的数字将这 40 亿分为正数和负数。 32 位计算机在其内存地址中使用此地址,这意味着程序可以本机访问 4 GB 内存。 64 位计算机的限制为 2^64,这个数字要高得多。
如果您使用 32 位数字来表示其他内容(例如用户、推文或自特定日期以来的秒数),您也会遇到 40 亿的限制。因此,32 位在一定范围内工作得很好,超过这个范围,尽管有多种方法可以解决该限制,但转向 64 位更有意义。
缺点是需要两倍的内存来存储数字。
For n bits, you can have 2^n different numbers represented by those bits. So 32 bit unsigned numbers go from 0 to 4,294,967,295 (2^32-1, the -1 is because 0 is a valid number). Signed numbers divide that 4 billion evenly between positive and negative. 32-bit computers use this in their memory addresses, which means a program can natively access 4 GB of memory. 64-bit computers have a limit of 2^64, which is much, much higher.
You also run across that 4 billion limit if you're using 32-bit numbers to represent other things, like for example users, tweets, or seconds since a certain date. So 32-bit works just fine up to a certain scale, then above that, even though there are ways to work around the limit, it makes more sense to go to 64-bit.
The disadvantage is it takes twice as much memory to store your numbers.