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

Bitwise operators HELP

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



Joined: 07 Jan 2017
Posts: 16

View user's profile Send private message

Bitwise operators HELP
PostPosted: Mon Jan 30, 2017 10:55 am     Reply with quote

Hi guys, I'm the noob again haha ^^.


I'm still learning C code and PICs, and I always used to switch pins like this PORTBbits.RB0 = 1; for example to do a simple LED chaser, but I saw "<< >> " to move bits right and left and I wanted to build an LED chaser from RB0 to RB7 using this bitwise operator, can I get an example please ?
newguy



Joined: 24 Jun 2004
Posts: 1907

View user's profile Send private message

PostPosted: Mon Jan 30, 2017 11:21 am     Reply with quote

Code:
unsigned int8 pattern = 0x80;

pattern >>= 1; // pattern now holds 0x40
pattern >>= 2; // pattern now holds 0x10
pattern <<= 1; // pattern now holds 0x20


I'm leaving it up to you to determine:
a) how to write directly to an entire port at once;
b) how to create your LED chaser. In general, it will have to consist of a shift section (you'll need to keep track of whether you're traveling L or R, and perform the appropriate shift), and a delay section. These two steps will repeat forever.
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Mon Jan 30, 2017 11:54 am     Reply with quote

Also, as a further comment, >> & << are the generic 'C' shifts. However for a rotation, you might want to look at the rotate_right and rotate_left functions:
Code:


    int8 val=0b00000001;

    rotate_left(&val,1); //gives 0b00000010
    rotate_right(&val,1); //back to the start
    rotate_right(&val,1); //gives 0b10000000

Unlike the shift, where the bit would disappear 'out the bottom', the bit rotates back round to the top of the variable.
ViperARG



Joined: 07 Jan 2017
Posts: 16

View user's profile Send private message

PostPosted: Wed Feb 01, 2017 12:03 pm     Reply with quote

Ttelmah wrote:
Also, as a further comment, >> & << are the generic 'C' shifts. However for a rotation, you might want to look at the rotate_right and rotate_left functions:
Code:


    int8 val=0b00000001;

    rotate_left(&val,1); //gives 0b00000010
    rotate_right(&val,1); //back to the start
    rotate_right(&val,1); //gives 0b10000000

Unlike the shift, where the bit would disappear 'out the bottom', the bit rotates back round to the top of the variable.



Awesome thanks, I didnĀ“t know that. I think that one is more suitable for what I want to do ^^
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