# The size of a byte in platform independent software

When writing platform independent software in C++, can I nowadays safely1 assume that a byte has a size of 8 bits? Or do I have to calculate it like this, for example:

``````#include <climits>

std::size_t byteSize = sizeof(char) * CHAR_BIT;
``````

1 In the sense it is standardized or otherwise guaranteed.

• Bytes are always 8 bit, and C99 standardizes "char" to be one byte. Dec 9, 2021 at 11:36
• I agree on "C99 standardizes "char" to be one byte", but do you have a source for "Bytes are always 8 bit"? Octets always have a size of 8 bits, but bytes are just the smallest addressable units, not necessarily octets (see en.wikipedia.org/wiki/Byte). That's basically why I ask this question. Dec 9, 2021 at 11:41
• @pjc50 You are confusing terminology here; an octet is always 8 bit, A byte is a difference concept, defined as being the smallest addressable unit of memory. The only guarantee provided by C is that a byte will always be at least 8 bits; C allows a byte and therefore a char to be larger than 8 bits. See en.wikipedia.org/wiki/36-bit_computing for example, which describes C implementations using 9-bit `char`. Dec 9, 2021 at 12:10

Nowadays, a byte is indeed an octet of 8 bits on most of the current computers. But the C++ standard doesn't guarantee that:

[intro.memory]/1: The fundamental storage unit in the C++ memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set and the eight-bit code units of the Unicode UTF-8 encoding form and is composed of a contiguous sequence of bits, the number of which is implementation- defined.

So, it's at least 8 bits. But it could be more.

If you write platform independent code, your worst ennemies are wrong assumptions. C++ code can run on a large set of microcontrolers as well, and who knows all the many platforms out there that are still alive. And maybe future platforms could use larger bytes as well, now that we're all working with unicode.

• As not everybody might have access to the standard, here is an unofficial, not authoritative source, that states the same: en.cppreference.com/w/cpp/language/memory_model#Byte Dec 9, 2021 at 12:14
• It is still not a universal truth that byte == octet, but you will need to be using specialist microprocessors (like DSPs) to find a situation where that is not the case. Dec 9, 2021 at 14:48
• @Bartvaningenschenau I guess that if the standard committee couldn’t reach an agreement to simplify to 8 bits in 2020, it’s not without reasons. For now, the wrong assumption only leads to nasty bugs on DSP and industrial microcontrollers as you rightly point out. But who knows how the mainstream will look like in 20 years? (after all, in the 80’s people used to encode dates with two digit years and a lot of code assumed that a simple comparison was enough despite Y2K and the 4 digits were not so far away…) ;-) Dec 9, 2021 at 16:29

sizeof(char) is always 1, because a char is the smallest data word. But there's no guarantee that a char is 8 bits. It is on most platforms, but there may still be some obscure specialist processors where it's not.

Many years ago, I had to write code for a DSP chip that only had one word size for all integer types.

I have once seen source code written for sixteen bit bytes. Some DSP processor, 20 years ago. Since storing an 8 bit character in a 16 bit byte is inefficient, they had a library handling strings with two characters stored in one byte.

You can “safely” assume a byte is eight bits. If it isn’t, you may have to do a lot of extra work.

When writing platform independent software in C++, can I nowadays safely assume that a byte has a size of 8 bits?

In theory, no. You could imagine future processors not having 8 bit bytes. Perhaps some weird VLIW processors don't have them.

In practice, yes, for the few next years.

Quantum computers, or old Setun computers,or IBM 7090 don't have bytes.

You could also generate C++ code (or C code), like GNU autoconf or RefPerSys or ANTLR or GNU bison are doing. Or using GPP.

Pitrat's book: Artificial Beings, the conscience of a conscious machine explains the interest of generating C code, and the explanation is extensible to generating C++ code.

Queinnec's book: Lisp In Small Pieces explains in detail how to do so.

Platform portability is somehow a fiction. Projects could generate C++ (or C) code to improve portability to other platforms.

The Qt project is in C++ and contains a C++ code generator, its `moc`

I nowadays safely1 assume that a byte has a size of 8 bits?

You can assume that your program can use (at least) 8 bits but you cannot assume that a byte occupies 8 bit in memory due to memory alignment.

The reason is that many processors are faster when fetching data from an even memory address:

if your byte is located at memory adress 0x123 the processor may need more time to fetch than fetching a byte located at adress 0x120.

That is the reason why c++ compilers have the option to optimize-for-speed or optimize-for-space.

To verify create a structure that contain two byte elements and look for the size of the structure.