|
|
View previous topic :: View next topic |
Author |
Message |
pcmgogo
Joined: 25 Dec 2004 Posts: 9
|
Copy only one bit from integer to another |
Posted: Fri Feb 06, 2009 3:01 am |
|
|
I was using CC5X compiler before CCS. I was copying one bit with following instructions.
a and b are integers
This mean, first bit of a variable is last bit of b variable.
In CCS,
I could not find any different solution from below,
Code: |
if(bit_test(a,1)==1)
{
bit_set(b,7);
}
else
{
bit_clear(b,7);
}
|
Are there any different type of code for making this?
Regards
[/i] |
|
|
Ttelmah Guest
|
|
Posted: Fri Feb 06, 2009 3:35 am |
|
|
There are several dozen different ways of doing this.
Probably the most efficient, would be almost the same as your original, but you declare 'names' for the bits. So:
Code: |
//with the two integers 'a', and 'b' declared.
#bit tbit=b.7
#bit sbit=a.1
//Then you can copy either way, with:
tbit=sbit; //sets b.7 to equal a.1
sbit=tbit; //sets a.1 to equal b.7
|
Obvious alternatives:
1) A union, of a bit array (provided your compiler is recent).
2) Simple logical bit masks (work in any C, but uses more instructions).
3) Use a conditional expression (more efficient in most C's, but not in CCS)
The code you have posted, for 'CCS', does the _opposite_ of the CC5X code you have posted.
Best Wishes |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: Copy only one bit from integer to another |
Posted: Fri Feb 06, 2009 6:57 am |
|
|
pcmgogo wrote: |
Code: |
if(bit_test(a,1)==1)
{
bit_set(b,7);
}
else
{
bit_clear(b,7);
}
|
|
This is actually not so bad. It compiles to 5 instructions and takes 4 or 5 instruction cycles to execute But this is even better:
Code: |
if(bit_test(a,1))
bit_set(b,7);
if( ! bit_test(a,1))
bit_clear(b,7);
|
Because of the absence of the "else", this compiles with no GOTO instructions. It takes only 4 instructions and executes in exactly 4 instruction cycles. It may look cleaner to write:
But there is no way you are going to beat 4 instructions and 4 instruction cycles. _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Fri Feb 06, 2009 9:27 am |
|
|
"there is no way you are going to beat 4 instructions and 4 instruction cycles."
Them's fightin' words, friend.
Code: |
0000 01498 .................... bit_clear(b, 7);
0275 13DD 01499 BCF 5D,7
0000 01500 .................... if (bit_test(a, 1))
0276 18DC 01501 BTFSC 5C,1
0000 01502 .................... bit_set(b, 7);
0277 17DD 01503 BSF 5D,7
|
|
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Fri Feb 06, 2009 11:01 am |
|
|
John P wrote: | "there is no way you are going to beat 4 instructions and 4 instruction cycles."
Them's fightin' words, friend.
Code: |
0000 01498 .................... bit_clear(b, 7);
0275 13DD 01499 BCF 5D,7
0000 01500 .................... if (bit_test(a, 1))
0276 18DC 01501 BTFSC 5C,1
0000 01502 .................... bit_set(b, 7);
0277 17DD 01503 BSF 5D,7
|
|
I can think of one situation in which this would not do. Suppose the main program is setting b.7 to 0 or 1 so that an ISR can read it. Doing it your way means there is a narrow window in which b.7 might actually be set wrong. If the interrupt hits during this window, then it would pick up and use an incorrect bit. But for any other situation, your are right. It works and is faster. _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
|
|
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
|