-3

I have 32 bit numbers A=0x0000000A and B=0X00000005.

I get A xor B by A^B and it gives 0b1111.

I rotated this and got D=0b111100000 but I want this to be 32 bit number not just for printing but I need MSB bits even though there are 0 in this case for further manipulation.

Leb
  • 15,483
  • 10
  • 56
  • 75

3 Answers3

1

Most high-level languages don't have ROR/ROL operators. There are two ways to deal with this: one is to add an external library like ctypes or https://github.com/scott-griffiths/bitstring, that have native support for rotate or bitslice support for integers (which is pretty easy to add).

One thing to keep in mind is that Python is 'infinite' precision - those MSBs are always 0 for positive numbers, 1 for negative numbers; python stores as many digits as it needs to hold up to the highest magnitude difference from the default. This is one reason you see weird notation in python like ~(0x3) is shown as -0x4, which is equivalent in two's complement notation, rather than the equivalent positive value, but -0x4 is always true, even if you AND it against a 5000 bit number, it will just mask off the bottom two bits.

Or, you can just do yourself, the way we all used to, and how the hardware actually does it:

def rotate_left(number, rotatebits, numbits=32):
    newnumber = (number << rotatebits) & ~((1<<numbits)-1)       
    newnumber |= (number & ~((1<<rotatebits)-1)) << rotatebits
    return newnumber
Corley Brigman
  • 11,633
  • 5
  • 33
  • 40
  • Hey I got the rotating part covered. Thanks. I have number 0b111100000 but i want this in 32 bits I have to use 24th bit and 32nd bit which is 0 in this case and do some operations – Aashish sharma Sharma Sep 15 '15 at 19:52
  • 1
    Printing all 32 bits might require some extra work. But bits 24 and 31 actually exist; you can still get to them with e.g. `(number & (1<<24)) != 0`. – Corley Brigman Sep 15 '15 at 19:56
  • Okay I get it what you are saying. But look at my problem. I have got say D=0b111100000 which is printed as 480. Now This no. D is actually 32 bit. I want to xor 24th,0th and 32nd bit of this number and replace the MSB of D with the this bit and get a new number E. I was able to access those bits as you said but I couldn't replace the result into MSB – Aashish sharma Sharma Sep 16 '15 at 06:26
  • not sure what you mean by 'replace the result into MSB' - but you should be able to do e.g. `number |= 0x80000000` and see bit 31 set. – Corley Brigman Sep 16 '15 at 14:05
0

To get the binary of an integer you could use bin().

Just an short example:

>>> i = 333333
>>> print (i)
333333
>>> print (bin(i))
0b1010001011000010101
>>> 
frlan
  • 6,950
  • 3
  • 31
  • 72
0
bin(i)[2:].zfill(32)

I guess does what you want.

I think your bigger problem here is that you are misunderstanding the difference between a number and its representation

12 ^ 18  #would xor the values
56 & 11  # and the values 

if you need actual 32bit signed integers you can use numpy

a =numpy.array(range(100),dtype=np.int32)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Okay I get it what you are saying. But look at my problem. I have got say D=0b111100000 which is printed as 480. Now This no D is actually 32 bit. I want to xor 24th,0th and 32nd bit of this number and replace the MSB of D with the this bit and get a new number E. Like Corley said I was able to access those bits but I couldnt replace the result into MSB – Aashish sharma Sharma Sep 16 '15 at 06:24