1. Need a hand with a small C conversion
- Posted by axtens Oct 18, 2008
- 1063 views
G'day everyone,
Seeing as Euphoria doesn't have much in the way of bit shifting (well, as far as I know, anyway), could someone advise me on the best way to convert this bit of code into Euphoria?
UTF32 X = (hi & ((1<<6) -1)) << 10 | lo & ((1<<10) -1); UTF32 W = (hi >> 6) & ((1<<5) - 1); UTF32 U = W + 1 ; codepoint = U << 16 | X;
The UTF32 datatype is defined as typedef unsigned __int32 UTF32; and hi and lo as UTF16 which is typedef unsigned __int16 UTF16;
The algorithms are from the Unicode Consortium http://www.unicode.org/unicode/faq/utf_bom.html#35
Kind regards,
Bruce.
2. Re: Need a hand with a small C conversion
- Posted by scooby Oct 18, 2008
- 1006 views
try these..
positive shift left
global function lshift(atom x, atom shift) return bitshift(x,shift) end function
global function rshift(atom x, atom shift) return bitshift(x,-shift) end function
3. Re: Need a hand with a small C conversion
- Posted by scooby Oct 18, 2008
- 991 views
here is some functions for splitting ints into hi and lo.
global function lo_word( atom a )
return low word return and_bits( a, #FFFF ) end function
global function hi_word( atom a )
return high word return and_bits(floor( a / #10000 ), #FFFF) end function
4. Re: Need a hand with a small C conversion
- Posted by scooby Oct 18, 2008
- 987 views
sorry, forgot to format.
-- positive shift left -- negative shift right global function bitshift(atom x, atom shift) return floor(x * power(2,shift) end function global function lshift(atom x, atom shift) return bitshift(x,shift) end function global function rshift(atom x, atom shift) return bitshift(x,-shift) end function ------------------------------------------------ global function lo_word( atom a ) ------------------------------------------------ -- syntax: i = lo_word( a ) -- description: -- returns the low word(2 bytes) i, from the long(4 bytes) a -- return low word return and_bits( a, #FFFF ) end function ------------------------------------------------ global function hi_word( atom a ) ------------------------------------------------ -- syntax: i = hi_word( a ) -- description: -- returns the high word(2 bytes) i, from the long(4 bytes) a -- return high word return and_bits(floor( a / #10000 ), #FFFF) end function
5. Re: Need a hand with a small C conversion
- Posted by axtens Oct 18, 2008
- 1006 views
Thanks, scooby! Hugely helpful.
6. Re: Need a hand with a small C conversion
- Posted by axtens Oct 19, 2008
- 977 views
Here's the completed routine, in case any one's interested:
function MergeSurrogates( atom hi, atom lo ) integer X integer W integer U X = or_bits( lshift( and_bits( hi, lshift(1,6) - 1), 10 ), and_bits( lo, lshift( 1, 10 ) - 1 ) ) W = and_bits( rshift( hi, 6 ), lshift( 1, 5 ) - 1) U = W + 1 return or_bits( lshift( U, 16 ), X ) end function
Thank to all contributors!
Bruce.