Jump to content

Recommended Posts

Hi,

 

looks like the var.bit method is not working when the variable is a struct member.

 

typedef struct {

unsigned char test;

} testtype;

 

testtype var;

 

 

var.test.3= 0;

 

 

this seems to be a not known construction by the compiler or....?!

 

fred

Link to post
Share on other sites
Hi,

 

looks like the var.bit  method is not working when the variable is a struct member.

 

typedef struct {

unsigned char test;

} testtype;

 

testtype  var;

 

 

var.test.3= 0;

 

 

this seems to be a not known construction by the compiler or....?!

 

fred

 

How can you assign to .3 ?

 

var.test = 0; should work, but .3 is not defined as anything in your structure.

 

If you had done:

typedef struct { unsigned char test[4] } testtype;

then i would think "var.test.3= 0;" would work fine as your then assigning

to the third element ... or is that right.. bit rusty on my structure stuff.

Link to post
Share on other sites

The OP is refering to bit fields.

 

Under your scenario

 

typedef struct { unsigned char test[4] } testtype;

then

var.test[3]= 0; would be valid

 

 

Hi,

 

looks like the var.bit  method is not working when the variable is a struct member.

 

typedef struct {

unsigned char test;

} testtype;

 

testtype  var;

 

 

var.test.3= 0;

 

 

this seems to be a not known construction by the compiler or....?!

 

fred

 

How can you assign to .3 ?

 

var.test = 0; should work, but .3 is not defined as anything in your structure.

 

If you had done:

typedef struct { unsigned char test[4] } testtype;

then i would think "var.test.3= 0;" would work fine as your then assigning

to the third element ... or is that right.. bit rusty on my structure stuff.

Link to post
Share on other sites
The OP is refering to bit fields.

 

Under your scenario

 

typedef struct { unsigned char test[4] } testtype;

then 

var.test[3]= 0; would be valid

 

 

Yeah i figured that var.test[3] makes more sense.

Link to post
Share on other sites

sorry for my late response. ;)

____

 

 

In your example var.test[3]= 0 you are setting the char content and not bit 4 to zero

 

normally you are able to access bit 4 in a variable using this construction:

 

unsigned char test;

 

test.3= 1; // similar to set_bit( test, 3)

 

 

what I meant is having the same construction for a struct variable.

 

=> instead of set_bit(var.test, 3) usage of var.test.3

 

This is usefull when setting a bit to the content of a variable.

 

normally you get something like

 

if ( var ) set_bit( var.test, 3) else clear_bit(var.test, 3);

 

this could be replaced by

var.test.3= var; if the existing construction also would work for struct variables.

Link to post
Share on other sites
sorry for my late response.  ;)

____

 

In your example var.test[3]= 0 you are setting the char content and not bit 4 to zero ...

 

 

Yes, i agree with you. *emte gets out the anti-rust spray*

 

In retrospect i am not sure why i even tried to respond since i avoid using the

set_bit/clear_bit functions in preference to bit masking. i have yet to verify

if it is more efficient under SourceBoost or not, so i still have a few old habits.

 

If you happen to know, i'd be curious, otherwise i'll have to use and track it in

my next test app.

Link to post
Share on other sites

I'm using bit maskinging aswell (macro's) but some of them are not very self explaining and I really need to work them out very carefully to get them do what I want... B)

 

Doesn't see so much profit in using the set_bit, clear_bit and test_bit. Really missing the interresting one: set a bit to a the value in var x.

 

a bitmask to get this done this looks pretty unreadable.. ;)

Link to post
Share on other sites

Set_bit and clear_bit are very efficient for manipulating single bits. In operations where the bit number is a constant they compile down to a single PIC bsf or bcf instruction.

 

Masks are more efficient when the need is to manipulate a group of bits. #defining the mask to a meaningful symbol will make the code more readable.

 

EG suppose we are using bits 0...4 of port b as a row of leds for a meter bar.

 

using

 

#define 0x1f METER_LED_MASK

 

and using it

 

latb &= ~METER_LED_MASK; // clear any set bits

latb |= meterleds & METER_LED_MASK; // set leds leaving unused portbits undisturbed

Link to post
Share on other sites

Pixie,

EG suppose we are using bits 0...4 of port b as a row of leds for a meter bar.

 

using

 

#define 0x1f METER_LED_MASK

 

and using it

 

latb &= ~METER_LED_MASK;                // clear any set bits

latb |= meterleds & METER_LED_MASK;      // set leds leaving unused portbits undisturbed

I think you mean:

#define METER_LED_MASK 0x1f

 

alternatively you can you enumerations eg

enum 
{
   METER_LED_MASK1 = 0x1f,
   METER_LED_MASK2 = 0x1e
};

 

Regards

Dave

Link to post
Share on other sites

#define set_bool2bit(var,bl,bt) var=((var&((1<<bt)^0xffff))|(bl<<bt))

 

var=24;

bt=3

bl= false;

 

set_bool2bit(var,bl,bt);

 

as bit masking replacement for

 

if ( bl ) set_bit(var, bt) else clear_bit(var, bt );

 

hm ;)

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...