-3

How can you convert a integer into it's bits representation?

For example number 9 representation in bits is: 10011

For example to convert a bit sequence into it's int representation you can do this:

$bits_sq = array(1,0,0,1,1);

function convert_bits_to_int($bits_sq){
    $sum = 0;
    for($i=0; $i < count($bits_sq); $i++){
       $sum = $sum + $bits_sq[$i] * pow(-2, $i);
    }
    print $sum; // equals to 9
}

But I want the other way around.

Edit: DO NOT MISTAKE BITS WITH BINARY, THIS IS NOT THE DUPLICATE NEITHER HAS THE ANSWER IN THE ABOVE THREAD

spreadzz
  • 270
  • 3
  • 10
  • 3
    Do you mean binary? http://php.net/manual/en/function.decbin.php – Jack Jan 22 '15 at 17:15
  • No, I need to convert it to it`s bits representation, I edited my answer with a example. – spreadzz Jan 22 '15 at 17:18
  • Possible duplicate of [this](http://stackoverflow.com/q/5705267/731240), [this](http://stackoverflow.com/q/1437670/731240), [this](http://stackoverflow.com/q/6382738/731240), [this](http://stackoverflow.com/q/25017724/731240), ... – dg99 Jan 22 '15 at 17:21
  • @dg99 Not a duplicate because I do not want to convert int to binary, I want to bits.... it's another ball park.... – spreadzz Jan 22 '15 at 17:28
  • 1
    Are you saying that you want to convert an int to an array of ones and zeros which represent the value of the int? So the value of int 9 (1001 in binary) would be converted to an array size of 4 elements whose elements would be $ar[0] == 1, $ar[1] == 0, $ar[2] == 0, and $ar[3] == 1? – Richard Chambers Jan 22 '15 at 18:42
  • @Richard Chambers Not to a array of binary, to a array of bits, it does not even need to be a array, as long as it returns the bits representation – spreadzz Jan 23 '15 at 09:18
  • 1
    what do you mean by an array of bits? What do you mean by the bits representation? Are you saying that there is a particular type in php called `bit`? I think you are a bit confused about this. I know I am uncertain as to what you want to accomplish and I suspect so is everyone else. No one can help you if you can not explain what it is you want to accomplish. Perhaps amend your question with some additional information about the context of what you are wanting to do. – Richard Chambers Jan 23 '15 at 13:37
  • @Richard Chambers I am not sure how I can explain more what I want... My question is very clear, and I offered an example above. I am looking for a reverse solution for my example function. – spreadzz Jan 26 '15 at 09:56
  • 3
    @spreadzz, when you ask a question of someone and they say they do not understand your question it means your question is not clear to them. It doesn't matter how clear you think your question may be if it is not clear to the person you are asking you are not going to get a high quality answer I provided what appears to be a reverse solution to your example and seen no feedback. Good luck. – Richard Chambers Jan 26 '15 at 14:19

3 Answers3

6

You need to use decbin() to convert an integer to binary.

http://php.net/manual/en/function.decbin.php

enter image description here

Jack
  • 3,271
  • 11
  • 48
  • 57
  • I do not want to convert it to binary, I want to convert it to the bits representation. – spreadzz Jan 22 '15 at 17:18
  • Each number is a 'binary digit' or 'bit'. – Jack Jan 22 '15 at 17:22
  • number 9 bits representation sequence is 10011. When you use the decbin function to conver number 9 to binary you get 1001 – spreadzz Jan 22 '15 at 17:29
  • (9)10 = (00001001)2 Oddly enough,the function is returning a correct binary value. – wadie Jan 22 '15 at 17:33
  • 10011 isn't 9 in bits. It has to be 4 segment chunks. Hence 8 bits = 1 byte. Where did you get '10011' from? – Jack Jan 22 '15 at 18:57
  • @Jack Nicholson Codilty.com they test developers with logical problem across multiple programming languages. Did a test for them recently and it annoyed me I did not solve this then, so I am trying to fiqure out a solution now. I think they know what they are saying when they say that nr 9 bits representation in bits is 10011 – spreadzz Jan 23 '15 at 09:23
  • 10011 outputs to 19. I.E. 00010011. You say not binary but bits - binary is made up of bits and bytes. There isn't any value of 10011 which equals 9 in all Ternary numeral systems. – Jack Jan 23 '15 at 11:02
  • @Jack Nicholson you should really do more research before posting... "A bit in information theory is not a binary digit, although the two are often confused. Understanding the definition of "bit" requires an understanding of Shannon's formula for information, h(p) = -logb(p). " Read here: http://cs.stanford.edu/people/eroberts/courses/soco/projects/1999-00/information-theory/bits_and_binary_digits_3.html – spreadzz Jan 26 '15 at 09:50
  • 1
    'Bit' is a portmanteau of binary digit. But the difference isn't relevant to the question. I tried to help you. – Jack Jan 26 '15 at 11:07
3

My php is rusty however if you want to do the reverse of this example

$bits_sq = array(1,0,0,1,1);

function convert_bits_to_int($bits_sq){
    $sum = 0;
    for($i=0; $i < count($bits_sq); $i++){
       $sum = $sum + $bits_sq[$i] * pow(-2, $i);
    }
    print $sum; // equals to 9
}

then I suppose you want something like:

$bits_sq = convert_int_to_bits ($iValue);
function convert_int_to_bits ($iValue) {
  $bits = array();  // initialize the array
  do {
    $bits[] = ($iValue & 1);
    $iValue >>= 1;    // shift the bit off so that we go to the next one
  } while ($iValue);  // continue as long as there are still some bits.
  // we have the bits in reverse order so lets reverse it.
  return array_reverse($bits);
}
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
0

Just dropping this here.

echo str_pad(decbin(1), 4, '0', STR_PAD_LEFT); // 0001
echo str_pad(decbin(7), 4, '0', STR_PAD_LEFT); // 0111
echo str_pad(decbin(9), 4, '0', STR_PAD_LEFT); // 1001
Philip Borbon
  • 707
  • 1
  • 11
  • 21