Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A common and very useful application of bitwise operators is in the creating and

ID: 3886870 • Letter: A

Question

A common and very useful application of bitwise operators is in the creating and manipulation of bit fields and bit masks. A bit field is a binary word where each bit represents the state of some property in the code, and a bit mask is a bit field that is used to perform parallel operations on multiple bits within another bit field. For example, recall the "Nintendo controller pad" lab in ENME351, where there were 4 buttons defining up, down, left, and right movement. Instead of creating separate variables to record the state of each button, using a bit field provides a much cleaner approach. Consider the following Python variable declarations: b_status = int('1010', 2) # button states (start with right & down on) b_up = int('0001', 2) b_down = int('0010', 2) b_left = int('0100', 2) b_right = int('1000', 2) Here b_status will hold all button states in a single 4-bit field, while the remaining variables define which bit in the field will hold each button state. Based on our discussion about bit fields in class, write Python code with the above global variables, and add the functions given below. For each functions, use the button variables (b_status, b_up, b_down, bjeft, b_right) and one or more logical operators in a single line of code. Note that you may need to add a return() statement, and that for some functions you will also need to either use global to access the button status variable, or pass the status as an argument to your function. setOn(b): # Set a given button state to ON. setOff(b): # Set a given button state to OFF. flip(b): # Flip the state of a button state (0 rightarrow 1 or 1 rightarrow 0), regardless of its initial state. invert(): # Invert all of the bit field properties from their current states. testOn(b): # Test if a given button is ON or OFF using a single assignment # (no //statements) that will return 1 if the button is ON, and 0 if it is OFF. Test your code with a few initial bit states to make sure that every function works, and submit your final code as p2.py.

Explanation / Answer

Make sure indentation when copying the code

#Global Variable declaration

b_status = int('1010',2)
b_up = int('0001',2)
b_down = int('0010',2)
b_right = int('0100',2)
b_left = int('1000',2)

#*************************************
# Have a look on bit reprasentation *
# b3 b2 b1 b0 *
# 1 0 1 0 *
#*************************************

#Turn on perticular bit
def setOn(b):
global b_down,b_right,b_up,b_status,b_left
if(b==0):
b_status= b_status | b_up
elif(b==1):
b_status= b_status | b_down
elif(b==2):
b_status= b_status | b_right
elif(b==3):
b_status= b_status | b_left
else:
print("Invalid input")
return
#note u can do b_status = b_status | (b_up<<b)
##############################################
#Trun off perticular bit
def setOff(b):
global b_status,b_up
b_status = b_status & (b_up<<b)
return
#########################################
#Flip the perticular bit
def flip(b):
global b_up,b_status
b_status = b_status ^ (b_up<<b)
return

#Inverting the all bits
def invert():
global b_status
b_status = ~(b_status)
return
#####################################
#Check status of bit
def testOn(b):
global b_status,b_up

return b_status & (b_up<<b)

################################################
#****This is testing purpose*******************

print("Before SetON b_status is :{}".format(bin(b_status)))
setOn(2) #calling function
print("After SetON b_status is :{}".format(bin(b_status)))

print("Before SetOff b_status is :{}".format(bin(b_status)))
setOff(3) #calling function
print("After SetOff b_status is :{}".format(bin(b_status)))

print("Before invert b_status is :{}".format(bin(b_status)))
invert()#calling function
print("After invert b_status is :{}".format(bin(b_status)))

print("Before flip b_status is :{}".format(bin(b_status)))
flip(2) #calling functio
print("Before flip b_status is :{}".format(bin(b_status)))

if testOn(1): #calling function
print("Bit is ON")
else:
print("Bit is OFF")
#*************************************************

sample out put:

Before SetON b_status is :0b1010
After SetON b_status is :0b1110
Before SetOff b_status is :0b1110
After SetOff b_status is :0b1000
Before invert b_status is :0b1000
After invert b_status is :-0b1001
Before flip b_status is :-0b1001
Before flip b_status is :-0b1101
Bit is ON

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote