Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Combining two integers into a bitfield

Adam Ramona
Registered User
Join date: 5 Jan 2005
Posts: 56
12-15-2007 03:42
I have two different integers I want give as start parameters via llRezObject(). Is it possible to combine the two integers (eg, 25 and 13) into a bitfield so I can give it as the start parameter, so the rezzed object can decode the bitfield to get the two integers? Apologies if this is a dumb question :)
Haruki Watanabe
llSLCrash(void);
Join date: 28 Mar 2007
Posts: 434
12-15-2007 03:58
I know, there's a more elegant way to do this, but you could try this:

Pass 2513 to the rezed object

In the on_rez-event, you simply do this:

(Pseudo code!)

on_rez(integer pass)
{
integer first = pass/1000;
integer second = pass - (first * 1000);
}

So first should be 25 and second should be 13

(Didn't test this... :)
Viktoria Dovgal
Join date: 29 Jul 2007
Posts: 3,593
12-15-2007 04:05
One way of doing this would be:

default
{
state_entry()
{
integer a = 4848;
integer b = 2345;
integer combined = (a * 32768) + b;
integer a_new = combined / 32768;
integer b_new = combined % 32768;
llOwnerSay ("a_new: " + (string) a_new + " b_new: " + (string) b_new);
}
}
Resolver Bouchard
Registered User
Join date: 19 Jul 2006
Posts: 89
12-15-2007 04:09
It can take a little getting your head around:)

The method is to split the 32 bit integer up into smaller integers by bit shifting and masking.

Its worth getting your head around hexadecimal numbers as it makes things a lot easier to represent.

A very simple example:

integer test;

integer out1;
integer out2;
integer MASK1 = 0xFF;
integer MASK2 = 0xFF00;
default
{
state_entry()
{

}

touch_start(integer total_number)
{
test = (MASK1 & 25) + (MASK2 & (13 << 8));
llSay(0, (string)test);

out1 = MASK1 & test;
out2 = (MASK2 & test) >> 8;
llSay(0, (string)out1 + " " + (string)out2);
}
}
Adam Ramona
Registered User
Join date: 5 Jan 2005
Posts: 56
12-15-2007 04:37
Thanks Resolver, that works a treat.
From: Resolver Bouchard
It can take a little getting your head around:)

It sure can! My feeble brain groans under the strain - I just barely follow what is going on, and there is no way I could explain it :) But, it does exactly what I need, so thank you very much!