Re: cypher tools preview, unsigned 32bits

new topic     » goto parent     » topic index » view thread      » older message » newer message

> My question: is there a way to perform bitwise operations on 32bit UNSIGNED
> integers(actually a sequence of integers)?  I need to alternate between
> 32bit bitwise and standard arithmetic operations.  The best idea I've
> thought of is to allocate enough memory to accommodate the sequence, then
> use a poke4() followed by a peek4u() to coax the interpreter into
> interpreting the values as I'd like it to, but this is inefficient.



You could use normal 32-bit atoms.
The only thing where it would go wrong is during outputting the value and when
comparing to a signed value.
For output, just add half the scope. The floating point value will be
signinicant enough.To compare unsigned values with singed
values, you could just use one generic routine, that makes a signed out of the
unsigned, rather than visa-versa. And yes, its that easy.



global function scompare (object is, atom us, integer bits)
    us = us - power (2, bits)
    return compare (is, us)
end function

global function to_signed (object is, integer bits) -- loose precision!
    is = is + power (2, bits)
end function

global function to_unsigned (object us, integer bits) -- loose precision!
    is = is - power (2, bits)
end function


Usage:
atom unsigned, signed

unsigned = to_unsigned (5, 32) -- unsigned 32 bits value
unsigned = unsigned * 2 + 2323 -- normal arithemetic works
signed = 2343
-- if signed > unsigned then       becomes
if scompare (signed, unsigned, 32) = 1 then

That's it... oh and when you want to switch the signed & unsigned value, use:
if scompare (unsigned, signed, -32) = 1 then

You can use the normal int_to_bytes () to store unsigned values to disk using
all precision.

Ralf N.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu