签名int范围混乱 [英] Signed int range confusion

查看:136
本文介绍了签名int范围混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能非常基本,但我只是在谷歌搜索几天之后才发布这里,以及我对C中签名整数的正确基本理解。

This question might be very basic but i post here only after days of googling and for my proper basic understanding of signed integers in C.

实际上有人说签名为int有范围

Actually some say signed int has range


  1. -32767到32767,其他人说它有范围

  2. -32768到32767

让我们得到int a = 5(签名/让我们考虑一个字节)

Let us have int a=5 (signed / let us consider just 1 byte)

* a = 5的第一个表示形式为00000101为正数,a = -5表示为10000101(因此范围-32767至32767对齐)
(此处msb /符号位为1/0该数字为正/负,其余(幅度位)不变)

*the 1st representation of a=5 is represented as 00000101 as a positive number and a=-5 is represented as 10000101 (so range -32767 to 32767 justified) (here the msb/sign bit is 1/0 the number will be positive/negative and rest(magnitude bits) are unchanged )

* a = 5的第二个表示形式为00000101为正数并且a = -5表示为11111011
(msb被视为-128,其余位被操纵以获得-5)(因此范围-32768至32767合理)

*the 2nd representation of a=5 is represented as 00000101 as a positive number and a=-5 is represented as 11111011 (the msb is considered as -128 and the rest of bits are manipulated to obtain -5) (so range -32768 to 32767 justified)

所以我在这两件事之间混淆了。我的疑问是c,1)或2)中的signed int的实际范围是什么

So I confuse between these two things. My doubt is what is the actual range of signed int in c ,1) or 2)

推荐答案

今天,签名的int通常是在两个补码表示法中完成。

Today, signed ints are usually done in two's complement notation.

最高位是符号位,它是为所有负数设置的。

The highest bit is the "sign bit", it is set for all negative numbers.

这意味着你有7位表示不同的值。

This means you have seven bits to represent different values.

最高位未设置,你可以(总共16位)代表值0..32767。

With the highest bit unset, you can (with 16 bits total) represent the values 0..32767.

随着最高位设置因为您已经有零的表示,您可以表示值-1 ..- 32768。

With the highest bit set, and because you already have a representation for zero, you can represent the values -1..-32768.

然而,这是实现定义的,其他表示也确实存在。您的平台/编译器上的有符号整数的实际范围限制是在您的环境中找到的 < limits.h> 。这是唯一明确的权限。

This is, however, implementation-defined, other representations do exist as well. The actual range limits for signed integers on your platform / for your compiler are the ones found in your environment's <limits.h>. That is the only definite authority.

在今天的桌面系统上, int 是通常为32或64位宽,相应的范围比您所说的16位32767/32768大得多。所以这些人都在谈论非常古老的平台,真正的旧知识,嵌入式系统或最小保证范围 - 标准规定 INT_MIN 必须至少 -32767, INT_MAX 至少 +32767,最低公分母。

On today's desktop systems, an int is usually 32 or 64 bits wide, for a correspondingly much larger range than the 16-bit 32767 / 32768 you are talking of. So either those people are talking about really old platforms, really old knowledge, embedded systems, or the minimum guaranteed range -- the standard states that INT_MIN must be at least -32767, and INT_MAX be at least +32767, the lowest common denominator.

这篇关于签名int范围混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