C++ Logo


Advanced search

Re: [std-proposals] CHAR_BIT might not be 8

From: William Linkmeyer <wlink10_at_[hidden]>
Date: Wed, 12 Oct 2022 20:32:36 -0400
Yeah, this is an oddity that doesn’t have a good solution AFAIK.

Two good reason for not mandating this:

- It‘s an open question if this would ever be implemented consistently because of historical reliance (trigraphs, anyone?)

- Forget supercomputers; I’ve worked on plenty of embedded devices that have small CHAR_BITs

- There would be subtle breakages between C and C++


> On Oct 12, 2022, at 7:25 PM, Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> wrote:
> This email isn't actually a proposal for the C++ Standard, however I
> think I should post it here for the committee to see (I've posted it
> to comp.lang.c++ too).
> Since I started programming in C/C++ back in the early 2000's, I've
> never encountered a compiler with CHAR_BIT anything other than 8. I
> heard about some fancy supercomputers that had the following setup:
> 1 == sizeof(char) == sizeof(int) == sizeof(short) == sizeof(long)
> == sizeof(long long)
> and so CHAR_BIT would be 64, but I've never walked within a mile of
> such a computer.
> Well just now in the past 20 minutes, I was trying to get C++ code for
> the 'base58' algorithm to compile for the Texas Instruments F2809
> microcontroller using the 'cl2000' compiler.
> At first I was puzzled as to why 'sizeof(long)' was coming back as 2.
> Of course my first assumption here was that 'long' was 16-Bit (even
> though the Standard says that a 'long' must be at least 32-Bit). So I
> figured that this compiler was just non-standard-compliant on a few
> issues.
> After a little messing around, I realised that CHAR_BIT is 16.
> I looked through the compiler manual and found this paragraph:
> "By ANSI/ISO C definition, the sizeof operator yields the number of
> bytes required to store an object. ANSI/ISO further stipulates that
> when sizeof is applied to char, the result is 1. Since the TMS320C28x
> char is 16 bits (to make it separately addressable), a byte is also 16
> bits. This yields results you may not expect; for example, size of
> (int) = = 1 (not 2). TMS320C28x bytes and words are equivalent (16
> bits). To access data in increments of 8 bits, use the __byte() and
> __mov_byte() intrinsics described in Section 7.6"
> So there you go: There are C++ compilers in use today in the year 2022
> with CHAR_BIT something other than 8.
> I've noticed that as the C++ Standard has evolved, freedoms have been
> taken away from compiler writers, for example negative numbers now
> have to be two's complement, whereas in the past they had the choice
> of sign-magnitude and one's.
> So if the committee is thinking about mandating CHAR_BIT to be 8, well
> I want you all to know that there's a microcontroller in use today in
> they year 2022 that has CHAR_BIT == 16.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-10-13 00:32:49