I would like to know the difference between int type, long type and long type in C++.
In particular, I would like to know about the upper limit values that can be expressed, so I looked into them and found out that the following is the recognition.
int->2^31-1
long->2^31-1
long long->2^63-1
I understand the difference between int and long, but I don't know how to position long.
Both long and long are 2^63-1.In the video of Atcoder's explanation, you explained that if int is not enough, use long long, but isn't it long after int?
The background is that when I looked into it, I couldn't understand it all the better.
I would appreciate it if you could explain the difference between long and long in terms of the upper limit.
It may depend on the version, so I will share that I use c++11.
I look forward to your kind cooperation.
c++ c++11
c is an ancient language. c++ displays important questions with tagged languageNow that you know about it,
c99Language Standards ISO/IEC 9899:1999 specifies
char
must be at least 8 bits int
or smallerint
must be at least 16 bits in sizelong
must be at least 32 bits long or smallerlong
must be at least 64bit in sizeI'm still using it as a matter of course in my embedded 8/16bit microcomputer to meet the previous requirements
char
8bitint
16bitlong
32bitSame for 32-bit microcomputers or advanced 32-bit processors such as x86
char
8bitshort
16bitint
32bitThe specific size of long
or long
depends on the processor's own designer, compiler designer, and OS's own specifier.The criteria for this decision are
The opposite is true, and after many twists and turns, many 32-bit processor compilers have
long
32-bit (to ensure compatibility with 16-bit source code)long
64bit (because long
must be larger)is calm (as stated in your question).
Therefore, the answer to your question will be, "This size has been adopted as a result of choosing a model size that is easy to port old source code."
"Compatibility with existing software" is particularly important for 64-bit processors, and different specifications exist side by side for the size of the model. Like LP64 or LLP64.
https://project-flora.net/2015/07/21/cc%E3%81%AB%E3%81%8A%E3%81%91%E3%82%8B%E6%95%B4%E6%95%B0%E5%9E%8B%E3%81%AB%E3%81%AF%E6%B0%97%E3%82%92%E3%81%A4%E3%81%91%E3%82%88/a>
And it's very inconvenient that the size of the type varies from compiler to compiler. c99 or int or long
instead of using a type that may vary in size for each processing system.
As you have answered, the sizes int
, long
, and long
are not specified in C and C++ languages and are implementation dependent.
For example, on Visual C++ and Windows,
int
32bit
long
32bit
long
64bit
It is stipulated that
As far as AtCorder is concerned, the Language Test page is
Note: See below for information on AtCoder Judge servers
$uname-a
Linux ip-***-***-***-*** 3.13.0-74-generic#118-Ubuntu SMP Thu Dec 17 22:52:10 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
$ cat/etc/lsb-release
DISTRIB_ID = Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.4 LTS"
It appears that you are using Linux x86_64.Linux has Linux Standard Base and Linux Standard Base Core Specification for X86-64/Chapter 7. Low Level System Information/7.1. Machine Interface/7.1.2.Data Representation/7.1.2.3. Fundamental Types
LSB-conforming applications shall use only the fundamental types described in Section 3.1.2 of System V Application Binary Interface AMD64 Architecture Processor Supplement.
int
32bit
long
64bit
long
64bit
It is stipulated thatAs you can see in the same way, Linux x86 specifies long
as 32-bit, so long
is different in size between x86 and x86_64.
The upper limit (bit width) of each integer type in C/C++ depends on the system (of course sizeof(int)<=sizeof(long)<=sizeof(long)
is always present for each system).
Basic Type - According to cppreference.com, specifically
int
16-bit or 32-bitlong
32-bit or 64-bitlong
64-bitIt may have a width of .Therefore, you can see that long
is not always greater than int
.On the other hand, long
is guaranteed to have a 64-bit width, so it is always larger than int
and the policy of "use long long if int is not enough" is always valid.
As far as AtCoder is concerned at least, it is safe to think that int
=32 bits, long
=64 bits.If you are worried, you may want to use a type that specifies the bit width, such as int32_t
or int64_t
.
By the way, AtCoder uses GCC, but GCC defines an integer with a 128-bit width of _int128
, which can handle situations where even 64 bits are insufficient.
The int, long and long types are defined in limits.h, so check the header file of your C++ compiler.
A special integer type may be defined in "stdint.h", so be sure to check it.
The difference between int and long and long depends on the computer environment (the number of bits in the CPU).
The int type is often an integer with the same number of bits as the CPU, long is a 32-bit integer, and long is a 64-bit integer. (The term "integer" is used to mean both integers and unsigned integers.)
In the table, the CPU type (number of bits) and the number of int, long, and long bits of each are as follows:
int long long long long
16bit CPU 163264
32-bit CPU 323264
64-bit CPU646464
912 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
617 Uncaught (inpromise) Error on Electron: An object could not be cloned
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.