0

I want to do a bit operation, and need some help:

I have a word of 16 bit and want to split it into two, reverse each and then join them again.

Example if i have 0b11000011

First I divide it into 0b1100 and 0b0011

Then i reverse both getting 0b0011 and 0b1100

And finally rejoin them getting 0b00111100

Thanks!

2 Answers 2

3

Here's one way to do it:

def rev(n):
    res = 0
    mask = 0x01
    while mask <= 0x80:
        res <<= 1
        res |= bool(n & mask)
        mask <<= 1

    return res

x = 0b1100000110000011
x = (rev(x >> 8) << 8) | rev(x & 0xFF)
print bin(x) # 0b1000001111000001

Note that the method above operates on words, not bytes as example in the question.

Sign up to request clarification or add additional context in comments.

Comments

1

here are some basic operations you can try, and you can concatenate results after splitting your string in two and reversing it

a = "0b11000011"  #make a string 

b = a[:6]         #get first 5 chars

c = a[::-1]       # invert the string

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.