Convert int into bits representation in php -
How do you change an integer into bits representation?
For example, number 9 representation in bits: 10011
For example, to change a bit sequence in it you can:
$ Bits_sq = array (1,0,0,1,1); Function convert_bits_to_in ($ bits_sax) {$ sum = 0; ($ I = 0; $ i & lt; Count ($ bits_sc); $ i ++) {$ sum = $ sum + $ bits_sq [$ i] * Pau (-2, $ i); } Print $ sum; // equal 9} But I want to the other side.
Edit: Do not make mistakes with double-headed, this is not a duplicate, the answer in the above is
$ bits_sq = array (1,0, behind the example) = "post-text" itemprop = "text" 0,1,1); Function convert_bits_to_in ($ bits_sax) {$ sum = 0; ($ I = 0; $ i & lt; Count ($ bits_sc); $ i ++) {$ sum = $ sum + $ bits_sq [$ i] * Pau (-2, $ i); } Print $ sum; // equal 9} So I think you want something like this:
$ bits_sq = convert_ int_to_bits ($ iValue); Function convert_ int_to_bits ($ iValue) {$ bits = array (); // Start the array {$ bits} = ($ iValue & amp; 1); $ IValue & gt; & Gt; = 1; // Make a little bit change so that we go to the next one} ($ iValue); // will continue until some bits still continue. // We have reverse order bits, so reverse it. Return array_reverse ($ bits); }
Comments
Post a Comment