0

I have a list unicode strings representing binary numbers e.g. "010110".

I wish to perform bitwise operations, so how do I convert that to a structure where I can perform bitwise operations on these (preferably an unsigned int)?

3 Answers 3

4

Use int() with the "base" option.

int("010110", 2)
Sign up to request clarification or add additional context in comments.

Comments

3

You can convert the strings to int and then use the regular shift operators on them:

>>> x = int("010110", 2)
>>> x >> 3
2
>>> x << 3
176

1 Comment

@Jon Thanks, the habit of adding a ; is hard to kick.
0

using int() is the most obvious and useful way. but you didn't say if you need these as integers or not.

just in case not, then:

x = '1010100100'
intx = int(x,2)
x
0x2a4
intx >> 5
0x15
bin(intx>>5)
'0b10101'
x[:-5]
'10101'

intx << 3
0x1520
bin(intx<<3)
'0b1010100100000'
x + '0'*3
'1010100100000'

the actual shifts are slower, but the end result isn't necessarily, and not as much slower as you'd think. This is because even though the actual shift is probably a single cycle on most modern architectures, whereas a slice obviously is many more instructions, there is a lot of overhead just looking up arguments etc. that make it not so much difference.

# Shifts are about 40% faster with integers vs. using equivalent string methods
In [331]: %timeit intx>>5
10000000 loops, best of 3: 48.3 ns per loop

In [332]: timeit x[:-5]
10000000 loops, best of 3: 69.9 ns per loop

In [333]: %timeit x+'0'*3
10000000 loops, best of 3: 70.5 ns per loop

In [334]: %timeit intx << 3
10000000 loops, best of 3: 51.7 ns per loop

# But the conversion back to string adds considerable time,
# dependent on the length of the string
In [335]: %timeit bin(intx>>5)
10000000 loops, best of 3: 157 ns per loop

In [338]: %timeit bin(intx<<3)
1000000 loops, best of 3: 242 ns per loop

# The whole process, including string -> int -> shift -> string,
# is about 8x slower than just using the string directly.
In [339]: %timeit int(x,2)>>5
1000000 loops, best of 3: 455 ns per loop

In [341]: %timeit int(x,2)<<3
1000000 loops, best of 3: 378 ns per loop

int(x,2) is probably still your best bet, but just some other ideas for optimization if you have use of it.

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.