Re: fast bit rotation
- Posted by "Carl R. White" <euphoria at cyreksoft.yorks.com> Dec 31, 2003
- 446 views
jiri babor wrote: > Happy New Year to you all - it's already 2004 (1.40 am) here in the > Godzone! Blimey. I have a whole ten-and-a-bit hours to go yet. > I am appealing to all speed merchants and optimization wizards (and > witches?, for Kat's sake): I need a really fast *single bit* rotation > of an integer, in either direction. Please... I'm not sure there's a fast native way. How about: function rotate_left_1(integer n, integer bitwidth) atom mask mask = power(2,bitwidth) return and_bits(n*2,mask-1)+(0!=and_bits(n,floor(mask/2))) end function function rotate_right_1(integer n, integer bitwidth) atom mask mask = power(2,bitwidth) return and_bits(floor(n/2),mask-1)+floor(mask/2)*and_bits(n,1) end function They simplify right down if you have a fixed bit width. e.g.: -- for 8 bits width: function rotate8_left_1(integer n) return and_bits(n*2,255)+(0!=and_bits(n,128)) end function function rotate8_right_1(integer n, integer bitwidth) return and_bits(floor(n/2),255)+128*and_bits(n,1) end function Carl -- [ Carl R White == aka () = The Domain of Cyrek = ] [ Cyrek the Illogical /\ www.cyreksoft.yorks.com ]