SlideShare a Scribd company logo
Module 2
Data Collections – Lists, Tuples,
and Dictionaries
Logic and bit operations
Module 2 Logic and bit operations
Computer logic
Argument A Argument B A and B
False False False
False True False
True False False
True True True
and
not
Argument not Argument
False True
True False
or
Argument A Argument B A or B
False False False
False True True
True False True
True True True
Module 2 Logic and bit operations
Bitwise operators
• & (ampersand) - bitwise conjunction
• | (bar) - bitwise disjunction
• ~ (tilde) - bitwise negation
• ^ (caret) - bitwise exclusive or (xor)
Argument A Argument B A & B A | B A ^ B
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Bitwise operations (&, |, and ^)
Bitwise operations (~)
Argument ~ Argument
0 1
1 0
Module 2 Logic and bit operations
Logical vs. bit operations
i = 15
j = 22
i: 00000000000000000000000000001111
j: 00000000000000000000000000010110
log = i and j
log: True
bit = i & j
i 00000000000000000000000000001111
j 00000000000000000000000000010110
bit = i & j 00000000000000000000000000000110
Module 2 Logic and bit operations
How do we deal with single bits
flag_register = 0x1234
flag_register = 0000000000000000000000000000x000
Check the state of
your bit
• x & 1 = x
• x & 0 = 0
Reset your bit
• flag_register =
flag_register &
~the_mask
• flag_register &=
~the_mask
Set your bit
• x | 1 = 1
• x | 0 = x
Negate your bit
• x ^ 1 = ~x
• x ^ 0 = x
Module 2 Logic and bit operations
Binary left shift & binary right shift
value << bits
value >> bits
var = 17
var_right = var >> 1
var_left = var << 2
print(var, var_left, var_right)
17 68 8
Priority Operator
1 ˜ , + , - unary
2 **
3 * , / , // , %
4 + , - binary
5 << , >>
6 <, <= , > , >=
7 == , !=
8 &
9 |
10 =, +=, -=, *=, /=, %=, &=, ˆ=, |=, >>=, <<=
Module 2 Logic and bit operations
Key takeaways
Logical operators:
• and, or, not
To use bitwise operators to manipulate single bits
of data:
• x = 15, which is 0000 1111 in binary
• y = 16, which is 0001 0000 in binary
• & and, | or, ˜ not, ˆ xor, >> right shift, << left shift

More Related Content

What's hot (20)

PDF
IRJET - Approximate Unsigned Multiplier with Varied Error Rate
IRJET Journal
 
PDF
8. operators
Way2itech
 
PDF
High Performance Baugh Wooley Multiplier Using Carry Skip Adder Structure
IRJET Journal
 
PDF
Alu app b
Sathish Kumaravelu
 
PPT
1 arithmetic
fyjordan9
 
PPT
Bit manipulation
Priyanka Yadav
 
PPTX
Bit manipulation
UthraSowrirajan1
 
PDF
Ashish garg research paper 660_CamReady
Ashish Garg
 
PPTX
Basic operators in matlab
rishiteta
 
PPT
Computer architecture
Sanjeev Patel
 
PPTX
Computer arithmetic
Balakrishna Chowdary
 
PDF
Design of 32-bit Floating Point Unit for Advanced Processors
IJERA Editor
 
PDF
Design and Simulation of Radix-8 Booth Encoder Multiplier for Signed and Unsi...
ijsrd.com
 
PDF
IRJET- Realization of Decimal Multiplication using Radix-16 Modified Booth En...
IRJET Journal
 
PPTX
Lecture1a data types
mbadhi barnabas
 
PPT
Binary parallel adder, decimal adder
shahzad ali
 
PDF
IRJET-Design and Implementation of LNS based Approximate Multiplier using Mit...
IRJET Journal
 
PDF
SINGLE PRECISION FLOATING POINT MULTIPLIER USING SHIFT AND ADD ALGORITHM
AM Publications
 
PDF
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Alpro
 
PDF
C++ Expressions Notes
Prof Ansari
 
