/* * ----------------------------------------------- * Class: BitwiseOperationsClass.as * ----------------------------------------------- * Version: 1.1 * ----------------------------------------------- * Latest update: August 7, 2003 * ----------------------------------------------- * Purpose: An object for binary/bitwise operations * ----------------------------------------------- * Developer: Richard Wright [wisolutions2002@shaw.ca] * ----------------------------------------------- * Copyright: Jos Juffermans [jjuffermans@CHELLO.com] * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of this software nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ----------------------------------------------- * Functions: * - class object * 1. Binary = {} * - class methods * Binary. * 2. binToDec(bin) * 3. binToBin(dec) * 4. binPad(bin,len) * 5. binPad64(bin,len) * 6. binOr(bin1,bin2,bin3,binN) * 7. binAnd(bin1,bin2,bin3,binN) * 8. binXor(bin1,bin2,bin3,binN) * 9. binNot(bin) * 10. binInvert(bin) * 11. binInvert64(bin) * 12. binShiftLeft(bin,pos) * 13. binShiftRight(bin,pos) * 14. binShiftRightUnsigned(bin,pos) * ----------------------------------------------- * Updates may be available at: * http://members.shaw.ca/flashmath101/as/ * ------------------------------------------------------ */ // 1. Class object Binary = new Object(); // 2. binToDec Binary.binToDec = function(bin) { return parseInt(bin,2); }; // 3. binToBin Binary.binToBin = function(dec) { return dec.toString(2); }; // 4. binPad Binary.binPad = function(bin,len) { if (bin.charAt(0)=="-") fill = "-", bin = bin.substr(1); else fill = ""; return (fill+"00000000000000000000000000000000").substr(0,len-bin.length)+bin; }; // 5. binPad64 Binary.binPad64 = function(bin,len) { if (bin.charAt(0)=="-") fill = "-", bin = bin.substr(1); else fill = ""; return (fill+"0000000000000000000000000000000000000000000000000000000000000000").substr(0,len-bin.length)+bin; }; /* * bin1, bin2, bin3, binN are not actually used, it's more to show you what * kind of argument to pass */ // 6. binOr Binary.binOr = function(bin1,bin2,bin3,binN) { var Test = parseInt(arguments[0],2); for (var i=1;i>pos).toString(2); }; // 14. binShiftRightUnsigned Binary.binShiftRightUnsigned = function(bin,pos) { return (parseInt(bin,2)>>>pos).toString(2); }; ///////////////////////////////////////// // Some samples: trace("-01001 binary = " + Binary.binToDec("-01001")+" decimal"); trace("-9 decimal = " + Binary.binToBin(-9)+" binary"); trace("-9 decimal = " + Binary.binPad(Binary.binToBin(-9),8)+" 8 bits binary"); trace("9 decimal = " + Binary.binPad(Binary.binToBin(9),32)+" 32 bits binary"); trace("-9 decimal = " + Binary.binPad64(Binary.binToBin(-9),64)+" 64 bits binary"); trace("01001 & 1010 = " + Binary.binAnd("01001","1010")); trace("01001 | 1010 = " + Binary.binOr("01001","1010")); trace("110001 | 000100 | 000111 = "+Binary.binOr("110001","000100","000111")); trace("01001 ^ 1010 = "+Binary.binXor("01001","1010")); trace("~ 01001 = "+Binary.binNot("01001")); trace("inv 01001 = "+Binary.binInvert("01001")); trace("inv64 01001 = "+Binary.binInvert64("01001")); trace("-01001 << 2 = "+Binary.binShiftLeft("-01001",2)); trace("-01001 >> 2 = "+Binary.binShiftRight("-01001",2)); trace("-01001 >>> 2 = "+Binary.binShiftRightUnsigned("-01001",2)); trace("******************************"); /* -01001 binary = -9 decimal -9 decimal = -1001 binary -9 decimal = -00001001 8 bits binary 9 decimal = 00000000000000000000000000001001 32 bits binary -9 decimal = -0000000000000000000000000000000000000000000000000000000000001001 64 bits binary 01001 & 1010 = 1000 01001 | 1010 = 1011 110001 | 000100 | 000111 = 110111 01001 ^ 1010 = 11 ~ 01001 = -1010 inv 01001 = 10110 inv64 01001 = 10110 -01001 << 2 = -100100 -01001 >> 2 = -11 -01001 >>> 2 = 111111111111111111111111111101 ****************************** */