George Boole pioneered a mathematical concept known as Boolean expressions, or B
ID: 3663702 • Letter: G
Question
George Boole pioneered a mathematical concept known as Boolean expressions, or Boolean algebra. Four of the basic Boolean operation are the AND, the OR, the XOR, and the NOT operations. Programs use these operations to evaluate the relationship between variables and make decisions based on those relationships. Comparing two or more variables, bitwise, results in either a True (1) or False (0) outcome.
Bitwise operations
Python operator
Example
AND
&
n & m
OR
|
a | b
XOR
^
e ^ f
Table 1 - Python Operators
to display a number
function
Example
in binary
bin(x)
bin(5) = 0b101
In hexadecimal
0x%x
‘0x%x’ %46 = 0x2e
Table 2 - Number Conversions
Boot up the Raspberry Pi (RPi).
Open IDLE.
Enter the program below.
Name = ‘Put Your Name Here’
Class= ‘ECT109’
Assignment = ‘Week 4 Lab 2’
#------------------------------------------------------------------
# Boolean bitwise AND operator
#------------------------------------------------------------------
print Name
print Class
print Assignment
print 'AND operator'
n = input(‘Enter 1st Number: ‘)
m = input(‘Enter 2nd Number: ‘)
print n, bin(n), '0x%x' % n
print m, bin(m), '0x%x' % m
x = n & m
print n, 'AND', m, ' = ', x, bin(x), '0x%x' % x
This Python program will perform the AND bitwise operator. Your assignment is to add to the above program the OR and XOR bitwise operators.
Verify your results.
Bitwise operations
Python operator
Example
AND
&
n & m
OR
|
a | b
XOR
^
e ^ f
Explanation / Answer
#------------------------------------------------------------------
# Boolean bitwise XOR operator
#------------------------------------------------------------------
print Name
print Class
print Assignment
print 'XOR operator'
n = input(‘Enter 1st Number: ‘)
m = input(‘Enter 2nd Number: ‘)
print n, bin(n), '0x%x' % n
print m, bin(m), '0x%x' % m
x = n ^ m
print n, 'XOR', m, ' = ', x, bin(x), '0x%x' % x
#------------------------------------------------------------------
# Boolean bitwise OR operator
#------------------------------------------------------------------
print Name
print Class
print Assignment
print 'OR operator'
n = input(‘Enter 1st Number: ‘)
m = input(‘Enter 2nd Number: ‘)
print n, bin(n), '0x%x' % n
print m, bin(m), '0x%x' % m
x = n | m
print n, 'OR', m, ' = ', x, bin(x), '0x%x' % x
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.