IRJET - Approximate Unsigned Multiplier with Varied Error Rate
IRJET Journal
 
8. operators
Way2itech
 
High Performance Baugh Wooley Multiplier Using Carry Skip Adder Structure
IRJET Journal
 
1 arithmetic
fyjordan9
 
Bit manipulation
Priyanka Yadav
 
Bit manipulation
UthraSowrirajan1
 
Ashish garg research paper 660_CamReady
Ashish Garg
 
Basic operators in matlab
rishiteta
 
Computer architecture
Sanjeev Patel
 
Computer arithmetic
Balakrishna Chowdary
 
Design of 32-bit Floating Point Unit for Advanced Processors
IJERA Editor
 
Design and Simulation of Radix-8 Booth Encoder Multiplier for Signed and Unsi...
ijsrd.com
 
IRJET- Realization of Decimal Multiplication using Radix-16 Modified Booth En...
IRJET Journal
 
Lecture1a data types
mbadhi barnabas
 
Binary parallel adder, decimal adder
shahzad ali
 
IRJET-Design and Implementation of LNS based Approximate Multiplier using Mit...
IRJET Journal
 
SINGLE PRECISION FLOATING POINT MULTIPLIER USING SHIFT AND ADD ALGORITHM
AM Publications
 
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Alpro
 
C++ Expressions Notes
Prof Ansari
 

Similar to Python PCEP Logic Bit Operations (20)

PPT
dld.ppt
DagneMulu1
 
DOC
Report on c
jasmeen kr
 
PPTX
Boolean+logic
Bilal Maqbool ツ
 
PDF
Bitwise
Axel Ryo
 
PDF
Bitwise Operations(1).pdf
DalvinCalvin
 
PDF
Digital notes
stivengo2
 
PPTX
Lecture 07.pptx
Mohammad Hassan
 
PPTX
Verilog operators.pptx
VandanaPagar1
 
PPT
lecture_binary_logic_and_logic_gates.ppt
SherifElGohary7
 
PPT
Bitwise Operators in C
yndaravind
 
PPTX
Python notes for students to develop and learn
kavithaadhilakshmi
 
PPT
Al2ed chapter9
Abdullelah Al-Fahad
 
PDF
Csc 2313 (lecture 4)
umardanjumamaiwada
 
PPT
3A.ppt
TamilSelvi165
 
PPT
Operators
Devi Pradeep Podugu
 
PPT
3A.ppt
CheeneeRivera
 
PPTX
Verilog operators
Dr.YNM
 
PPTX
1 Standard Data types.pptx
ssuser8e50d8
 
PDF
Simplification of Circuits.pdf
MaryJacob24
 
PPT
Week - 01, 02, 03 Bits-n-Pieces Chapter 1.ppt
TALHA RIAZ PERSOTA
 
dld.ppt
DagneMulu1
 
Report on c
jasmeen kr
 
Boolean+logic
Bilal Maqbool ツ
 
Bitwise
Axel Ryo
 
Bitwise Operations(1).pdf
DalvinCalvin
 
Digital notes
stivengo2
 
Lecture 07.pptx
Mohammad Hassan
 
Verilog operators.pptx
VandanaPagar1
 
lecture_binary_logic_and_logic_gates.ppt
SherifElGohary7
 
Bitwise Operators in C
yndaravind
 
Python notes for students to develop and learn
kavithaadhilakshmi
 
Al2ed chapter9
Abdullelah Al-Fahad
 
Csc 2313 (lecture 4)
umardanjumamaiwada
 
Verilog operators
Dr.YNM
 
1 Standard Data types.pptx
ssuser8e50d8
 
Simplification of Circuits.pdf
MaryJacob24
 
Week - 01, 02, 03 Bits-n-Pieces Chapter 1.ppt
TALHA RIAZ PERSOTA
 
Ad

More from IHTMINSTITUTE (19)

PPTX
Python PCEP Tuples and Dictionaries
IHTMINSTITUTE
 
