My OLD Syntax

Started by
79 comments, last by 21st Century Moose 10 years, 5 months ago
Unfortunately, C/C++ are a little flexible in their definition of what constitutes a boolean value...

Well, as usual, the C++ committee took great care to avoid actually saying anything while saying a lot of things, but I think you can still deduct the de-facto definition from what they say. It's amazing how you can write 1,300 pages without laying something very simple down in a clear, understandable, non-ambiguous manner. biggrin.png

Values of type bool are either true or false.

and

A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

Great. Very nice, except we don't know what true and false are. This isn't mentioned anywhere within those 1,300 pages. For all we know, false could be 354357 and true could be 77342.

However, in combination with this:

A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

one can pretty much safely presume that false == 0 and true == 1, even though again it doesn't explicitly say so. Converting to bool and converting from bool converts to/from 0 and 1, respectively.

Of course, given this wording, it is still technically allowable to have different values and add extra code for conversion (similarly, it is perfectly allowable for a null pointer to be something different from zero, even though the literal zero explicitly converts to the null pointer).

But even though technically allowable, it's not something that really makes sense. The values 0 and 1 are as good as any other values, and they do not require extra code for a conversion that has no benefit.

Further, it is explicitly allowable to store a bool in a bit-field, and in this case no "implementation specific" value that isn't exactly 1 would work for true. Neither would (given only two possible values in a single bit) any other value but 0 work for false, since 1 is already taken.

Advertisement

one can pretty much safely presume that false == 0 and true == 1, even though again it doesn't explicitly say so.

Fair enough.

However, the code sample I was commenting on didn't indicate that the values being operated on were actually booleans. It's not uncommon for legacy code to perform boolean operations on integers, for example.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

When I was a newer programmer, it felt unintuitive to me that -1 == true. I just naturally assumed positive numbers were true, and zero + negative numbers were false. smile.png

I've used machines where true == ~false (ie. ~0 == -1 in two's complement) because that was the most efficient way to encode it in the machine instructions. A conversion to the integral value 1 would take extra instructions, but it's a corner case. Logical operations are performed on booleans far far more often than arithmetic operations are.

Stephen M. Webb
Professional Free Software Developer


For the record, I know at least one language where it is indeed the case (it only pays attention to the lowest bit instead of the whole value).

But it wouldn't be bitwise anymore, then, right?

It makes AND, OR and XOR behave as both logical and bitwise, since technically a bitwise operation will still affect the lowest bit exactly the same way. It gets rid of the risk of mixing both variants by simply not having them (the bitwise variant now works for both cases, so the logical one is ditched).

EDIT: actually NOT too. No more need to have separate NOT and complement operators either.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.


if (x & y & funcz(a)) {
// do shirt here
}
That conditional almost certainly doesn't do what you meant it to do tongue.png

Hint: bitwise & is not the same as logical &&.

The conditional wasnt meant to do anything but be some arbitrary condition for a formating example (so it did exactly what I meant it to do )

I never use Bitwise operators on single value Boolean variables or conditional clauses , But have regularly used bit-wise operators in my coding as a way to compact data in games (single bit flags in sets of flags for objects) . I also have judiciously used the pack settings (on compilers that actually implement them)....

How large do the C/C++ compliles make a single Boolean variable these days 32 bits, 8 at best? Blasting needlessly oversized object structures through a network wasnt conducive to efficiency (including possibly Page Fault stallings in general)

Some might say that is a time for size tradeoff, but dont the out-of-order processing of instructions cancel most fo that these days?

NOTE - I also did exclusively X86 coding so dont read me the usual stuff about endians and compiler differenced in packing

Since someone else brought it up I also #define'd my own constants TRUE 1 FALSE 0 simply to make that clear and also to make the values use stand out in code (though in bitwise masking I often used hex 0x0001 and such forrmats to make what was going on clear also)

--------------------------------------------[size="1"]Ratings are Opinion, not Fact

Curly brace arguments. Keeping programmers busy since 1969. tongue.png

Besides, the only correct brace style is the one I use.

Which one do you use eh?

View my game dev blog here!


Which one do you use eh?

Quantum braces.

They are of indeterminate style until observed, at which point they concretely represent a randomly-selected style.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Schrödinger's braces?

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

Schrödinger's braces?

They look like this: \(\langle \text{code} \rangle \)

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

This topic is closed to new replies.

Advertisement