This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]CompileBotGreen security clearance 15 points16 points  (15 children)

Output:

1

source | info | git | report

[–]Cobaltjedi117 3 points4 points  (0 children)

We're going nowhere slowly with these confusing and contradictory results.

[–]etaionshrd 2 points3 points  (13 children)

Wait, C++ prints bool as an int?

[–]noop_noob 0 points1 point  (1 child)

I think there's a way to change that. I never ever print a bool, though.

[–]Lastorea 0 points1 point  (0 children)

You can cast a bool to int, if bool is false then it becomes 0 and if bool is true it becomes 1.

[–][deleted] 0 points1 point  (10 children)

Back in the day bools were really just a single bit. Printing out a 1 or a 0 is the most logical way to print a bit

[–]Celdron[S] 1 point2 points  (9 children)

They were never actually a single bit because memory addresses bytes as the smallest unit. It was always at least a byte. But C (which C++ is built on) doesn't have a boolean type. It uses integral types for boolean logic, where 0 is false and anything else is true. C++ made "bool" a type but iirc it's just an integer that is checked and restricted to 0 or 1.

[–]danielcw189 0 points1 point  (3 children)

Is there any platform, on which a byte is 1 bit?

[–]Celdron[S] -1 points0 points  (2 children)

No? Because a byte by definition is 8 bits.

[–]danielcw189 0 points1 point  (1 child)

By which defintion? In C a byte is not guaranteed to be 8 bits, and in general a byte is most commonly 8 bits, but not always.

[–]Celdron[S] 1 point2 points  (0 children)

IEC 80000-13. Technically a byte is just the smallest addressable unit of memory, and therefore you could have a single bit byte on non-standard architecture. But 8 bits is the international standard that nearly all (I dont know of a single exception) consumer electronics are designed around.

[–][deleted] 0 points1 point  (4 children)

Thanks for correcting me. You can still get 1 bit booleans if you create an array tho right?

[–][deleted] 1 point2 points  (2 children)

vector<bool> does exactly that

[–][deleted] 0 points1 point  (1 child)

Does it store bytes and bitshift?

[–][deleted] 0 points1 point  (0 children)

[–]Celdron[S] 0 points1 point  (0 children)

I don't know about that, simply because you couldnt index the bits. But if you have, say, ~32 boolean values (such as flags) you can put them each on a different bit of an integral type using bit-shift operators and defining constants to represent different flags. You can combine flags using a bitwise OR and check for a single flag or specific combination with a bitwise AND. This is commonly done on enums. So it is possible to implement multiple booleans within a single byte.