PPTX
Python PCEP Tuples and Dictionaries
IHTMINSTITUTE
 
PPTX
Python PCEP Creating Simple Functions
IHTMINSTITUTE
 
PPTX
Python PCEP Functions And Scopes
IHTMINSTITUTE
 
PPTX
Python PCEP Function Parameters
IHTMINSTITUTE
 
PPTX
Python PCEP Functions
IHTMINSTITUTE
 
PPTX
Python PCEP Multidemensional Arrays
IHTMINSTITUTE
 
PPTX
Python PCEP Operations On Lists
IHTMINSTITUTE
 
PPTX
Python PCEP Sorting Simple Lists
IHTMINSTITUTE
 
PPTX
Python PCEP Lists Collections of Data
IHTMINSTITUTE
 
PPTX
Python PCEP Loops
IHTMINSTITUTE
 
PPTX
Python PCEP Comparison Operators And Conditional Execution
IHTMINSTITUTE
 
PPTX
Python PCEP How To Talk To Computer
IHTMINSTITUTE
 
PPTX
Python PCEP Variables
IHTMINSTITUTE
 
PPTX
Python PCEP Operators
IHTMINSTITUTE
 
PPTX
Python PCEP Literals
IHTMINSTITUTE
 
PPTX
IHTM Python PCEP Hello World
IHTMINSTITUTE
 
PPTX
IHTM Python PCEP Introduction to Python
IHTMINSTITUTE
 
PPTX
Python PCEP Welcome Opening
IHTMINSTITUTE
 
Python PCEP Tuples and Dictionaries
IHTMINSTITUTE
 
Python PCEP Tuples and Dictionaries
IHTMINSTITUTE
 
Python PCEP Creating Simple Functions
IHTMINSTITUTE
 
Python PCEP Functions And Scopes
IHTMINSTITUTE
 
Python PCEP Function Parameters
IHTMINSTITUTE
 
Python PCEP Functions
IHTMINSTITUTE
 
Python PCEP Multidemensional Arrays
IHTMINSTITUTE
 
Python PCEP Operations On Lists
IHTMINSTITUTE
 
Python PCEP Sorting Simple Lists
IHTMINSTITUTE
 
Python PCEP Lists Collections of Data
IHTMINSTITUTE
 
Python PCEP Loops
IHTMINSTITUTE
 
Python PCEP Comparison Operators And Conditional Execution
IHTMINSTITUTE
 
Python PCEP How To Talk To Computer
IHTMINSTITUTE
 
Python PCEP Variables
IHTMINSTITUTE
 
Python PCEP Operators
IHTMINSTITUTE
 
Python PCEP Literals
IHTMINSTITUTE
 
IHTM Python PCEP Hello World
IHTMINSTITUTE
 
IHTM Python PCEP Introduction to Python
IHTMINSTITUTE
 
Python PCEP Welcome Opening
IHTMINSTITUTE
 
Ad

Recently uploaded (20)

PPTX
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
PPTX
Softuni - Psychology of entrepreneurship
Kalin Karakehayov
 
PDF
FutureCon Seattle 2025 Presentation Slides - You Had One Job
Suzanne Aldrich
 
PPTX
PHIPA-Compliant Web Hosting in Toronto: What Healthcare Providers Must Know
steve198109
 
PPTX
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
PPTX
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
PDF
Digital burnout toolkit for youth workers and teachers
asociatiastart123
 
PDF
BRKACI-1003 ACI Brownfield Migration - Real World Experiences and Best Practi...
fcesargonca
 
PDF
Boardroom AI: The Next 10 Moves | Cerebraix Talent Tech
ssuser73bdb11
 
PPTX
西班牙巴利阿里群岛大学电子版毕业证{UIBLetterUIB文凭证书}文凭复刻
Taqyea
 
PPTX
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
PDF
BRKSP-2551 - Introduction to Segment Routing.pdf
fcesargonca
 
DOCX
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
PPTX
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
PPTX
Orchestrating things in Angular application
Peter Abraham
 
