There is a binary array:

var BinArr:Array [ 1, 1, 1, 0, 1, 0, 0]; 

I need to create a 32-bit number or several numbers from this array if the array has more than 32 elements. The main problem is that Actionscript 2.0 does not support the uint type, and when I use the Number and the number is more than 31 bits, everything hangs and eventually it turns out to be -1, for example, this array of bits gives -1 after the hang:

 var BinArr:Array [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]; 

Here, by the way, is the function by which I convert the array into a number:

 function binaryToDecimal(s:String):Number{ var n:Number = 0; for(var i:int=0;i<s.length;i++){ n+=Number(s.substr(i,1))<<(s.length-1-i) } return n; } 

I can not understand what the error is, because Number is Decimal , as far as I understood, so 32 bits should fit, but for some reason it does not fit, the maximum is that it works 31 bits.

UPDATE1: In general, I solved the problem, I do not hang up, I immediately get -1 as it should be according to the idea. Now the problem is in the inverse function, it normally converts the number back into a binary string, but does not want to convert a negative number, here is the function:

 function decimalToBinary(num:Number):String{ var bin:String = ""; while (num) { bin = num % 2 + bin; num = Math.floor(num / 2); } return (bin) ? bin : String(0); } 
  • Explain the word "hang". If the program hangs, how does it produce the result? - Petr Abdulin
  • Hangs on for 20 seconds, then droops and gives a result of -1. - SkyDancer
  • Better ask a separate question. - Sergey Snegirev

2 answers 2

Apparently, the problem is that Flash AS2 interprets the result of the shifts as a signed int . On the same page there is a mention of the fact that & or bitwise AND, as well as ^ or bitwise XOR convert arguments to an unsigned integer, and therefore returns an unsigned integer. That is, you can try to wrap the shift in (xor 0) and get an unsigned number to add to the existing (significant, but in fact, real) Number .

 function binaryToDecimal(s:String):Number{ var n:Number = 0; for(var i:int=0;i<s.length;i++){ n+=(Number(s.substr(i,1))<<(s.length-1-i)) ^ 0); } return n; } 

However, it should be noted that such a format will not work for arrays over 32 in length, since moving the whole integer will yield 0 regardless of the input value. In this case, it will be correct to use multiplication and the Number type.

 function binaryToDecimal2(s:String):Number { var two:Number=1; // power of two var n:Number=0; // accumulator for (var i:int=s.length-1;i>=0;i--) { if (s.charAt(i) == '1') { n+=two; } // assuming all other symbols are 0. two=two*2; } return n; } 

In general, if possible, avoid excessive type conversion, such as adding or shifting a symbol as a number, if you can interpret it as a symbol and use a ready-made number.

UPDATE: As for converting from a negative number - here you just need to use shift and bit operations. The trick is that Math.floor(-1/2) is -1, and we have an infinite loop.

 function decimalToBinary(num:Number):String{ var bin:String = ""; while (num!=0) { bin = num % 2 + bin; num = num >>> 1; // беззнаковый сдвиг, в итоге из -1 получится 0x7FFFFFFF } return (bin) ? bin : String(0); } 
  • Well, I'll try your option, but I still have a question, when I got the result -1, everything terribly hung at me when performing this operation, in principle, I thought this, in fact, I would be converted to a HEX string from an array of bits, and back, maybe if you convert a string to HEX then it will not hang, is it possible to convert a binary array to a HEX string immediately bypassing the cast to Number ? - SkyDancer
  • Well, it is not binary, but from characters, judging by the fact that the function accepts a string. As for the conversion to HEX - in theory, if the plug is there, it will shut up in the same place. It is better to work with numbers as with numbers, without any extra conversion, into a string or somewhere else. - Vesper
  • Updated the question. - SkyDancer

If 32 single bits give -1, then the receiver number is significant, the most significant single bit indicates that the number is negative and you get nothing at all what you want.

  • Understood, And how do I write the code so that the number of the receiver was unsigned? - SkyDancer
  • I can not imagine, you need to look at what data types are in a specific language. - Vladimir Martyanov
  • I am also completely unfamiliar with the language, and it seems that as far as I understood, in this version as 2.0 there is only one type of Nember and it actually goes Decimal that is, it’s the sign one way or another ... - SkyDancer
  • Then alas. In memory, apparently, you have all 32 bits as it should be located, but their interpretation is not the same. - Vladimir Martyanov
  • @ Vladimir Martianov The question was why 1 << 31 turns out to be a landmark, and how to get around this. The answer is in the documentation, but really it is necessary to get to the bottom of it with manners. So the answer is just about nothing, and it would be better to comment. - Vesper