Hey all,
If I have a 16 bit variable called "zone" and I need to read bit 15 or write bit 3 how do I go about that ?
thanks
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hey all,
If I have a 16 bit variable called "zone" and I need to read bit 15 or write bit 3 how do I go about that ?
thanks
Assuming lsb starts from bit 0 and msb is bit 15.
#define BIT0 0x0001
#define BIT15 0x8000
// reading bit 16
if ((zone & BIT15) == BIT15) // Check whether bit15 is 1
{
Statement 1;
}
else // Bit 15 is 0
{
Statement 0;
}
// Write 1 to bit 3
zone |= BIT3;
// Write 0 to Bit 3
zone &= ~(BIT3);
You can also implement bit fields as is done in the heade files. This structure is described here: http://www.ti.com/lit/an/spraa85c/spraa85c.pdf .this has alot of upfront cost organizing everything, but the compiler will take care of the mechanics of the reads and writes (by doing some bit manipulations, similar to above).
Thanks Vivek,
I was hoping to avoid having to set up a whole structure just to handle it, and it lks like you may have helped me. My interst was to read the sign bits of 3 variables and set or clears bits 0:3 of another variable accordingly. I'm attempting to save cycle time by avoiding shifts if possible, which is why I asked .
David thanks for your assistance, Hopefully I won't need a struct!