PDF
The Internet - By the numbers, presented at npNOG 11
APNIC
 
PDF
Cleaning up your RPKI invalids, presented at PacNOG 35
APNIC
 
PPTX
Networking_Essentials_version_3.0_-_Module_3.pptx
ryan622010
 
PPTX
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
PDF
Top 10 Testing Procedures to Ensure Your Magento to Shopify Migration Success...
CartCoders
 
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
Softuni - Psychology of entrepreneurship
Kalin Karakehayov
 
FutureCon Seattle 2025 Presentation Slides - You Had One Job
Suzanne Aldrich
 
PHIPA-Compliant Web Hosting in Toronto: What Healthcare Providers Must Know
steve198109
 
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
Digital burnout toolkit for youth workers and teachers
asociatiastart123
 
BRKACI-1003 ACI Brownfield Migration - Real World Experiences and Best Practi...
fcesargonca
 
Boardroom AI: The Next 10 Moves | Cerebraix Talent Tech
ssuser73bdb11
 
西班牙巴利阿里群岛大学电子版毕业证{UIBLetterUIB文凭证书}文凭复刻
Taqyea
 
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
BRKSP-2551 - Introduction to Segment Routing.pdf
fcesargonca
 
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
Orchestrating things in Angular application
Peter Abraham
 
The Internet - By the numbers, presented at npNOG 11
APNIC
 
Cleaning up your RPKI invalids, presented at PacNOG 35
APNIC
 
Networking_Essentials_version_3.0_-_Module_3.pptx
ryan622010
 
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
Top 10 Testing Procedures to Ensure Your Magento to Shopify Migration Success...
CartCoders
 

Python PCEP Logic Bit Operations

  • 1. Module 2 Data Collections – Lists, Tuples, and Dictionaries Logic and bit operations
  • 2. Module 2 Logic and bit operations Computer logic Argument A Argument B A and B False False False False True False True False False True True True and not Argument not Argument False True True False or Argument A Argument B A or B False False False False True True True False True True True True
  • 3. Module 2 Logic and bit operations Bitwise operators • & (ampersand) - bitwise conjunction • | (bar) - bitwise disjunction • ~ (tilde) - bitwise negation • ^ (caret) - bitwise exclusive or (xor) Argument A Argument B A & B A | B A ^ B 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 Bitwise operations (&, |, and ^) Bitwise operations (~) Argument ~ Argument 0 1 1 0
  • 4. Module 2 Logic and bit operations Logical vs. bit operations i = 15 j = 22 i: 00000000000000000000000000001111 j: 00000000000000000000000000010110 log = i and j log: True bit = i & j i 00000000000000000000000000001111 j 00000000000000000000000000010110 bit = i & j 00000000000000000000000000000110
  • 5. Module 2 Logic and bit operations How do we deal with single bits flag_register = 0x1234 flag_register = 0000000000000000000000000000x000 Check the state of your bit • x & 1 = x • x & 0 = 0 Reset your bit • flag_register = flag_register & ~the_mask • flag_register &= ~the_mask Set your bit • x | 1 = 1 • x | 0 = x Negate your bit • x ^ 1 = ~x • x ^ 0 = x
  • 6. Module 2 Logic and bit operations Binary left shift & binary right shift value << bits value >> bits var = 17 var_right = var >> 1 var_left = var << 2 print(var, var_left, var_right) 17 68 8 Priority Operator 1 ˜ , + , - unary 2 ** 3 * , / , // , % 4 + , - binary 5 << , >> 6 <, <= , > , >= 7 == , != 8 & 9 | 10 =, +=, -=, *=, /=, %=, &=, ˆ=, |=, >>=, <<=
  • 7. Module 2 Logic and bit operations Key takeaways Logical operators: • and, or, not To use bitwise operators to manipulate single bits of data: • x = 15, which is 0000 1111 in binary • y = 16, which is 0001 0000 in binary • & and, | or, ˜ not, ˆ xor, >> right shift, << left shift