The Self-Taught Computer Scientist


Download 1.48 Mb.
Pdf ko'rish
bet59/147
Sana17.06.2023
Hajmi1.48 Mb.
#1540634
1   ...   55   56   57   58   59   60   61   62   ...   147
Bog'liq
books-library.net-11301817Az7X6

Chapter 6 Math
67
If both sides are 
False
, it returns 
False
.
print(1==2 and 2==3)
>> False
If one side is 
True
and the other is 
False
, it returns 
False
as well.
print(1==1 and 1==2)
>> False
Let’s look at an example of using bitwise AND. Say you have two integers, 2 and 3. Two in binary 
is 0b10, and 3 is 0b11. The first bit of 2 is 0, and the first bit of 3 is 1.
10 # 2
11 # 3
— —
0
Applying bitwise AND to these bits produces 0 because there is a 
True
and a 
False
, which returns 
False
(remember: 0 is 
False
and 1 is 
True
). Applying bitwise AND to the second set of bits produces 
1 because both binary digits are 
True
, so Python returns 
True
.
10 # 2
11 # 3
— —
10
In this case, the bitwise AND operation produces 0b10, which is the number 2 in binary (you will 
learn why this is useful shortly).
In Python, the bitwise AND operator is the ampersand symbol (&). Here is how you can use bitwise 
AND on the binary numbers 0b10 and 0b11 in Python:
print(0b10 & 0b11)
>> 2
You don’t have to use a bitwise operator like bitwise AND on binary numbers.
print(2 & 3)
>> 2
In this case, you evaluated bitwise AND on decimal numbers, but Python uses the binary values 
for 2 and 3 to carry out the operation.


Introduction to Algorithms
68
Python also has a bitwise OR operator, which operates bit by bit and returns 1 when one or more 
of the two bits is 
True
and returns 
False
when both are 
False
, just like the 
or
keyword in Python. 
For example, let’s take a look at what happens when you use the bitwise OR operator on the numbers 
2 and 3. Applying bitwise OR to the first two bits produces 1 because one of the bits is 
True
.
10 # 2
11 # 3
— —
1
When you use bitwise OR on the second set of bits, Python returns 1 because both bits are 
True
(1).
10 # 2
11 # 3
— —
11
As you can see, the result of bitwise OR on 2 and 3 is 0b11, which is the decimal number 3.
In Python, the bitwise OR operator is the pipe symbol (|).
print(2 | 3)
>> 3
The binary operators you’ve learned about so far are some of the most common; however, there are 
other binary operators like bitwise XOR, bitwise NOT, bitwise right shift, and bitwise left shift you 
can learn about in Python’s documentation.
Let’s take a look at some situations where bitwise operators are helpful. You can use the bitwise 
AND operator to check whether a number is even or odd. An even number like 2 always has a 0 at 
the end, whereas the number 1 always has a 1 at the end (and contains only one binary digit: 1).
10 # 2
1 # 1
When you use bitwise AND on an even number and 1, Python will always return 
False
because 
the even number will end with a 0, and 1 has only one binary digit: 1.
10 # 2
1 # 1
--
0
On the other hand, when you use bitwise AND on an odd number and 1, Python will always return 
True
because the odd number will end with a 1, and 1 has only one binary digit: 1.



Download 1.48 Mb.

Do'stlaringiz bilan baham:
1   ...   55   56   57   58   59   60   61   62   ...   147




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling