0

I am doing some calculations and I was wondering how you can invert a byte in python.

For example:

0b11111111 should be flipped to 0b00000000.

I have tried turning into a string and flipping it then but I cant somehow turn it back into a Integer.

Also, I want to add that the ~ does not work, as it simply just makes the same number but negative

5
  • 1
    Look at the ^ operator. ^ 0b11111111 Commented Jun 22, 2021 at 10:13
  • @trincot already tried that. It Simply shows as syntax error. Commented Jun 22, 2021 at 10:22
  • 1
    Does this answer your question? How do I do a bitwise Not operation in Python? Commented Jun 22, 2021 at 10:23
  • 1
    Syntax error? Did you look at the documentation? for instance: 0b10101 ^ 0b11111111 == 0b11101010 Commented Jun 22, 2021 at 10:28
  • @trincot Okay I got! a = 0b11111111^0b00101010 print(bin(a)) This gives me The opposite Commented Jun 22, 2021 at 10:30

3 Answers 3

1
 bin(0b11010111 ^ 0b11111111) // bit subtraction
 # Result
 # bin(0b11010111 ^ 0b11111111)
 #'0b101000'
 # for each bit subtraction with 1 occur
Sign up to request clarification or add additional context in comments.

2 Comments

Could you add an explanation of your solution?
I added comments to solution
1

To explain why ~ "dit not work": you want to invert all bits in a byte (8-bits), but the 0b.... value is not a byte, but an int which has more bits than just 8. ~var inverts all bits in an integer. You can clear all bits except the lowest 8 with a mask to get the result you expect:

MASK8 = 0b11111111  # or 255 decimal

v = 0b11101110                                                                                                                                                    
inv = ~v & MASK8    # 0b10001

Comments

0
def flippingBits(n):
    binary_list=list(bin(n))[2::]
    binary_32=''
    binary_result=''
    for item in binary_list:
        if item=='1':
            binary_32+='0'
        else:
            binary_32+='1'
    if 32-len(binary_list)>0:
        for i in range(32-len(binary_list)):
            binary_result+='1'
        binary_result+=binary_32
    else:
        binary_result=binary_32
    result=int(binary_result, 2)
    return result

This is a solution a made for a flipping bits in hacker rank: The function receive an unsigned int, turn this number into a binary representation and flip all the bits in a 32 bits representation, then return the unsigned int number corresponding to the remaining binary number. First I change into the binary representation using the bin() function, then i tansform that into a string with a for loop but changing every bit for the opposite(flipping), last I concatenate the string with the difference between the string length and the 32 spaces I need (32 bits), adding 1s to the rest of the string. Last I change into an integer using int(number, base) function. I hope it helps you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.