otuatail Posted December 2, 2011 Share Posted December 2, 2011 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 https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/ Share on other sites More sharing options...
requinix Posted December 2, 2011 Share Posted December 2, 2011 It's just a string... "0" . substr($bin, 0, -1) Link to comment https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293607 Share on other sites More sharing options...
otuatail Posted December 2, 2011 Author Share Posted December 2, 2011 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 https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293666 Share on other sites More sharing options...
requinix Posted December 2, 2011 Share Posted December 2, 2011 The problem is that you're over-complicating the issue. And that you're posting different code from what you're actually working with. array_unshift($MyArray, "0"); array_pop($MyArray); Link to comment https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293669 Share on other sites More sharing options...
Pikachu2000 Posted December 2, 2011 Share Posted December 2, 2011 And there are bitwise logical left/right shift operators in php as well, although the implementation seems a bit (pun intended) clunky to me. Link to comment https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293671 Share on other sites More sharing options...
otuatail Posted December 2, 2011 Author Share Posted December 2, 2011 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 https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293678 Share on other sites More sharing options...
otuatail Posted December 2, 2011 Author Share Posted December 2, 2011 all I NEED is a backward loop from 7 to zero Link to comment https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293679 Share on other sites More sharing options...
requinix Posted December 2, 2011 Share Posted December 2, 2011 Clearly you are a troll. Link to comment https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293687 Share on other sites More sharing options...
otuatail Posted December 2, 2011 Author Share Posted December 2, 2011 requinix You are inteligent. All i need is someone now to help me with my coding problem. If anyone can sensible can help for($x = 7; $x == 0; $x--) { count backwards } $x-- means subtract !!! Link to comment https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293701 Share on other sites More sharing options...
otuatail Posted December 2, 2011 Author Share Posted December 2, 2011 Pikachu2000 knows what I mean I just want a for() loop to work from 6 to 0 for($x = 7; $x > 0; $x--) { echo $x; $bin[$x+1] = $bin[$x]; } dosn't for some reason Link to comment https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293705 Share on other sites More sharing options...
trq Posted December 2, 2011 Share Posted December 2, 2011 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 https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293713 Share on other sites More sharing options...
meltingpoint Posted December 2, 2011 Share Posted December 2, 2011 $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 https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293720 Share on other sites More sharing options...
otuatail Posted December 3, 2011 Author Share Posted December 3, 2011 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 https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293726 Share on other sites More sharing options...
Pikachu2000 Posted December 3, 2011 Share Posted December 3, 2011 What do you mean php can't do that? Of course it can. for( $i = 10; $i >= -10; $i-- ) { echo "$i<br>"; } outputs 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, Link to comment https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293729 Share on other sites More sharing options...
otuatail Posted December 3, 2011 Author Share Posted December 3, 2011 You can do this in the 'C' language which php comes from. The C Programming Language Brian Kernighan and Dennis Ritchie Link to comment https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293730 Share on other sites More sharing options...
Pandemikk Posted December 3, 2011 Share Posted December 3, 2011 Can a mod please close this? It's obvious OP is asking a question but not bothering to read the answers. (I think it's a troll) Link to comment https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293732 Share on other sites More sharing options...
trq Posted December 3, 2011 Share Posted December 3, 2011 Agreed. Link to comment https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293734 Share on other sites More sharing options...
Pikachu2000 Posted December 3, 2011 Share Posted December 3, 2011 $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 https://forums.phpfreaks.com/topic/252336-shifting-an-aray-contents-right/#findComment-1293736 Share on other sites More sharing options...
Recommended Posts