& in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

& in python

why is bool(1&2) false aren't both numbers true

1st Nov 2017, 1:01 PM
Hasan Fares
Hasan Fares - avatar
7 Answers
+ 8
& is a bitwise AND 1 in 4 bit binary is 0001 2 in 4 bit binary is 0010 0001 0010 & -------- 0000 = 0 = false With a bitwise & both bits must be 1 to result in 1 otherwise the bit is 0
1st Nov 2017, 1:20 PM
ChaoticDawg
ChaoticDawg - avatar
+ 9
thanks for clearing that up @sayan I will sure do
1st Nov 2017, 8:30 PM
Hasan Fares
Hasan Fares - avatar
+ 7
thanks now I understood
1st Nov 2017, 1:21 PM
Hasan Fares
Hasan Fares - avatar
+ 7
@shaikh salman you mean bool(1&2) ,bool(1|2) ,I mean isn't "and" , and "or" in their literal form do normal logical operations ,while binary operators &,| do binary comparison , please correct me if I'm wrong I really thought I got it.
13th Nov 2017, 11:32 AM
Hasan Fares
Hasan Fares - avatar
+ 6
.... 1 = 01 2 = 10 and(&) together go to "0" ....? bool(1 and 2) perhaps? bc & is binary operator
1st Nov 2017, 1:19 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
0
& is logical AND as in AND gates ## Truth table.. A B Y=A.B 0 0 0 1 0 0 0 1 0 1 1 1 bool of 0 is false bool of anything else is true(positive or negetive number) other logical opeeators are >> << ~ | ^ (google them)
1st Nov 2017, 2:14 PM
sayan chandra
sayan chandra - avatar
0
1&2 -> 0001 & 0010 -> (0.0)(0.0)(0.1)(1.0) -> 0000-> false
1st Nov 2017, 2:15 PM
sayan chandra
sayan chandra - avatar