In article <11dt…@dont-email.me>, Arne Vajhøj <ar…@vajhoej.dk> wrote:
>On 11/21/2026 6:58 PM, Dan Cross wrote:
>> In article <6b02…@news.sunsite.dk>, >> Arne Vajhøj <ar…@vajhoej.dk> wrote:
>>> On 11/20/2026 3:24 PM, Arne Vajhøj wrote:
>>>> On 11/20/2026 3:10 PM, Simon Clubley wrote:
>>>>> [snip]
>>>> Doesn't C require/mandate short circuiting in cases like this??
>>> >>> I believe the VMS landscape looks like: >>> >>> Undefined(*) L to R L to R >>> all short circuit >>> [snip] >>> VMS Fortran .and. >>> .or. >>> VMS Pascal and and_then >>> or or_else >>> C & && >>> | || >>> Ada and and then >>> or or else >>> Java & && >>> | || >>> Python & and >>> | or >>> PHP & && >>> | || >>> and(**) >>> or(**) >>> [snip]
>> >> For C, Java, Python, and (probably?) PHP, you are mixing up >> different categories of operators: `&` and `|` are bitwise >> operators in those languages, while `&&` and `||` are boolean >> operators. Since they are intended to do differnt things, the >> semantics are different.
> >Those 4 are a bit different. > >Java has both bitwise & and | on integer types and logical & and | on >booleans.
Eh...kinda. It's true that Java has well-defined bitwise operators on booleans, but it's really just expressing the integer rules for a type that can only have two values. It's still a bitwise-operation.
>In C and Python bool are sort of an int so bitwise and logical >are the same. > >PHP just auto converts booleans to ints for bitwise operator. > >But all 4 quacks like a duck. >
>> I doubt that there are many many languages that would introduce >> short-circuiting behavior for bitwise operators. But note also >> that we may see behaviors like this: >> >> ```c >> #include <stdio.h> >> int >> main(void) >> { >> int a = 1, b = 2; >> >> if (a) >> printf("a is true\n"); >> if (b) >> printf("b is true\n"); >> if (a & b) >> printf("both are true\n"); >> else >> printf("but `&` is not a boolean operator\n"); >> >> return 0; >> } >> ```
> >Good point. > >The problem here is the use of int instead of bool.
Incorrect.
>#include <stdio.h> >#include <stdbool.h> > >int main() >{ > bool a = 1, b = 2;
This does not do what you think that it does.
> if (a) > printf("a is true\n"); > if (b) > printf("b is true\n"); > if (a & b) > printf("both are true\n"); > else > printf("but `&` is not a boolean operator\n"); > > return 0; >} > >Outputs a different result.
Because in this case, `a` and `b` are both equal to (integer) 1. The assignment `bool b = 2` sets `b` to `true` (any non-zero integer constant is "true" in a boolean context in C), and `true` has the value 1 (C23, sec 6.4.4.6, para 3). An object of type `bool` in C has exactly one value-holding bit, and `sizeof(bool)*CHAR_BIT-1` padding bits (C23, sec 6.2.6.2, para 1). When applied to a bitwise operator like `&` or `|`, both `a` and `b` will first be converted to a (signed) `int` based on the rank of the `bool` type and the rules for integer promotions (C23, sec 6.3.1.1). ``` term% cat bitwise.c #include <stdbool.h> #include <stdio.h> int main(void) { bool a = 1, b = 2; if (a) printf("a is true\n"); if (b) printf("b is true\n"); if (a & b) printf("both are true (a = %d, b = %d)\n", a, b); else printf("but `&` is not a boolean operator\n"); return 0; } term% make bitwise cc -O2 -pipe -o bitwise bitwise.c term% ./bitwise a is true b is true both are true (a = 1, b = 1) term% ```
>But treating int's as booleans is an integrated part >of C.
In C, `bool` is an integer type. The usual arithmetic conversions apply, including the integer promotion rules.
>And it has spilled over to many other languages. > >So yes - if working with int's instead of bool's, >then bitwise operators can not be used as logical >operators.
No. In all cases, bitwise operators are doing bitwise operations. They are simply not boolean operators, even if applied to boolean values. - Dan C.