Jump to content

shifting an aray contents right.


otuatail

Recommended Posts

Hi This is something used in CPUs

Logical shift right

 

I have a string

$bin = "10011101";  // result should be  "01001110"

 

the function is

 

function LogicalShiftRight($bin)

{

  for($x = 7; $x == 0; $x--)

{

  echo $x;

  $bin[$x+1] = $bin[$x];

}

$bin[0] = 0;

return $bin;

}

 

cant get the loop to work backwords. This is an 8 bit shift right putting a zero at the highest [0] bit.

 

 

Link to comment
Share on other sites

No that is not what I am doing and the code is nearly right

I have this

$MyArray = array('1','0','0','1','1','1','0','1');

$MyArray = LogicalShiftRight($MyArray);

 

 

function LogicalShiftRight($MyArray)

{

  for($x = 7; $x == 0; $x--)

  {

    echo $x;

    $MyArray[$x+1] = $MyArray[$x];

  }

  $MyArray[0] = 0;

  return $MyArray;

}

 

 

The Idea is to

take the 7th element of the array and put it into the 8th element

take the 6th element of the array and put it into the 7th element

take the 5th element of the array and put it into the 6th element

take the 4th element of the array and put it into the 5th element

take the 3rd element of the array and put it into the 4th element

take the 2nd element of the array and put it into the 3rd element 

take the 1st element of the array and put it into the 2nd element

take the 0th element of the array and put it into the 1st element

 

And put a zero into the 0th element

 

In a u Processor it is called a logical shift right.

 

My problem is that the loop does not work. Simple.

 

 

 

 

 

 

 

Link to comment
Share on other sites

No this is definatly not what I want. php.net shows this

 

<?php

$queue = array("orange", "banana");

array_unshift($queue, "apple", "raspberry");

print_r($queue);

?>

 

 

Array

(

    [0] => apple

    [1] => raspberry

    [2] => orange

    [3] => banana

)

 

I NOW have 4 elements in the array where I had only 2. I do not want to add 2 elements to the array.

What I want is this

 

$MyArray = array('1','0','0','1','1','1','0','1');

$NewArray = LogicalShiftRight($MyArray);

 

$NewArray will be  $MyArray = array('0','1','0','0','1','1','1','0');

 

I started with 8 eliments and I finish with 8 elements.

 

What was in element [6] will have physically MOVED to element [7]

What was in element [5] will have Physically MOVED to element [6]

 

 

This is what happens inside the chip inside your computer it is called bit shifting.

 

see  http://en.wikipedia.org/wiki/Shift_register

 

 

 

Link to comment
Share on other sites

This topic is going nowhere fast. If you are not prepared to listen to the replies provided don't bother asking questions. If you have read the replies but don't understand how they are useful re read them and ask a specific question about what you don't understand.

 

If your just going to keep repeating your own failed solutions, stop wasting our time.

Link to comment
Share on other sites

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

$other_array = array(0, $Myarray[0] , $Myarray[1] , $Myarray[2] , $Myarray[3] , $Myarray[4] , $Myarray[5] , $Myarray[6]);
print_r($Myarray);
echo "<br>";
print_r($other_array);

 

Result:

Array ( [0] => 1 [1] => 0 [2] => 0 [3] => 1 [4] => 1 [5] => 1 [6] => 0 [7] => 1 )

Array ( [0] => 0 [1] => 1 [2] => 0 [3] => 0 [4] => 1 [5] => 1 [6] => 1 [7] => 0 )

 

**is there a reason to loop through it if the number of elements never change?  Wouldn't the above suit your need?

 

 

Link to comment
Share on other sites

Ok I get the idea you cant count backwards in a for() loop in php

 

for( ; ; $x--)

 

most other languages allow -- to count backwards

php can't do this so the best silly way is to

 

function LogicalShiftRight($bin)

{

  $value = 8;

  for($x = 0; $x < 8; $x++)

{

  $bin[$value-$x-1)] = $bin[$value-$x)];

}

$bin[0] = 0;

return $bin;

}

 

Link to comment
Share on other sites

$shift = 1; // number of bits to shift
$bin = decbin(bindec(10011101) >> $shift); // convert, shift the bits and convert back
echo str_repeat('0', $shift) . $bin; // leading 0s are insignificant, so must concatenate same number of 0s as the number of shifted bits for display.

 

Returns: 01001110

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.