[Tutor] Preference to Bitwise operators in comparison to Logical operators

Matt Ruffalo mruffalo at cs.cmu.edu
Thu Sep 10 19:26:53 EDT 2020


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On 10/09/20 13:51, Manprit Singh wrote:
> Dear sir ,
>
> Just need to ask you a question .
> As we all know True is 1 and False is 0 .
> Now
> (1)  if the operands are True or  False  to logical operators and ,
or, not
>  (2) and if operands are 1 or 0 to bitwise operators  x & y, x | y 
and x ^
> 1
> serves the same purpose , only difference is Logical operators will return
> either True or False in this particular case .
>
> Secondarily Bitwise operator gives extra operators  - XOR & Shifts
> Secondarily bitwise operators are faster too.
>
> What are your views on this? Kindly Guide .
Note that 'and' and 'or' are not technically Boolean operators; they are
*control flow* operators. These return one of the operands, not
necessarily True or False. (With the exception of the 'not' operator,
which does not bother with trying to construct an "inverse" instance of
whatever type it is given.)

See:
"""
>>> 2 or 3
2
>>> 2 and 3
3
>>> 0 or 'yes'
'yes'
>>> 0.0 and 'no'
0.0
"""

MMR...
-----BEGIN PGP SIGNATURE-----

iHUEARYKAB0WIQQWM9A0jbPocR1dcwvzzCpWYzEsSAUCX1q2OAAKCRDzzCpWYzEs
SJEgAP0X9i+BulFohsHLWlkocbphqNRlZvNm2xWaKZAjXXl1XwEAoujheN5wHFEX
WndyK0C30tV2bndTjp58arjn3E7pUwI=
=9um1
-----END PGP SIGNATURE-----



More information about the Tutor mailing list