Self Deleted...
Printable View
Self Deleted...
There are many ways to do this. With an 8-bit field, I like to use unsigned char. since that is an object with 8 bits. Copy the original byte to be tested to temp variable of the same type.
Then a "mask" can be created to test the temp byte. Remember, "or" turns bits on and
"and" turns bits off when the mask is set up properly. A brute force method (I'm assuming you're new at C) would be to create 8 masks to test each bit position. Set up a loop to test the result of each of the 8 mask operations. When you get some code and it works, try
to pulling it out of main() and into its own function.
EDIT: Clarification - Use the mask to turn off the bits not being tested, then read the variable to check if the
bit was on or off.
Edit#2: 8 masks is a bit too much "brute force" for me, but I suggested it that way to give you a clear understanding. A better
way would be to have ONE mask, and then increment the bit being tested i.e change it to test for byte values 1, 2, 4, etc.
NP, we can do this! (I'm very thankful that my first C program was "Hello World"
and not stuff like this.
While I'm at it, I'd like to announce to the world that I'm now reading
K/R The C Programming Language, 2nd addition!
No longer torture this board with questions because of misinformation from
the "learn C 21 Days"....
sorry for troubling again,
but in my situation the variable X is not in my control. it keeps on changing. so i need to keep monitoring it. any one bit or all the bits may go high (1) every one millisecond. so do i have to write like this say for bit 1 mask off and check variable? bit 2 do the same bit 3 do the same... or am i getting it all wrong
I had learnt c programming, only printf and scanf stuff ages ago. never used it for any application. an electrical engineer is better off handling high voltage stuff. overconfidence put me in this job( trying to program an embedded board ) stupid me. but right now i have no choice so struggling with it..
Thanks for helping out :)
I looked at what I wrote above, the mask should be used to turn off all bits except
for the bit position being tested for. Sorry, I'm still waking up!
First you have to write some code, using code tags. Here is a useful link
on the FAQ here: Cprogramming.com FAQ
Check out the "How do I...?" links
sorry had got logged off My battery died :(
Thanks ..