|
|
View previous topic :: View next topic |
Author |
Message |
chava
Joined: 07 Sep 2003 Posts: 56
|
rotate long variable |
Posted: Tue Mar 09, 2004 1:45 pm |
|
|
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
|
rotate long variable |
Posted: Tue Mar 09, 2004 2:00 pm |
|
|
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
|
Re: rotate long variable |
Posted: Tue Mar 09, 2004 4:18 pm |
|
|
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()
|
|
|
|
|
|
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
|