I have a bit-addressable var (ibase) that I am loading with a value and then rotating--placing the msb on an I/O pin (SData). The SData pin never changes--it stays high. What am I doing wrong? Thanks!
for(i=0;i<8;i++) { SClock = 0; SData = ibase^7; ibase = _irol_(ibase,1); delay(); SClock = 1; timer0_delay(5); }
This line:
SData = ibase^7;
doesn't do what you think. You've fallen into the somewhat common trap of believing that the
sbit var=bitvar^bitno
syntax extends to all expressions involving bit-addressable variables. Well, it doesn't. You need something along the lines of
SData = (ibase & 0x80) ? 1 : 0;
Wow, thanks! That did it. So that I don't continue to suffer in ignorance, just what did that do? (I understand the 0x80 mask, but what about the ? 1 : 0 syntax)?
Thanks.
"(I understand the 0x80 mask, but what about the ? 1 : 0 syntax)?"
It is called a "conditional expression" and is equivalent to:
SData = ((ibase & 0x80) != 0);
and:
SData = !!(ibase & 0x80);
Got it--nice shorthand. Thanks.
It is called the Ternary Operator - it's a standard ANSI 'C' feature.
As Dan says, the Ternary Operator is used to create a conditional expression
a = (x==y) ? b : c;
is equivalent to
if( x==y ) { a = b; } else { a = c; }
Better continue with the C classes.
When ^ is used in a C/C++ expression, it is a bit-wise xor. This is all part of the standard, while the ^ used to declare bit variables in Keil is a Keil-specific extension required since the C/C++ standards do not have support for bit variables.
Thanks to all who posted the useful comments. I have always programmed in assembler (and frankly, considered embedded C to be a bit of a bloated alternative). I'm not taking classes, just studying on my own, which is why I asked for help on the syntax.
The gratuitous "Better continue with the C classes" was undoubtedly the least helpful, because it was designed to be denigrating, not illuminating. After all, I said that I understood the concept perfectly several postings ago.
Ooohh ... nothing contentious there, then...!
;-)
But you are actually wrong on several accounts.
The sentence "Better continue with the C classes." was not designed to be denigrating.
You did use the xor construct, missing the meaning of ^ when used in a C expression, as in contrast to the Keil extension of declaring bit variables.
And no, you did not several posts up said that you understood the concept.
You did say: "Wow, thanks! That did it. So that I don't continue to suffer in ignorance, just what did that do? (I understand the 0x80 mask, but what about the ? 1 : 0 syntax)?
Thanks."
That must be read as relating to the ternary operator.
After having received a quick lesson about the ternary operator, you answered with "Got it--nice shorthand. Thanks."
Still no mention that you understood the ^ operator.
So, since only the ternary operator was described, I thought that I could be nice - yes, as in helpful - and continue what Dan Henry and And Neil started by describing the meaning of the xor operator.
I realize that "Better continue with the C classes" can be read as "You should continue with...", but if you think about it again, it can also be read as: "Since we are describing C functionality, let's continue by describing this construct, that you are also having troubles with."
It is always dangerous to start throwing punches around, unless you are really, really sure what you are doing.
Actually, I like the maintainability of using C. It's much easier to revisit. Also, with more on-chip code space available these days, extra-tight code isn't as critical (unless there are time-critical requirements--then there's always the ability to add asm). I used the ternary definition as a springboard to find more information about those operators, and it has been very helpful.
Not really throwing punches; I expressed my gratitude to all--you too. Obviously, I am a neophyte on the HL side of embedded programming, or I would not have asked such an (for you) elementary question. I didn't just take the OPs suggestion and paste it, but also wanted to know _how_ it worked. The reason that I said that I thought the classes reference was gratutitous, was that my inexperience was implicit in such a fundamental question. Of course I need to learn more. Anyway, thanks again for a more complete explanation--it all helps!
"or I would not have asked such an (for you) elementary question." I didn't think it elementary. There is always new things to learn. The important thing is to stay and learn whenever you see something new.
I have seen at least one person who had programmed in C (PC-class programs) for 10 years and did not know about the xor operator ^. He was 100% sure he new everything, but since he had never bothered to really read the language standard, and considered a book about C stupid to read, he just did not have any way to find out - he stumbled first when he got a piece of code written by someone else and couldn't decode the (as he thought) non-standard code. The situation would have been quite funny if it hadn't been such a traumatic experience for the poor sod ;)
When I was first trying to determine why my original code wasn't behaving, I looked at the disassembled code and saw the xor opcode, but couldn't understand why it compiled that way. Now that you have explained about the ^ operator, it makes sense.
"... read the language standard ..."
Well, since you've gone there, I'll pick one nit. What is being referred to here as the "ternary operator" (?:), the standard calls the "conditional operator". However, I have seen "ternary" used in places other than just here.
K&R, for example...
"K&R ..."
That's one of the places -- 'the standard' before "the standards".