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 support@ccsinfo.com

rotate long variable

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



Joined: 07 Sep 2003
Posts: 56

View user's profile Send private message Send e-mail

rotate long variable
PostPosted: Tue Mar 09, 2004 1:45 pm     Reply with quote

Hello
I have a char variable, let say N, which stores a number 0-16
I also have a long variable, its name is bits
I want to set bit number "N" in the variable "bits"
i.e: when N=2 the program will set bit number 2 in the variable "bits"

I did: bits!= 1 << N;
but this works only when the value of N is <8
when N>=8 the corresponding bit of "bits" does not affected.

I also tryed "rotate_left(1,N) but this function requieres N to be a numeric element, not a variable
SteveS



Joined: 27 Oct 2003
Posts: 126

View user's profile Send private message

rotate long variable
PostPosted: Tue Mar 09, 2004 2:00 pm     Reply with quote

Not elegant but:

int8 i;
bits = 1;
for( i = N; i > 0; i--) rotate_left(&bits,1);

- or -

int16 BitArray[16] = { 1, 2, 4, 8, ..... };
bits = BitArray[N];

- SteveS
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: rotate long variable
PostPosted: Tue Mar 09, 2004 4:18 pm     Reply with quote

chava wrote:
Hello
I have a char variable, let say N, which stores a number 0-16
I also have a long variable, its name is bits
I want to set bit number "N" in the variable "bits"
i.e: when N=2 the program will set bit number 2 in the variable "bits"

I did: bits!= 1 << N;
but this works only when the value of N is <8
when N>=8 the corresponding bit of "bits" does not affected.

I also tryed "rotate_left(1,N) but this function requieres N to be a numeric element, not a variable


From the manual
Quote:

BIT_SET( )

Syntax:
bit_set(var, bit)

Parameters:
var may be a 8,16 or 32 bit variable (any lvalue) bit is a number 0-31 representing a bit number, 0 is the least significant bit.

Returns:
undefined

Function:
Sets the specified bit (0-7, 0-15 or 0-31) in the given variable. The least significant bit is 0. This function is the same as: var |= (1<<bit);

Availability:
All devices

Requires
Nothing

Examples:
int x;

x=5;

bit_set(x,3); // x is now 13

bit_set(*6,1); // A crude way to set pin B1 high

Example Files:
ex_patg.c

Also See:
bit_clear(), bit_test()
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