views:

1336

answers:

9

Best in PHP,

for example,

11011111 ==> 11111011

+2  A: 

Try to get this book, there is whole chapter about bits reversion: Hacker's Delight. But please check content first if this suits you.

Roman Nikitchenko
A: 

In your example you are not doing reversal. See PHP documentation on this for shift right bitwise operation.

If you were doing a true reversal, you'd use the NOT operation like so:

$test = b'111000111';
$rtest = ~$test;

The b'1111' above requires PHP 5.3 in order to use binary strings.

Kevin Peno
I don't think you got his question right. He doesn't mean inverting each bit value, but reversing the character string, as seen as a word over the alphabet { 0, 1 }.
Arthur Reutenauer
I guess I'm missing something. I'm not understnading your reply. Please don't bash me too hard, I've had a long (early) morning :P
Kevin Peno
Like "Kevin Peno" => "oneP niveK".
Michael Myers
Oh god...duh. Told you I'm out of it.
Kevin Peno
+5  A: 

The quickest way, but also the one requiring more space is a lookup, whereby each possible value of a byte (256 if you go for the whole range), is associated with its "reversed" equivalent.

If you only have a few such bytes to handle, bit-wise operators will do but that will be slower, maybe something like:

function reverseBits($in)
{
  $out = 0
  if ($in & 0x01) { $out |= 0x80;}
  if ($in & 0x02) { $out |= 0x40;}
  if ($in & 0x04) { $out |= 0x20;}
  if ($in & 0x08) { $out |= 0x10;}
  if ($in & 0x10) { $out |= 0x08;}
  if ($in & 0x12) { $out |= 0x04;}
  if ($in & 0x14) { $out |= 0x02;}
  if ($in & 0x18) { $out |= 0x01;}
  return $out;
}
mjv
Historically, I've found that having a 256 byte lookup table is the fastest way to achieve it as it's just a lookup. 256 bytes is not a lot of space to dedicate to something like this if it need to be fast. Though the above reverseBits function is about as small and tight as you can get the code without having a lookup. Also for one small optimization you could change the first { $out |= 0x80;} to { $out = 0x80;} as you know that first time you're ORing with 0.
skirmish
@skirmish, agreed, I'd tend to use the lookup array in most cases. An intermediate solution, would be to have a smaller array, for 4 bits, and do two lookups with the associated multiplication/division for the leftmost bit-quad). This way of doing can also be used to deal with say integers rather that bytes. (The downside of the lookup approach is that its space requirement grows exponentially, whereby the coded appraoch linearly (w/ regards to the number of bits).
mjv
+1  A: 

Make a print out of the bits onto a piece of paper. Hold the piece of paper up to a mirror and then type them in in reverse order, using the mirror reflection. Simple, really.

Kinopiko
+1 lol! omg...coffee everywhere.
Kevin Peno
Remember to use a font where `0` doesn't have a diagonal line through it, and the `1` doesn't have a serif, or the bits will come out backwards in the result.
Steve Jessop
+7  A: 

The straight forward approach is to perform 8 masks, 8 rotates, and 7 additions:

$blah = $blah & 128 >> 7 + $blah & 64 >> 5 + $blah & 32 >> 3 + $blah & 16 >> 1 + $blah & 8 << 1 + $blah & 4 << 3 + $blah & 2 << 5 + $blah & 1 << 7;
Epsilon Prime
That got two upvotes?
Kinopiko
@Kinopiko: 3 upvotes, with mine. Do you have a better solution. Post it and you'll get my vote too!
Seb
Well, it is answering the question :-)
Arthur Reutenauer
which mathematical theorem or algorithm is this answer based on?
Lukman
"The Principle of Brute Force"
Epsilon Prime
+10  A: 

If you have already the bits in the form of a string, use strrev.

If not, convert first the byte to its binary representation by using decbin, then reverse using strrev, then go back to byte (if necessary) by using bindec.

Konamiman
The best answer IMO that will work for general cases. Those bit shifting, rotation etc are just targeting the example value given and won't work for random binary strings ..
Lukman
+1 This is what I would have suggested had I not botched the initial understanding.
Kevin Peno
I am really curious about why I have been downvoted here...
Konamiman
+11  A: 

Check the section on reversing bit sequences in Bit Twiddling Hacks. Should be easy to adapt one of the techniques into PHP.

While probably not practical for PHP, there's a particularly fascinating one using 3 64bit operations:

unsigned char b; // reverse this (8-bit) byte
b = (b * 0x0202020202ULL & 0x010884422010ULL) % 1023;
Paul Dixon
Can you explain what "ULL" means here?
Mask
@mask that's an unsigned long long, the suffix tells the compiler that what proceeds it is a 64-bit unsigned integer literal.
PeterAllenWebb
+1  A: 

I disagree with using a look up table as (for larger integers) the amount of time necessary to load it into memory trumps processing performance.

I also use a bitwise masking approach for a O(logn) solution, which looks like:

MASK = onescompliment of 0    
while SIZE is greater than 0
  SIZE = SIZE shiftRight 1
  MASK = MASK xor (MASK shiftLeft SIZE)
  output = ((output shiftRight  SIZE) bitwiseAnd MASK) bitwiseOR ((onescompliment of MASK) bitwiseAnd (output shfitLeft SIZE))

The advantage of this approach is it handles the size of your integer as an argument

in php this might look like:

function bitrev($bitstring, $size){

  $mask = ~0;
  while ($size > 0){

    $size = $size >> 1;
    $mask = $mask ^ ($mask << $size);
    $bitstring = (($bitstring >> $size) & $mask) | ((~$mask) & ($bitstring << $size));
  }
}

unless I screwed up my php somewhere :(

tzenes
if you're performing the operation just once, a lookup table is bad. But if it's a frequent operation, it should outperform any other approach.
Paul Dixon
+2  A: 

This is O(n) with the bit length. Just think of the input as a stack and write to the output stack.

My attempt at writing this in PHP.

function bitrev ($inBits, $bitlen){
   $cloneBits=$inBits;
   $inBits=0;
   $count=0;

   while ($count < $bitlen){
      $count=$count+1;
      $inBits=$inBits<<1;
      $inBits=$inBits|($cloneBits & 0x1);
      $cloneBits=$cloneBits>>1;
   }
}
Jeffery Williams