pythonbit-manipulationshift

bitwise shift output a wrong result


I am finishing my Informatics assignment and I wrote these for my question:

Create a variable s with a value of 154 and a variable p with a value of 6. Display its values in decimal and binary form on the screen.

s=154
p=6
bs=bin(s)
bp=bin(p)
print("In decimal form, s =",s,"and p =",p)
print("In binary form, s =",bs[2:],"and p =",bp[2:])

Perform a bitwise OR operation on the variables s and p and write the result to the variable s. Output the value in decimal and binary form to the screen.

s=s|p
bsp=bin(s)
print("In decimal form, the value is",s)
print("In binary form, the value is",bsp[2:])

Perform the operation of bitwise shift to the right by 2 bits on the variables s and p with the recording of the results in the corresponding variables. Display the values in decimal and binary form on the screen.

#print(s)
sbutshift2digit=s>>2
binsshift=bin(sbutshift2digit)
pbutshift2digit=p>>2
binpshift=bin(pbutshift2digit)
print("for the operation of bitwise shift to the right by 2 bits on the variables s, the value in decimal is",sbutshift2digit)
print("for the operation of bitwise shift to the right by 2 bits on the variables s, the value in binary is",binsshift[2:])
print("\n")
print("for the operation of bitwise shift to the right by 2 bits on the variables s, the value in decimal is",pbutshift2digit)
print("for the operation of bitwise shift to the right by 2 bits on the variables s, the value in binary is",binpshift[2:])

Here's the result of my third part of code:

for the operation of bitwise shift to the right by 2 bits on the variables s, the value in decimal is 39
for the operation of bitwise shift to the right by 2 bits on the variables s, the value in binary is 100111


for the operation of bitwise shift to the right by 2 bits on the variables s, the value in decimal is 1
for the operation of bitwise shift to the right by 2 bits on the variables s, the value in binary is 1

The problem is, for the operation of bitwise shift to the right by 2 bits on the variables s, the value in decimal should be 38, not 39. I don't really know what goes wrong......


Solution

  • You're overriding your variables. After you perform the bitwise OR, the variable s is now 158, not 154. You're then performing your bitshift on this new value instead of the original one. To fix it you just need to reset s and p between your operations:

    s = 154
    p = 6
    bs = bin(s)
    bp = bin(p)
    print("In decimal form, s =", s, "and p =", p)
    print("In binary form, s =", bs[2:], "and p =", bp[2:])
    
    # Perform a bitwise OR operation on the variables s and p
    s = s | p
    bsp = bin(s)
    print("In decimal form, the value is", s)
    print("In binary form, the value is", bsp[2:])
    
    # Reset your variables
    s = 154
    p = 6
    bs = bin(s)
    bp = bin(p)
    
    # Perform the operation of bitwise shift to the right by 2 bits on the variables s and p
    sbutshift2digit = s >> 2
    binsshift = bin(sbutshift2digit)
    pbutshift2digit = p >> 2
    binpshift = bin(pbutshift2digit)
    print("For the operation of bitwise shift to the right by 2 bits on the variable s, the value in decimal is", sbutshift2digit)
    print("For the operation of bitwise shift to the right by 2 bits on the variable s, the value in binary is", binsshift[2:])
    print("\n")
    print("For the operation of bitwise shift to the right by 2 bits on the variable p, the value in decimal is", pbutshift2digit)
    print("For the operation of bitwise shift to the right by 2 bits on the variable p, the value in binary is", binpshift[2:])