Search Header Logo
Bitwise operator

Bitwise operator

Assessment

Presentation

Computers

9th Grade

Easy

Used 4+ times

FREE Resource

16 Slides • 4 Questions

1

Logic and Bitwise operators in Python

By

2

Open Ended

¬How many times the loop will run according to the below code

word = "Python"

for letter in word:

    print(xletter)

3

Objectives

¬PC 3.1 Identify the logical operators AND, OR, and NOT and construct the truth table for logical operations within a statement or a computer program.

¬PC 3.2 Create code that utilizes bitwise and logical operators and evaluate its output

¬PC 3.3 Implement the correct syntax for a particular bitwise or logical operator according to a given scenario

¬PC 3.4 Evaluate expressions using De Morgan law by applying it within the code for a given scenario to demonstrate the negation of conjunction and disjunction

¬PC 3.5 Demonstrate understanding of bitwise operators as operators to deal with every bit separately where the arguments of these operators must be integers

¬PC 3.6 Perform calculations of logical operators and bitwise operators in both normal and appreciated/shorthand form

Some text here about the topic of discussion

4

​Overview of Python's Bitwise Operators

¬Python comes with a few different kinds of operators, such as the arithmetic, logical, and comparison operators.

¬Bitwise operations are performed on integers, and more specifically the underlying binary digits of an integer.

Subject | Subject

Some text here about the topic of discussion

5

​Overview of Python's Bitwise Operators

media

6

Bitwise AND

¬The operator symbol for AND is &. The statement is true (1) if the value of x and y are 1. Both values must be equal to 1. If only one variable is 1, the result is false (0).

Subject | Subject

Some text here about the topic of discussion

7

Bitwise AND

Example: 

a = 10 = 1010 (Binary)

b = 4 = 0100 (Binary)

a & b = 1010

& 0100

= 0000

= 0 (Decimal)

Subject | Subject

Some text here about the topic of discussion

8

Multiple Choice

What is the output of the following code

x = 1

y = 1

print(x & y)

1

True

2

False

9

Multiple Choice

What is the output of the following code?

x = 0

y = 1

print(x & y)

1

True

2

False

10

Bitwise OR

¬The operator symbol for OR is |. The statement is true (1) if either value of x or y are 1. Both values must be equal to 0 for the result to be 0. If both values are set to 1, the result is true (1). If both values are set to 0, the result is false (0).

Subject | Subject

Some text here about the topic of discussion

11

Bitwise OR

a = 10 = 1010 (Binary)

b = 4 = 0100 (Binary)

a | b = 1010

|

0100

= 1110

= 14 (Decimal).

Subject | Subject

Some text here about the topic of discussion

12

Multiple Choice

What is the output of the following Code?

x = 1

y = 0

print(x|y)

1

True

2

False

13

Bitwise NOT

¬The operator symbol for NOT is ~. This is a negation operator, meaning it is the opposite of the value. If x = 1, then ~x = 0. If y = 0, then ~y = 1. ~x can be read as NOT x, while ~y can be read as NOT y.

Subject | Subject

Some text here about the topic of discussion

14

Bitwise NOT

a = 10 = 1010 (Binary)

~a = ~1010

= -(1010 + 1) =

-(1011) =

-11 (Decimal)​

Subject | Subject

Some text here about the topic of discussion

15

Activity

# Python program to show

# bitwise operators​

a = 10

b = 4​

# Print bitwise AND operation

print("a & b =", a & b)​

# Print bitwise OR operation

print("a | b =", a | b)​

# Print bitwise NOT operation

print("~a =", ~a)

 

16

De Morgan's Law

De Morgan's law state that

"not (A and B)" is the same as "(not A) or (not B)"

and also,

"not (A or B)" is the same as "(not A) and (not B)"​

Subject | Subject

Some text here about the topic of discussion

17

media

De Morgan's Law

18

media

19

Activity

x = int(input("Enter a value for x: "))

y = int(input("Enter a value for y: "))

print ((not(x < 15 and y >= 3)))

print ((x >= 15 or y < 3))

Subject | Subject

Some text here about the topic of discussion

20

The End

media

Thank you :)

Logic and Bitwise operators in Python

By

Show answer

Auto Play

Slide 1 / 20

SLIDE