10

This fails, not surprisingly:

>>> 'abc' << 8
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for <<: 'str' and 'int'
>>> 

With ascii abc being equal to 011000010110001001100011 or 6382179, is there a way to shift it some arbitrary amount so 'abc' << 8 would be 01100001011000100110001100000000?

What about other bitwise operations? 'abc' & 63 = 100011 etc?

5
  • Care to explain why you want this? Maybe we could come up with an alternative. Commented Jun 8, 2011 at 13:08
  • Yea... I'm not really seeing a use case... it would be better to store a number and then transform it into a string when it needed to be output, but even still.... Commented Jun 8, 2011 at 13:19
  • curosity really; something had come up where it might have been useful. Never ended up needing it, just made me start thinking. Commented Jun 8, 2011 at 13:19
  • 1
    Encoding/decoding SMS PDUs, they have user data string bit-shifted for no reason. Commented Apr 30, 2015 at 20:35
  • Would this operation work in java ? Commented Jan 17, 2019 at 13:46

4 Answers 4

10

What you probably want is the bitstring module (see http://code.google.com/p/python-bitstring/). It seems to support bitwise operations as well as a bunch of other manipulations of bit arrays. But you should be careful to feed bytes into it (e.g. b'abc' or bytes('abc')), not characters - characters can contain Unicode and occupy more than one byte.

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

Comments

7

It doesn't make any sense to do bitwise operations on strings. You probably want to use the struct module to convert your strings to numbers:

>>> import struct
>>> x = 'abc'
>>> x = '\x00' * (4-len(x)) + x
>>> number = struct.unpack('!i', x)[0]
>>> number
6382179

You can then do all your operations on number. When (if) you want a string back, you can do struct.pack('!i', number).

3 Comments

It doesn't make sense? Don't be so general; what if I want to bit-shift a string by 4 to prepare it for RS encoding in a QR code?
I don't know about bitwise operations on strings but the struct worked great for me. Here's the use case. The SGF file format exists for recording certain games. In the case of Go, played on a 19x19 board, coordinates in the SGF file are given in lowercase alphabetic characters. I need to convert them to standard, numeric x-y coordinates and this answer works perfectly for that. Thanks!
I'm currently looking to do bitwise operations on strings to implement an encryption function. I'm sure others who want to do this have reason to do so that you haven't thought of.
3

I wrote a couple functions to convert ascii to int and back using only builtins. I may have mixed up the MSB/LSB though, so I'm using [::-1] to reverse the input strings. Easy fix if you don't like the ordering.

Enjoy:

>>> intstr = lambda z : ''.join([str(unichr((z & (255*(256**i)))/(256**i))) for i in range(0,((len(bin(z)) - 2) / 8) + (1 if ((len(bin(z)) - 2) / 8) else 0))])
>>> strint = lambda z : reduce(lambda x,y: x | y, [ord(str(z)[i])*((2**8)**i) for i in range(len(str(z)))])
>>> strint('abc'[::-1])
6382179
>>> bin(strint('abc'[::-1]) & 63)
'0b100011'
>>> bin(strint('abc'[::-1]) << 8)
'0b1100001011000100110001100000000'

Comments

2

use eval function to take input of string , all the bitwise operations then you can perform on string .

> t ="1|0"
eval(t)
output: 1

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.