classification
Title: Logical Error
Type: behavior Stage: resolved
Components: Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Kshitish, tim.peters
Priority: normal Keywords:

Created on 2020-11-25 04:19 by Kshitish, last changed 2020-12-11 18:28 by eric.araujo. This issue is now closed.

Files
File name Uploaded Description Edit
0_PROOF.png Kshitish, 2020-11-25 04:19 The prooof
main.py Kshitish, 2020-12-09 06:18
Messages (4)
msg381787 - (view) Author: Nishant Gautam (Kshitish) * Date: 2020-11-25 04:19
The python programming give wrong output. If any algorithm develop in python then, may be that will be affected by this.
msg381790 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2020-11-25 04:56
There's no bug here.

"&" is the bitwise Boolean logical-and operator on integers. For example,

>>> 1 & 2
0
>>> 1 & 3
1

It binds more tightly than the "==" equality-testing operator. To get the result you want, you would need to add more parentheses to override the natural operator precedence:

>>> (a == 10) & (not(a!=b)) & (b == 10)
True

But, more to the point, what you probably really want is the "and" logical operator, not the bitwise integer "&" operator:

>>> a == 10 and (not(a!=b)) and b == 10
True
msg382787 - (view) Author: Nishant Gautam (Kshitish) * Date: 2020-12-09 06:18
a = 10

b = 10


if (a >= b) & (a & b) & (a == b):

        print("Yes")

else:

        print("No")

Output: No

This is the bug because all then condition in if is true but it print No, This is the vulnerability of this code.
msg382788 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2020-12-09 06:29
Please ask for help on StackOverflow or the general Python mailing list. Your understanding of the operations is incorrect.

"&" is NOT a logical operator ("and" is). It's a bitwise operator on integers.

>>> 10 & 10
10
>>> bin(10)
'0b1010'

The last bit is 0, so when you "&" the result with True (which is equivalent to integer 1) the result is 0:

>>> (10 & 10) & True
0

and the integer 0 is treated as False in a Boolean context.
History
Date User Action Args
2020-12-11 18:28:37eric.araujosethgrepos: - hgrepo394
2020-12-09 06:29:09tim.peterssetstatus: open -> closed
resolution: not a bug
messages: + msg382788
2020-12-09 06:18:34Kshitishsetstatus: closed -> open
files: + main.py
versions: + Python 3.6, Python 3.7, Python 3.9
messages: + msg382787

resolution: not a bug -> (no value)
2020-11-25 04:56:28tim.peterssetstatus: open -> closed

nosy: + tim.peters
messages: + msg381790

resolution: not a bug
stage: resolved
2020-11-25 04:19:56Kshitishcreate