6

As far as I could find, the width of the bool type is implementation-defined. But are there any fixed-width boolean types, or should I stick to, for e.g., a uint8_t to represent a fixed-width bool?

[EDIT] I made this python script that auto-generates a C++ class which can hold the variables I want to be able to send between a micro controller and my computer. The way it works is that it also keeps two arrays holding a pointer to each one of these variables and the sizeof each one of them. This gives me the necessary information to easily serialize and deserialize each one of these variables. For this to work however the sizeof, endianness, etc of the variable types have to be the same on both sides since I'm using the same generated code on both sides. I don't know if this will be a problem yet, but I don't expect it to be. I have already worked with this (32bit ARM) chip before and haven't had problems sending integer and float types in the past. However it will be a few days until I'm back and can try booleans out on the chip. This might be a bigger issue later, since this code might be reused on other chips later.

So my question is. Is there a fixed width bool type defined in the standard libraries or should I just use a uint8_t to represent the boolean?

Aaron de Windt
  • 16,794
  • 13
  • 47
  • 62
  • 2
    A `bool` type is fixed at `true` and `false`. How can 2 states have varying width? – Thomas Matthews Dec 25 '15 at 00:19
  • Are you talking about packing one or more bools in an integral type? – Thomas Matthews Dec 25 '15 at 00:20
  • 1
    Do you refer to size in the memory? If so it is compiler dependent for bool. In 99.9% it takes up 1 bit as it represents true or false, on or off. However, the other 7 are stuffed, can't be used. Did that answer it for you? – Emz Dec 25 '15 at 00:21
  • Are you talking about 8-bit vs 16-bit vs 32-bit vs 64-bit integers to represent a single `bool`? – Thomas Matthews Dec 25 '15 at 00:21
  • @Emz how did you reach that conclusion of 99.9% ? – M.M Dec 25 '15 at 00:25
  • I am currently writing some code that communicates between a microcrontroller and a computer. I was just wondering if sizeof(bool) would give me the same result on both. Apparently not necessary. – Aaron de Windt Dec 25 '15 at 00:25
  • Depends on the *minimal accessible size* for the microcontroller. If the microcontroller can only operate on 16-bit quantities or larger, then the size of a `bool` will be 16-bits. Some microcontrollers can access a bit as the minimum size. – Thomas Matthews Dec 25 '15 at 00:27
  • @M.M I made it up based on my own experience (100%) then I subtracted .1% before Jon Skeet or some other monster comes and corrects me. I could always claim I took that into consideration! (I am SMRT!) Jokes aside: That number is made up and represents that it is most commonly stored as 1 byte, where only 1 bit is used. [Reference](http://stackoverflow.com/questions/4897844/is-sizeofbool-defined) *Disclaimer: I do not claim Jon Skeet to be an actual monster.* – Emz Dec 25 '15 at 00:29
  • There is only one datatype explicitly for boolean values, and it doesn't have the property you want. Simple answer: pick a fixed-width type and use it as a bool. – tenfour Dec 25 '15 at 00:31
  • 1
    @Emz: Typically `bool` is 8 bits, with all but the low-order bit being set to 0. – Keith Thompson Dec 25 '15 at 00:44
  • 2
    It would help if you could explain *why* you need a boolean type of some particular width. What purpose would such a type serve that isn't already served by `bool`? ("Having a fixed width" is not an answer.) – Keith Thompson Dec 25 '15 at 00:45
  • @KeithThompson That is what I said? Except zero initialization of the other bits, because there is no guarantee of that. Then I would have to add another 99.9% case. – Emz Dec 25 '15 at 00:55
  • If using a different type to simulate bool, be aware that you'll need to manually produce boolean conversions, e.g. `x & FLAG` needs to become `!!(x & FLAG)` – M.M Dec 25 '15 at 00:55
  • 1
    sizeof any types in C and C++ are not necessarily the same across different architectures – phuclv Dec 25 '15 at 05:44
  • @LưuVĩnhPhúc except char and friends, these are always 1 byte. If you need the same number of bits, that's uint32_t and friends. – n. m. could be an AI Dec 25 '15 at 06:41
  • @KeithThompson I made this python script which autogenerates a C++ class which can hold a the variables I want to be able to send between the micro controller and computer. The way it works is that I also keep two arrays holding a pointer to each one of these variables and the sizeof each one of them. This gives me the necessary information to easily serialize and deserialize each one of these variables. For this to work however the sizeof, endianness, etc of the variable types have to be the same on both sides. – Aaron de Windt Dec 25 '15 at 12:39
  • That information should be in the question, not just in a comment. With that additional background, I don't think this is a duplicate. You're asking how to transfer boolean values from one system to another, where the two systems may not use the same representation. – Keith Thompson Dec 25 '15 at 19:19
  • @KeithThompson As you recommended, I added the background information to the question. – Aaron de Windt Dec 26 '15 at 00:10

1 Answers1

4

There is not. Just use uint8_t if you need to be sure of the size. Any integer type can easily be treated as boolean in C-related languages. See https://stackoverflow.com/a/4897859/1105015 for a lengthy discussion of how bool's size is not guaranteed by the standard to be any specific value.

Community
  • 1
  • 1
Rob Starling
  • 3,868
  • 3
  • 23
  • 40
  • 4
    of course there's no implicit conversion to `bool` when doing this so you have to take care. For example `uint8_t x = 0x500 & 0x100;` will be `0`, but a bool would have been `true` – M.M Dec 26 '15 at 07:01
  • @M.M true, but it's easy to fix: `uint8_t x = 0x500 & 0x100 != 0`. You just need to be careful. – Mark Ransom Dec 26 '15 at 21:28
  • 2
    @MarkRansom ITYM `(0x500 & 0x100) != 0` ;) – M.M Dec 26 '15 at 22:07
  • A better solution may be to check the size of `bool` with a static assert. As exemplified by the above comments and [this answer](https://stackoverflow.com/questions/75009081/return-true-or-1/75009552#75009552) using `uint8_t` instead of `bool` comes with the risk of subtle errors. – nielsen Jun 29 '23 at 06:54