CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

shift_left function

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
evelikov92



Joined: 10 Mar 2015
Posts: 23

View user's profile Send private message

shift_left function
PostPosted: Thu Oct 15, 2015 1:38 am     Reply with quote

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: 357
Location: South Africa

View user's profile Send private message

PostPosted: Thu Oct 15, 2015 2:18 am     Reply with quote

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: 19504

View user's profile Send private message

PostPosted: Thu Oct 15, 2015 2:19 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Oct 15, 2015 2:28 am     Reply with quote

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: 19504

View user's profile Send private message

PostPosted: Thu Oct 15, 2015 2:50 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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