Go Down

Topic: Understanding this line " ? HIGH : LOW; " (Read 8 times) previous topic - next topic

Zoandar

Zoandar
  ---v/\/\/\v---

Coding Badly

I have another unexplained use of something which I would like to understand. In the code below, what does the following line using a single "&" do?


Bit-wise-and.

Nick Gammon


I have found a number of shortcomings like this one in the Arduino documentation. Really confusing for a newbie and time consuming to search for answers. Is there a mechanism to provide feedback to the developers on such matters?


I feel for the developers on this one. Since they have basically provided an IDE that uses C++ using a very-widely used compiler (gcc), they could have just said "go look up how C++ works". And there are many, many good tutorials on it. And a lot of documentation. But of course this "go find out for yourself" attitude is not beginner-friendly. So they try to summarize C (and C++) on their site. But sooner or later they will miss something out (like the "?" operator).

Now they can add that in, but then someone will ask how constructors/destructors work, or some other obscure language feature (like pragmas). And whilst I like the idea of keeping everything central, sooner or later you have to go find out a bit more about C yourself.

And then there's the question about "is C friendly for beginnners?". Well, possibly not. But the easier you make it for beginners the harder you make it for people who pass beginner stage and want to actually do something useful.

Let me give you an example ... a year or so ago my son was working on a PICAXE chip (programmed in their version of Basic). And I suppose you can say that Basic is easier to learn than C. But once you get past making a few beeps and flashing a few lights, you start hitting limitations. Like, the Basic itself has an overhead that takes up valuable memory space, so your maximum program size is more limited than using C.

So anyway, we were making a program that played a small sentence in Morse code. So we wrote a subroutine to do a dot, and another to do a dash, to try to keep memory usage down. But then we hit some bizarre limitation, that you could only use something like 16 GOSUBs in a program. Not nested, just a total of 16, anywhere! It made me wonder how they implemented it ... some sort of jump table rather than genuine subroutine calls.

So my point is, you are trading off making it easy for people on their first day (of which they will only have one) against making it easy for them in subsequent days, of which there will be many.

Graynomad

I think Picaxe is a good platform in many ways, but if you hang around the forum for long you'll notice that half the threads are about getting around the platform's limitations.

For example most threads that ask "How do I chew gum and walk at the same time, all at 500Hz" usually get a response like "Use one Picaxe to chew gum and another to walk".

To be fair though it is I think appropriate for its intended audience, ie beginners and school kids, although I have seen some pretty reasonable projects using them.

BTW that's interesting about the gosub limit, I always assumed it was a stack depth issue and as few programs get 16 deep in nested functions I didn't see it as a serious limitation.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Zoandar


I have another unexplained use of something which I would like to understand. In the code below, what does the following line using a single "&" do?


Bit-wise-and.



Thanks. I Googled "bit-wise and" and read part of a wiki about it. Seems rather complex considering it is working with individual bits. I am not sure why they chose to implement such a seemingly advanced command in a book for beginners. Surely there must have been an easier-to-understand way to accomplish the same task.
Zoandar
  ---v/\/\/\v---

Go Up