Why K & R did &&, || logical and &, | bitwise, and not vice versa?

I know that == was chosen for equality and = for assignment, because they thought that people were doing more tasks than checking for equality --- but, of course, they didn’t think there would be more bits than logical logic?

Why not go the other way around: & and | are logical operators?

+6
source share
1 answer

From the horse's mouth :

Quick changes continued after the language was named, for example, the introduction of the && and || . In BCPL and B, evaluating expressions depends on the context: inside if and other conditional statements that compare the value of an expression with zero, these languages ​​put a special interpretation into the and ( & ) and / ( | ) operators. In normal contexts, they work dimensionally, but in expression B
 if (e1 & e2) ... 
the compiler must evaluate e1 , and if it is nonzero, evaluate e2 , and if it is also not equal to zero, specify an instruction that depends on if. The requirement converges recursively to & and | operators within e1 and e2 . The semantics of short circuits of Boolean operators in such a β€œtruth” context seemed desirable, but operator overloading was difficult to explain and use. At the suggestion of Alan Snyder, I introduced the operators && and || to make the mechanism more explicit.

Remember that C was not created in a vacuum; most of its oddity can be attributed to BCPL and B.

+9
source

Source: https://habr.com/ru/post/916966/


All Articles