Re: fast bit rotation
- Posted by jiri babor <jbabor at paradise.net.nz> Dec 31, 2003
- 433 views
Thanks, Carl, I'll play with it. jiri ----- Original Message ----- From: "Carl W." <euphoria at cyreksoft.yorks.com> To: <EUforum at topica.com> Sent: 01 January 2004 2:14 AM Subject: Re: fast bit rotation > > > 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 ]