  | 
	  | 
		 
	 
	
		| View previous topic :: View next topic   | 
	 
	
	
		| Author | 
		Message | 
	 
	
		
			evelikov92
 
 
  Joined: 10 Mar 2015 Posts: 23
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| shift_left function | 
			 
			
				 Posted: Thu Oct 15, 2015 1:38 am     | 
				     | 
			 
			
				
  | 
			 
			
				I need to use functionality like shift_left function but I can't understand exactly how is working. I test with array of bytes[4]
 
 
 	  | Code: | 	 		  
 
int8 number[4] = { 0x01, 0x02, 0x03, 0xDC};
 
   
 
   lcd_init();
 
   
 
   while(1)
 
   {
 
      printf(lcd_putc, "\f");      
 
      lcd_gotoxy(2, 2);
 
      
 
      shift_left(number, 4, 0);
 
      
 
      delay_ms(50);
 
      
 
      lcd_gotoxy(2, 1);
 
      
 
      
 
      printf(lcd_putc, "%u, %u, %u, %u", number[0], number[1], number[2], number[3]);
 
      delay_ms(3000);
 
   }
 
 | 	  
 
 
And this is few results 
 
 
 	  | Quote: | 	 		  
 
0000 0001   0000 0010    0000 0011    1101 1100
 
0000 0010   0000 0100    0000 0110	1011 1000
 
0000 0100	  0000 1000    0000 1100	0111 0000
 
0000 1000	  0001 0000    0001 1000	1110 0000
 
0001 0000	  0010 0000    0011 0000	1100 0000
 
0010 0000	  0100 0000    0110 0000	1000 0000
 
0100 0000   1000 0000    1100 0000	0000 0000
 
1000 0000	  0000 0000    1000 0001	0000 0001
 
0000 0000   0000 0001    0000 0010    0000 0011
 
0000 0000   0000 0010    0000 0100    0000 0110
 
0000 0000   0000 0100    0000 1000    0000 1100
 
0000 0000   0000 1000    0001 0000    0001 1000
 
0000 0000   0001 0000    0010 0000    0011 0000
 
0000 0000   0010 0000    0100 0000    0110 0000
 
0000 0000   0100 0000    1000 0000    1100 0000
 
0000 0000   1000 0000    0000 0000    1000 0001
 
etc.
 
 | 	  
 
 
I don't understand why in last element of array when is 0 next time is start 1. | 
			 
		  | 
	 
	
		  | 
	 
	
		
			alan
 
 
  Joined: 12 Nov 2012 Posts: 358 Location: South Africa 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Oct 15, 2015 2:18 am     | 
				     | 
			 
			
				
  | 
			 
			
				| Look at how the shift works. It shift number[0] and the overflow goes to number[1] etc. So if you print 3,2,1,0 then it will be as expected. | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Ttelmah
 
 
  Joined: 11 Mar 2010 Posts: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Oct 15, 2015 2:19 am     | 
				     | 
			 
			
				
  | 
			 
			
				Now, what you post has no resemblance to the output you are going to get. Where is it coming from?.
 
 
You will get 
 
 
2, 4, 6, 18
 
44, 8, 12, 11
 
28, 16, 24, 22
 
 
etc.
 
 
After 32 passes, you will get 0, 0, 0, 0
 
 
So first thing, switch to a numeric format that makes it easier to see what is going on. This is where hex is much easier:
 
 
%02x, %02x, %02x, %02x
 
 
Your lines will then stay the same length, and you will get
 
 
 	  | Code: | 	 		  
 
02, 04, 06, b8
 
04, 08, 0c, 70
 
08, 10, 18, e0
 
10, 20, 30, c0
 
20, 40, 60, 80
 
40, 80, c0, 00
 
80, 00, 81, 01
 
00, 01, 02, 03
 
00, 02, 04, 06
 
00, 04, 08, 0c
 
00, 08, 10, 18
 
00, 10, 20, 30
 
00, 20, 40, 60
 
00, 40, 80, c0
 
00, 80, 00, 81
 
00, 00, 01, 02
 
00, 00, 02, 04
 
00, 00, 04, 08
 
00, 00, 08, 10
 
00, 00, 10, 20
 
00, 00, 20, 40
 
00, 00, 40, 80
 
00, 00, 80, 00
 
00, 00, 00, 01
 
00, 00, 00, 02
 
00, 00, 00, 04
 
00, 00, 00, 08
 
00, 00, 00, 10
 
00, 00, 00, 20
 
00, 00, 00, 40
 
00, 00, 00, 80
 
00, 00, 00, 00
 
 | 	  
 
Now you can see what is happening.
 
 
If you want, you can even use itoa, and output the value in binary directly.
 
 
The value for each 'number' doubles as the bits move up (left). When a bit gets to the top, it moves over to the bottom of the next number. So at line 7, 0x80, in the first number, after the rotation in line 8 appears as 0x01 in the next number. At line 24, there is only one bit left, the '1' in the bottom of the last number. Seven shifts later,, this is at the top of the number, and the next rotation takes it out the top, and nothing is left. From this point, the output is 00, 00, 00, 00, no matter how much longer you run the program. | 
			 
		  | 
	 
	
		  | 
	 
	
		
			evelikov92
 
 
  Joined: 10 Mar 2015 Posts: 23
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Oct 15, 2015 2:28 am     | 
				     | 
			 
			
				
  | 
			 
			
				OK. Now I understand how is working. Thanks a lot Alan and Ttelmah.
 
I have one more question. If I have 8 elements in array of bytes. From where is start. From element[0] to element[3] or from element[4] to element[7]? And again I write
 
 
 	  | Code: | 	 		  | shift_left(number, 4, 0);  | 	 
  | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Ttelmah
 
 
  Joined: 11 Mar 2010 Posts: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Oct 15, 2015 2:50 am     | 
				     | 
			 
			
				
  | 
			 
			
				| LSB is lowest in RAM on a PIC. The bits move up the bits in the byte (so the effective value doubles with each shift), then up the bytes in memory. | 
			 
		  | 
	 
	
		  | 
	 
	
		 | 
	 
 
  
	 
	    
	   | 
	
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
  | 
   
 
  
Powered by phpBB © 2001, 2005 phpBB Group
  
		 |