search

Home  >  Q&A  >  body text

java - int占4字节,一数占一字符,为什么int能表示5位以上的数字?

我知道这个问题~有点无厘头和傻乎乎。还是想请人解答一下~
----------可能目前脑子正处于抽风阶段----------
首先,我知道int占4个字节,包含正负2的31次方内的数字,也就是基本可以表示10位数字。
那么根据Ascll里,一个数字占用一个字节的规则来思考。
我让int按照这种规则来储存数字,是不是应该只能储存最多4位数字???
还请明白人~指出我这样思考的错误点在哪里?或者告知正确的解答方式~。

高洛峰高洛峰2802 days ago1656

reply all(16)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 09:28:53

    ............
    ‘1’! = 1
    ‘2’! = 2
    ‘3’! = 3
    (1)10 = (1)2
    (2)10 = (10)2
    (3)10 = (11)2
    char occupies one byte
    32-bit int occupies four bytes

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:28:53

    '1'[char] and 1[int] are different
    because they are expressed in different ways in binary
    '1' =》00110001[char is one byte, eight bits, and will have special meanings, such as Can represent symbols or something]
    1 =》00000000000000000000000000000001 [int is 4 bytes, 32 bits, only used to represent numbers]

    reply
    0
  • 黄舟

    黄舟2017-04-18 09:28:53

    Basically similar to the idea above.
    First of all, these are two concepts. In my opinion, one means storage and the other means display.
    For example: We all know the boolean type. The same value, in some cases, can be expressed as 1, 0, or true or false.

    The poster’s misconception lies in the mistake in the concept.
    First, let’s analyze the poster’s logic and find out the errors:
    ∵ ① int = 4 bytes; ② In Ascll, 1 number = 1 byte ∴ int = 4 numbers, thus deducing that int can store up to 4 digits .

    We can see that condition ② is wrong, so the answer obtained is wrong
    Analysis:
    1 number = 1 byte, then does 1 byte = 1 number?
    We can say that they are equal in some cases, that is, in base 256, but the poster used base 10 to cover base 256, so it is wrong
    Why is it in base 256? Because 1 byte can represent any value between 0-255, so int can store up to 4 digits of 256-bit numbers

    To sum up, what the original poster said is correct in some cases, but a prerequisite needs to be added, which is a 256-base number

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 09:28:53

    ASCII uses numbers to express strings, and numbers are stored in memory at the previous level of ASCII. You can simply understand it like this:
    Memory (0 and 1) => Numbers (usually can also be used directly for other Base) => Chinese characters or letters (the final display expressed by various bases)

    They are not a level concept.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 09:28:53

    To put it simply:
    The former has a total of 32 grids, each grid can have two situations,
    The latter only has a total of 4 grids, each grid has ten situations (0 to 9),
    One is 2 32nd power, one is 10 raised to the 4th power. If you change both the base and the power, the result will definitely be different.
    The former is actually the binary system in computers, while the latter’s thinking becomes the decimal system we use daily.

    To be specific:
    The ascii code contains 256 characters, including numbers, English letters, symbols, etc. Numbers only account for 10 of them (0 to 9).
    So why can ascii represent so many characters?
    You should know (if you don’t know, you can’t help it), one ASCII code occupies one byte, and one byte has eight bits.
    Each bit has only two situations, 0 and 1, so the combination of 8 bits should be:
    2 X 2 X 2 X 2 X 2 X 2 X 2 X 2 = 2 to the 8th power = 256
    That is, 8 bits can represent a total of 256 situations. If all 256 situations are used to represent numbers, 256 numbers from 0 to 255 can be represented.

    Okay, if you understand this, then it will be easy to understand int. Int has 4 bytes. This is a prerequisite that you also know.
    As explained earlier, one byte can represent 256 situations, so 4 bytes according to the previous algorithm are:
    256 X 256 X 256 X 256 = 2 to the 8th power Power of 295.

    You have secretly changed the concepts before and after. You later changed the problem into:

    10
    10 10 * 10 = 10 to the 4th power = 10000 The result can only represent 4 digits.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:28:53

    It feels like such a silly question. . . ,,Bytes and how many digits are stored are definitely not two different concepts

    reply
    0
  • Cancelreply