1

Given a Python integer which is within the size of 4 bits, how does one transform it – with bitwise arithmetic instead of string processing – into an integer within the size of 4 bytes, for which each bit in the original corresponds to a byte which is the bit repeated 8 times?

For example: 0b1011 should become 0b11111111000000001111111111111111

3 Answers 3

4

With apologies to ncoghlan:

expanded_bits = [
    0b00000000000000000000000000000000,
    0b00000000000000000000000011111111,
    0b00000000000000001111111100000000,
    0b00000000000000001111111111111111,
    0b00000000111111110000000000000000,
    0b00000000111111110000000011111111,
    0b00000000111111111111111100000000,
    0b00000000111111111111111111111111,
    0b11111111000000000000000000000000,
    0b11111111000000000000000011111111,
    0b11111111000000001111111100000000,
    0b11111111000000001111111111111111,
    0b11111111111111110000000000000000,
    0b11111111111111110000000011111111,
    0b11111111111111111111111100000000,
    0b11111111111111111111111111111111,
    ]

Then just index this list with the nibble you want to transform:

>>> bin(expanded_bits[0b1011])
"0b11111111000000001111111111111111"
Sign up to request clarification or add additional context in comments.

6 Comments

@John Machin -- OK, sure. But just so I understand, why?
@senderle table lookups are much faster.
@Keith, was what I had before John Machin's edit not a lookup table?
@senderle I don't know what you had before. Perhaps I'm jumping into the middle of something here? Now that I take a closer look I think I answered to wrong question.
@senderle: Less whitespace, looks better. You can view previous versions by clicking on the edited 1 hour ago link.
|
1

I'd just do a loop:

x = 0b1011
y = 0
for i in range(4):
    if x & (1 << i):
        y |= (255 << (i * 8))
print "%x" % y

1 Comment

And, having done that once, I would cache the result in a 16 entry lookup table.
0

The following recursive solution uses only addition, left/right shift operators and bitwise & operator with integers:

def xform_rec(n):
    if n == 0:
        return 0
    else:
        if 0 == n & 0b1:
            return xform_rec(n >> 1) << 8
        else:
            return 0b11111111 + (xform_rec(n >> 1) << 8)

Or, as a one-liner:

def xform_rec(n):
    return 0 if n == 0 else (0 if 0 == n & 0b1 else 0b11111111) + (xform_rec(n >> 1) << 8)

Examples:

>>> print bin(xform_rec(0b1011))
0b11111111000000001111111111111111
>>> print bin(xform_rec(0b0000))
0b0
>>> print bin(xform_rec(0b1111))
0b11111111111111111111111111111111)

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.