c++ - Is `long` guaranteed to be at least 32 bits? -
by reading of c++ standard, have understood sizes of integral fundamental types in c++ follows:
sizeof(char) <= sizeof(short int) <= sizeof(int) <= sizeof(long int) i deduced 3.9.1/2:
- there 4 signed integer types: “signed char”, “short int”, “int”, , “long int.” in list, each type provides @ least storage preceding in list. plain ints have natural size suggested architecture of execution environment
further, size of char described 3.9.1/ being:
- [...] large enough store member of implementation’s basic character set.
1.7/1 defines in more concrete terms:
- the fundamental storage unit in c + + memory model byte. byte @ least large enough contain member of basic execution character set , composed of contiguous sequence of bits, number of implementation-defined.
this leads me following conclusion:
1 == sizeof(char) <= sizeof(short int) <= sizeof(int) <= sizeof(long int) where sizeof tells how many bytes type is. furthermore, implementation-defined how many bits in byte. of used dealing 8-bit bytes, standard says there n bits in byte.
in this post, alf p. steinbach says:
long guaranteed (at least) 32 bits.
this flies in face of understand size of fundamental types in c++ according standard. discount statement beginner being wrong, since alf decided worth investigating further.
so, you? long guaranteed standard @ least 32 bits? if so, please specific how guarantee made. don't see it.
the c++ standard says in order know c++ must know c (1.2/1) 1
the c++ standard implicitly defines minimum limit on values
longcan accommodatelong_min-long_max2
so no matter how big long is, has big enough hold long_min long_max.
but alf , others specific long must @ least 32 bits. i'm trying establish. c++ standard explicit number of bits in byte not specified (it 4, 8, 16, 42) how connection made being able accommodate numbers long_min-long_max being @ least 32 bits?
(1) 1.2/1: following referenced documents indispensable application of document. dated references, edition cited applies. undated references, latest edition of referenced document (including amendments) applies.
- iso/iec 2382 (all parts), information technology – vocabulary
- iso/iec 9899:1999, programming languages – c
- iso/iec 10646-1:2000, information technology – universal multiple-octet coded character set (ucs) – part 1: architecture , basic multilingual plane
(2) defined in <climits> as:
long_min -2147483647 // -(2^31 - 1) long_max +2147483647 // 2^31 - 1
c++ uses limits defined in c standard (c++: 18.3.2 (c.limits), c: 5.2.4.2.1):
long_min -2147483647 // -(2^31 - 1) long_max +2147483647 // 2^31 - 1 so guaranteed long @ least 32 bits.
and if want follow long circuitous route whether long_min/long_max representable long, have @ 18.3.1.2 (numeric.limits.members) in c++ standard:
static constexpr t min() throw(); // equivalent char_min, shrt_min, flt_min, dbl_min, etc. static constexpr t max() throw(); // equivalent char_max, shrt_max, flt_max, dbl_max, etc. i moved footnotes comment, it's not appears in standard. implies std::numeric_limits<long>::min()==long_min==(long)long_min , std::numeric_limits<long>::max()==long_max==(long)long_max.
so, though c++ standard not specify bitwise representation of (signed) negative numbers, has either twos-complement , require 32-bits of storage in total, or has explicit sign bit means has 32-bits of storage also.
Comments
Post a Comment