Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Pulling single-bit values from an integer...

Kenn Nilsson
AeonVox
Join date: 24 May 2005
Posts: 897
03-23-2008 10:53
So I've got the following script:

CODE

i = 65;
for(x = 0; x < 32; x++)
{
llOwnerSay((string)i);
llOwnerSay((string)((i << x) & 0x01));
}


...according to my understanding, it SHOULD report:

65
1
65
0
65
0
65
0
65
0
65
0
65
1
.....(all the rest being 65 and 0)

...HOWEVER, in practice it is reporting

65
1
65
0
....(all the rest being 65 and 0)

...am I doing something wrong that does not allow my to catch the ticked digit at 64? For the record, this function will later be part of a larger script that needs to check each bit of a 32-bit integer for tick/non-tick.
_____________________
--AeonVox--

Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms chasing ghosts, eating magic pills, and listening to repetitive, addictive, electronic music.
Sho Iuga
Registered User
Join date: 6 Jun 2007
Posts: 35
03-23-2008 11:28
In the script you posted you are bitshifting your testobject in the wrong direction :-)

Try to replace the line
llOwnerSay((string)((i << x) & 0x01));

with the following:
llOwnerSay((string)((i >> x) & 0x01));

Your test tests for the lowest bit, so you want a rightshift to first test bit 0, then bit1, then bit 2....

the script you posted used a leftshift and thus tested bit 0, then bit0 after a leftshift(which in LSL will always be 0 as the sign bit gets lost), then bit 0 after 2 leftshifts ....
Kenn Nilsson
AeonVox
Join date: 24 May 2005
Posts: 897
03-23-2008 11:31
Haha...thank you...dumb mistake on my part! I appreciate the help.
_____________________
--AeonVox--

Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms chasing ghosts, eating magic pills, and listening to repetitive, addictive, electronic music.