Languages like i.e. Java and C# have both bitwise and logical operators.
Logical operators make only sense with boolean operands, bitwise operators work with integer types as well. Since C had no boolean type and treats all non-zero integers as true, the existence of both logical and bitwise operators makes sense there. However, languages like Java or C# have a boolean type so the compiler could automatically use the right kind of operators, depending on the type context.
So, is there some concrete reason for having both logical and bitwise operators in those languages? Or were they just included for familiarity reasons?
(I am aware that you can use the "bitwise" operators in a boolean context to circumvent the short-circuiting in Java and C#, but i have never needed such a behaviour, so i guess it might be a mostly unused special case)
1) is there some concrete reason for having both logical and bitwise operators in those languages?
Yes:
2) I am aware that you can use the "bitwise" operators in a boolean context to circumvent the short-circuiting in Java and C#,
For as far as C# goes this simply is not true.
C# has for example 2 boolean AND operators: &
(full) and &&
(short) but it does not allow bitwise operations on booleans.
So, there really is no 'overlap' or redundancy between logical and bitwise operators. The two do not apply to the same types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With