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

Copy only one bit from integer to another

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



Joined: 25 Dec 2004
Posts: 9

View user's profile Send private message

Copy only one bit from integer to another
PostPosted: Fri Feb 06, 2009 3:01 am     Reply with quote

I was using CC5X compiler before CCS. I was copying one bit with following instructions.
a and b are integers
Code:

a.1=b.7;

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







PostPosted: Fri Feb 06, 2009 3:35 am     Reply with quote

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

View user's profile Send private message

Re: Copy only one bit from integer to another
PostPosted: Fri Feb 06, 2009 6:57 am     Reply with quote

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:

Code:

  b.7 = a.1;


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

View user's profile Send private message

PostPosted: Fri Feb 06, 2009 9:27 am     Reply with quote

"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

View user's profile Send private message

PostPosted: Fri Feb 06, 2009 11:01 am     Reply with quote

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