r/ProgrammerHumor • • 12d ago

Meme cppAndItsVectorBooleans

Post image
3.4k Upvotes

214 comments sorted by

View all comments

Show parent comment

19

u/ImpulsiveBloop 12d ago

Yeah, I recall there being a difference between using char and uint8_t, too. But I can't be bothered to remember what. Overflow or something.

Unsigned char would be better. But that is more stuff to type out, and less clear on what it is imo.

26

u/nfitzen 12d ago edited 11d ago

From a C perspective, since IDK C++: char is defined to be the smallest addressable unit (in other words, a machine byte, which may be more than 8 bits on cursed hardware). Since CHAR_MAX >= 127 per the ISO C standard, it's probably the best thing to use for a hacky boolean type (of course, when bool doesn't work). uint8_t could also be used, but I feel like it communicates that we need 8 bits specifically rather than "the minimal amount the machine can address individually."

Edit: Also, unsigned char might be my first instinct, but it doesn't particularly matter. Any non-zero value is truthy to C. So I'd probably use char since I'm a fan of brevity.

7

u/ImpulsiveBloop 11d ago

Thank you. Just pretend I put in the effort to say this myself lol

6

u/bwmat 12d ago

If you're actually using the elements as booleans, only zero/non-zero matters, so char, signed char, unsigned char (yeah these are actually 3 separate types lol cpp) are all the same in this context

3

u/ImpulsiveBloop 11d ago

Yeah, I just meant in general. 

They are different, was my point, so they shouldn't be used interchangeably without consideration.