12 Replies Latest reply on Mar 20, 2008 3:04 AM by Newsgroup_User

    bit rotation

    Level 7
      Hi

      Is it possible with flash to some bitwise rotation

      eg a binary number 1000011 rotated 3 to the left becomes 0011100

      Thanks


        • 1. Re: bit rotation
          kglad Adobe Community Professional & MVP
          flash can perform bitwise operations. check the flash index under symbols.
          • 2. Re: bit rotation
            Level 7
            AS3 has no built-in bitwise rotation, but you can accomplish the same
            (albeit slower) using functions like these:

            function bitRotate(value,amount,bits) {
            var msk = (bits<32) ? 0x7fffffff >> (31-bits) : 0xffffffff;
            if (amount>0) {
            amount %= bits;
            value = (value<<amount) | (value>>(bits-amount));
            } else if (amount<0) {
            amount = -amount%bits;
            value = (value>>amount) | (value<<(bits-amount));
            }
            return value & msk;
            }
            function bitRotate32(value,amount) {
            if (amount>0) {
            amount %= 32;
            value = (value<<amount) | (value>>(32-amount));
            } else if (amount<0) {
            amount = -amount%32;
            value = (value>>amount) | (value<<(32-amount));
            }
            return value;
            }
            function bitString(value, bits)
            {
            var result = value.toString(2);
            while(result.length < bits){result = "0" + result;}
            return result;
            }

            var testVal = 41231;
            for (var j=0; i<64; i++) {
            var val1 = bitRotate(testVal,-j,8); // rotates 8 bits to the left
            (-amount)
            var val2 = bitRotate(testVal,j,8); // rotates 8 bits to the right (amount)
            trace(bitString(val1,8), bitString(val2,8));
            }

            • 3. Re: bit rotation
              Level 7
              Thanks for the reply on this, I am unfortunately using as2, could you give
              me the same info in as2?

              Many thanks

              Dave

              "Raymond Basque" <_NOSPAM_rbasque@jednm.com_NOSPAM_> wrote in message
              news:frmkv5$esr$1@forums.macromedia.com...
              > AS3 has no built-in bitwise rotation, but you can accomplish the same
              > (albeit slower) using functions like these:
              >
              > function bitRotate(value,amount,bits) {
              > var msk = (bits<32) ? 0x7fffffff >> (31-bits) : 0xffffffff;
              > if (amount>0) {
              > amount %= bits;
              > value = (value<<amount) | (value>>(bits-amount));
              > } else if (amount<0) {
              > amount = -amount%bits;
              > value = (value>>amount) | (value<<(bits-amount));
              > }
              > return value & msk;
              > }
              > function bitRotate32(value,amount) {
              > if (amount>0) {
              > amount %= 32;
              > value = (value<<amount) | (value>>(32-amount));
              > } else if (amount<0) {
              > amount = -amount%32;
              > value = (value>>amount) | (value<<(32-amount));
              > }
              > return value;
              > }
              > function bitString(value, bits)
              > {
              > var result = value.toString(2);
              > while(result.length < bits){result = "0" + result;}
              > return result;
              > }
              >
              > var testVal = 41231;
              > for (var j=0; i<64; i++) {
              > var val1 = bitRotate(testVal,-j,8); // rotates 8 bits to the left
              > (-amount)
              > var val2 = bitRotate(testVal,j,8); // rotates 8 bits to the right (amount)
              > trace(bitString(val1,8), bitString(val2,8));
              > }
              >


              • 4. Re: bit rotation
                Raymond Basque Level 4
                Correction. This works with AS2
                • 5. Re: bit rotation
                  Raymond Basque Level 4
                  The algorithm in my porevious post does not work properly. This seems to be on the money:
                  • 6. Re: bit rotation
                    Raymond Basque Level 4
                    Note: The example used in my last post will produce erroneous results because testVal exceeds the maximum value that can be assigned to the number of bits designated for the rotation. This will produce proper results:
                    • 7. Re: bit rotation
                      Level 7
                      Thanks for this Raymond, I am tring to get my head around exactly what is
                      happening as its an area of flash I have not experienced, specifically the
                      first line of the bitRotate function. Why do you check for the number of
                      bits?

                      It may take a while! Thanks anyway.


                      "Raymond Basque" <webforumsuser@macromedia.com> wrote in message
                      news:frogup$gsa$1@forums.macromedia.com...
                      > Note: The example used in my last post will produce erroneous results
                      > because
                      > testVal exceeds the maximum value that can be assigned to the number of
                      > bits
                      > designated for the rotation. This will produce proper results:
                      >
                      > var testVal = 127;
                      > var bits = 8;
                      >
                      > for (var j=0; j<64; j++) {
                      > var val1 = bitRotate(testVal,-j,bits);
                      > var val2 = bitRotate(testVal,j,bits);
                      > trace(bitString(val1,bits) + " " + bitString(val2,bits));
                      > }
                      >


                      • 8. Re: bit rotation
                        Level 7
                        This creates a mask so we can strip out the unwanted most significant bits.
                        For example, the mask for 8 bits is 11111111. We get the mask by shifting
                        0x7fffffff (31 bits) to the right by 31-bits.

                        var msk = (bits<32) ? 0x7fffffff >> (31-bits) : 0xffffffff;

                        For example, an 8-bit mask would be
                        1111111111111111111111111111111 shifted 23 places to the right, which
                        produces 11111111.

                        Consider this:

                        bitRotate(127,5,8)

                        The result of (n >> x | n << (bits - x)) using these arguments is 1019 (or
                        1111111011 binary). But we want only the 8 least significant bit. So by
                        applying the mask using the bitwise AND operator (&), we modify the result
                        to 11111011;



                        • 9. Re: bit rotation
                          Level 7
                          Ok, that is getting a little clearer, but why are the 32 bits treated
                          differently, would it work the same if you just shifted 32 bits by 32-bits?

                          Thanks for the guidance

                          "Raymond Basque" <nospam-rbasque-at-jednm-dot-com@nospam.com> wrote in
                          message news:frojam$jbm$1@forums.macromedia.com...
                          > This creates a mask so we can strip out the unwanted most significant
                          > bits.
                          > For example, the mask for 8 bits is 11111111. We get the mask by shifting
                          > 0x7fffffff (31 bits) to the right by 31-bits.
                          >
                          > var msk = (bits<32) ? 0x7fffffff >> (31-bits) : 0xffffffff;
                          >
                          > For example, an 8-bit mask would be
                          > 1111111111111111111111111111111 shifted 23 places to the right, which
                          > produces 11111111.
                          >
                          > Consider this:
                          >
                          > bitRotate(127,5,8)
                          >
                          > The result of (n >> x | n << (bits - x)) using these arguments is 1019
                          > (or
                          > 1111111011 binary). But we want only the 8 least significant bit. So by
                          > applying the mask using the bitwise AND operator (&), we modify the result
                          > to 11111011;
                          >
                          >
                          >


                          • 10. Re: bit rotation
                            Level 7
                            Well, yes you could. But you'd need to use the unsigned shift operator.

                            var msk = 0xffffffff >>> (32-bits);


                            • 11. Re: bit rotation
                              Raymond Basque Level 4
                              Took another look at this and discovered some bugs. Here's a fixed-up function.
                              • 12. Re: bit rotation
                                Level 7
                                Thanks for your time on this Raymond, I shall be testing today.

                                "Raymond Basque" <webforumsuser@macromedia.com> wrote in message
                                news:frr5mf$euh$1@forums.macromedia.com...
                                > Took another look at this and discovered some bugs. Here's a fixed-up
                                > function.
                                >
                                > function bitRotate(n, x, bits)
                                > {
                                > var tmp =0;
                                > var msk = 0xffffffff >>> (32-bits);
                                > var result=0;
                                >
                                > x %= bits;
                                >
                                > // save off the affected bits
                                > tmp = n & msk;
                                >
                                > // do the rotation
                                > result = x < 0 ? (n << -x) : (n >>> x);
                                >
                                > // shift the saved off bits
                                > if (x < 0){tmp >>>= (bits + x);}
                                > else {tmp <<= (bits - x);}
                                >
                                > // add the rotated bits back in (in the proper location)
                                > result |= tmp;
                                >
                                > // return the masked result
                                > return result & msk;
                                > }
                                >