Hi Santanu, hi Benjamin, On 13 Aug., 06:23, Benjamin Jones <[email protected]> wrote: > On Aug 12, 9:38 pm, Santanu Sarkar <[email protected]> > See section 5.4.1 of: > > http://docs.python.org/library/stdtypes.html
I doubt that that is answering the question: "I have 64 bit integer N. I want to rotate bits of N cyclically 5 bits right and 5 bits left to generate two integers N1, N2." I guess that Benjamin refers to "left or right shift". In the case of an integer, this is the same as multiplication resp. truncated division by a power of 2: sage: 3<<2 12 sage: 13>>2 3 However, Santanu asked about "cyclic rotation", not "shift". It is not totally clear to me what cyclic rotation should mean. Please provide a definition and/or an example! Do you perhaps mean that n bits on the right end are moved to the left end? That could be emulated with shift operators and modulus, of course: sage: def rotate(I,n): ....: right_end = I%(2^n) ....: left_end_shifted = I>>n ....: return left_end_shifted+(right_end<<(I.nbits()-n)) ....: sage: I = ZZ(randint(100,500)) sage: I.binary() '11101100' sage: rotate(I,3).binary() '10011101' Or do you mean that you consider ALL 64 bits of your integer, even leading zeroes? Then I guess you have to replace "I.nbits()-n" by "64- n". That was cyclic rotation n bits to the right side, I hope you are able to modify the example for cyclic rotation to the left side. Cheers, Simon -- To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
