Slowly I study Swift and after the lesson by type there was a question regarding Int. It is written that for devices with a 32 bit processor Int from -2147483648 to 2147483647. And for 64 bit from -9223372036854775808 to 9223372036854775807. Actually the question is one, but with sub-questions:

  1. How to use it correctly?

1.1 What will happen if, as a result of any calculations, a number greater than 2147483647 is obtained on a device with a 32-bit processor? The number is not particularly large.

1.2 If the numbers are guaranteed to be in the range from -2147483648 to 2147483647, does it make sense that there will be such big limits on 64 bit? In the same place, something seems to be connected with memory allocation, will it require more resources / slower work?

  • I think that it is necessary to use as if int 32 бита , if you need more, then use long long farther - long. It is safer. The speed of work will be about the same (the processor will still process it in 1 clock). If you need to fix the size, it is better to use something like uint32 and so on ... - pavel
  • one
    @pavel Here only in Swift there is no long. S-shnye Clong and CLongLong are essentially the same Int and Int64. The question is of course deep, but judging by the official documentation, where it is advised not to bother with the choice, and to use just Int, it will not affect anything in most standard tasks. (regarding 1 and 1.2) - VAndrJ

1 answer 1

If, as a result of any calculations, a number greater or less is obtained, the following will occur. All bits with an index greater than 32 will be discarded. Those. the number will take a different value.

Example 1 :

x = 2147483647;

In bit terms, this is 1111111111111111111111111111111

If we add 1 to it, the idea should be like this:

2147483647 + 1 = 2147483648

However, in practice, we get in the bit expression the number 10000000000000000000000000000000

If this is an int type, then this number will be -2147483648

Those. we get 2147483647 + 1 = -2147483648

Example 2 :

 x = 2147483647; y = 1000; 

If you multiply x * y , the idea should be like this:

2147483647 * 1000 = 2147483647000

In practice, we will get the number 11111001111111111111111111111110000011000 in bit expression

If this is an int type, then all that is more than 32 characters (beginning on the right) is truncated and we get: 11111111111111111111110000011000

this number will be -1000

Those. we will get 2147483647 * 1000 = -1000

About memory allocation:

32 bits is 4 bytes, 64 bits is 8 bytes. Conclusion - will require more resources and as a result will be slower. But since Since RAM is in megabytes (millions of bytes) and the process is gigahertz (billions of operations per second), then the slowdown will not be noticeable. Those. this can be neglected.

  • Thanks for the theory, everything is logical and helps to understand how it works. But in practice, when I write var test = 2147483647; test + = 1 and choose 4s / 5, then immediately underlines in red. And when the rest - does not emphasize anything. - Son'ka V
  • Use type Double if you need to operate with large numbers - Maxim Bogdanov
  • The problem is not in operating with large numbers, but to understand whether it makes sense to use it until the 32-bit versions cease to be supported. - Son'ka V
  • Do your numbers take on such large values? - Maxim Bogdanov