hello, I would like to do a bit test of a unsigned char variable. It's like a jb in assembler.
here is my code in c.
unsigned char displ; unsigned char display3 = 0xA8; unsigned char i; sbit display_data = P1^0; displ = display3; for (i=0; i<7; i++) { if (displ^0 == 1){ display_data = 0; } else { display_data = 1; } displ>>=1; }
I don't have any warning or error, but the result of test is wrong. is it possible to do in c?
tkx.
The standard, portable way to test bits in C has been presented -- the bitwise and (&).
On an 8051, you can also test bits directly if the bit happens to be in an SFR or bit-addressable RAM.
if (TI) ...
It's generally not worth moving a bit to bit-addressable RAM just to access its bits. If a variable lives there, fine; if you just have some byte, use &.
On the all-important braces question:
- I agree that consistency is more important than any of the styles
- Lef to my own devices, I go for seperate lines and indented braces:
if (TI) { } else { }
- My favorite style is not the Pascal/C "block statement" syntax, but rather the Algol/Modula/Ada style where the grammar allows multiple statements in control structures:
if (TI) else for () end end
The begin/end {/} delimiters are grammatically unnecessary and just visual clutter. But there's nothing to be done about the C grammar at this point. Unfortunately, C is popular enough that nearly all the minor languages feel obliged to be "C-like" so that people will use them.
"On an 8051, you can also test bits directly if the bit happens to be in an SFR or bit-addressable RAM."
To be precise, that should be "...in a bit-addressable SFR or bit-addressable RAM."
Not all SFRs are bit-addressable.
Thanks for the correction. Not all SFRs are bit addressable.