## Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

# Bitwise Integer Ops

Pat Murphy
The Wandering Wizard
Join date: 2 Dec 2002
Posts: 142
02-26-2004 02:29
Here's some bitwise integer operations, cross posted from the main scripting forum.

CODE
`integer shift_left(integer val, integer count){    return val * (integer) llPow(2, (float) count);}integer shift_right(integer val, integer count){    if( val >= 0 )    {        return val / (integer) llPow(2, (float) count);    }    else    {        return ((integer) llPow(2, 32 - count) - 1) - (llAbs(val) - 1) / (integer) llPow(2, (float) count);    }}integer rotate_left(integer val, integer count){    integer shift = shift_left(val, count);    integer overflow = shift_right(val, 32 - count);    return shift + overflow;}integer rotate_right(integer val, integer count){    integer shift = shift_right(val, count);    integer overflow = val * (integer) llPow(2, (float) 32 - count);    return shift + overflow;}integer reverse(integer val){    integer newval = 0;    integer i;    for( i = 0; i < 32; i++ )    {        newval = shift_left(newval, 1);        newval = newval + (val % 2);        val = shift_right(val, 1);    }    return newval;}integer testbit(integer val, integer pos){    return shift_right(val, pos) % 2;}string tostring(integer val){    string retval;    integer i;    for( i = 0; i < 32; i++ )    {        retval = (string) llAbs(val % 2) + retval;        val = shift_right(val, 1);    }    return retval;}`

_____________________
That's how they showed their respect for Paddy Murphy
That's how they showed their honour and their pride;
They said it was a sin and shame and they winked at one another
And every drink in the place was full the night Pat Murphy died.
-Great Big Sea
gene Poole
"Foolish humans!"
Join date: 16 Jun 2004
Posts: 324
03-18-2005 23:20
Coolness, but may I just suggest using actual shifts (<< and >>) as substitutes for your shift_left and shift_right functions?
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
03-23-2005 12:50
(this script is a year old, before the bit shift operators were added to LSL)
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey