# Anim8or Community

## General Category => Ongoing Anim8or Development => Topic started by: Steve on March 05, 2014, 10:26:09 pm

Title: ASL integer bit-wise operators
Post by: Steve on March 05, 2014, 10:26:09 pm
Build 1071 adds the integer bit-wise operators &, ^, |, << and >>. Since there is no unsigned int in ASL the right shift always sign extends.

Build 1075 adds bit negate ~, and ++ and --.

Note: Build 175 had a bug parsing prefix ++ and -- so it was replaced by build 1076.
Title: Re: ASL integer bit-wise operators
Post by: Raxx on March 15, 2014, 03:09:09 pm
Hey Steve,

Finally able to give these operators a shot (trying to implement this algorithm (http://devmaster.net/posts/6145/advanced-rasterization)), but couldn't help but notice that the bit-wise NOT (~) operator is missing. Granted, it can be done manually (http://stackoverflow.com/questions/1449260/bitwise-operations-without-bitwise-operators), but it'd be awesome if it were in there.

Thanks
Title: Re: ASL integer bit-wise operators
Post by: Claude on March 15, 2014, 05:54:30 pm
Raxx

Until it's implemented,you could also apply XOR ^ between your integer and one which is all 1s to get the equivalent of a twiddle.

Hope it helps.
Claude
Title: Re: ASL integer bit-wise operators
Post by: Raxx on March 15, 2014, 09:06:40 pm
Hey Claude, makes perfect sense, totally ignored the XOR operator there.

I came up with this solution via code, but I'm wondering if there's a less intensive method of determining the all 1-bit integer. Scratch that
Title: Re: ASL integer bit-wise operators
Post by: Claude on March 15, 2014, 10:34:37 pm
return \$int  ^  0xffffffff;

Haven't tested it,but It should work.
Title: Re: ASL integer bit-wise operators
Post by: Raxx on March 15, 2014, 11:35:59 pm
Hum...it works, apparently as it should. I think I'm just a little confuzzled about the bitwise NOT operation overall. Time to read up on it!

Thanks Claude, simplifies things tremendously.
Title: Re: ASL integer bit-wise operators
Post by: Claude on March 16, 2014, 12:55:45 am
Sorry,didn't have much time when I posted.

In case this 0xffffffff is the confusing part,I should explain.
In ASL, an int is a 32-bit signed integer. Constants may be hexadecimal.
So,we define our constant as 0xffffffff

0x indicates that the following characters are hexadecimal.
Each character represents 4 bits(4 zeros or ones).
Example:

Code: [Select]
`0x0       0       0       0       0        f         c        6  0000    0000    0000    0000    0000     1111      1100     0110`
Read the first 2 paragraphs and look at the table lower on the right
side.

So, 0xffffffff gives us a 32 bits of 1.

If you already knew,it's OK.It might help someone else
interrested in ASL.
Title: Re: ASL integer bit-wise operators
Post by: Raxx on March 16, 2014, 01:38:46 am
Hey Claude, nice explanation. I pretty much knew that part (a good refresher though ;)), but like you said it could help someone else.

My main confusion was, I thought NOT was supposed to flip just the numbered bits, not the entire 32-bit span.

For example, my assumption was:

Int: 244 (0xf4)
Normal: [0000  0000  0000  0000  0000  0000]  1111 0100
Assumed NOT: [0000  0000  0000  0000  0000 0000]  0000 1011
Actual NOT: [1111  1111  1111  1111  1111  1111]  0000  1011 (int value: -245)
Title: Re: ASL integer bit-wise operators
Post by: Claude on March 16, 2014, 02:42:13 am
I understand.

In C and C++,the bitwise complement operator flips every bit.
Since ASL most of the time follows the same rules,
we may assume that it will be like that.
The XOR operation is used as a sort of selective
complement operator.
Bitwise operators are not something I've used very often.Feels like a  refresher course to me too.

Bye
Claude
Title: Re: ASL integer bit-wise operators
Post by: Steve on March 16, 2014, 08:55:11 pm

(I guess I should look at the C++ spec occasionally instead of trying to remember everything myself - getting too old for that!)
Title: Re: ASL integer bit-wise operators
Post by: Steve on March 26, 2014, 01:42:33 am
Bumping topic so it's apparent that I updated it.
Title: Re: ASL integer bit-wise operators
Post by: Raxx on March 26, 2014, 05:08:39 am
I tested --/++ a bit, looking good. There is an issue with the C-like for loop though, incrementing/decrementing (like in the code below) doesn't work.

Code: [Select]
`#command("object");int \$i;file \$c;\$c.open("\$console", "w");for (\$i = 10; \$i > 0; \$i--){ \$c.print("%d\n", \$i);}\$c.close();`
I notice that these are only post-decrement/increment operators, not pre-decrement/increment. I rarely use pre-, so I don't really care, but figured it's worth pointing out.

By the way, having alt toggle arc rotate is like a sweet breath of fresh air :) Many thanks!
Title: Re: ASL integer bit-wise operators
Post by: Steve on March 26, 2014, 05:50:48 am
Hmmm. Pre increment and decrement should be there as well. I only have a couple of test cases (test cases are as much work as the original code !!!) so can you send me yours that don't work correctly?
Title: Re: ASL integer bit-wise operators
Post by: Raxx on March 26, 2014, 07:47:26 am
Here's a few.

Code: [Select]
`#command("object");int \$i;file \$c;\$i = 2;\$c.open("\$console", "w");\$c.print("%d\n", \$i);\$c.print("%d\n", (++\$i)); // Fine in parenthesis\$c.print("%d\n", ++\$i); // Causes Loading Error\$c.print("%d\n", --\$i); // Causes Loading Error++\$i; // Causes Loading Error--\$i; // Causes Loading Error\$c.close();`
Title: Re: ASL integer bit-wise operators
Post by: Steve on March 26, 2014, 08:13:43 pm
Ahhh, foiled by the old Leading Unary Operator trick!

All my tests are of the form "\$x = ++\$y" so I can see the value of the expression as well as the underlying variable.  Anim8or doesn't currently (and apparently never did!) accept expression statements that begin with any unary operator, such as:

-\$i;

I'll fix this and have an update shortly.
Title: Re: ASL integer bit-wise operators
Post by: Steve on March 27, 2014, 03:19:29 am
I've posted build 1076 which should fix this problem as well as a slew of other corner cases such as:

\$i + \$j;

which is a valid C statement, however meaningless.