Go Down

Topic: 8 lose bits to 1 binairy number to convert. (Read 803 times) previous topic - next topic


I dearly love C++; it's like writing jiyuritsu haiku with a demonically possessed chainsaw.


One could get rid of the entire ternary operator by relying on the fact that HIGH is 1 and LOW is 0, and that will probably never change due to the way how avr-libc works with regard to the port/pin registers.


relying on the fact that HIGH is 1 and LOW is 0, and that will probably never change

It's true, Dominik, one could do that. And it would probably not cause problems in the future.

But, for me, probably isn't good enough. I've developed a number of habits over the years based on shortcuts just not being worth the trouble. One of these is never to assume that a macro won't change; I won't rely on anything other than the contract a macro implies. I have seen numerous problems caused by doing otherwise.

In this case, I actually the like the result of this habit; the code says "if the input is high, insert a 1, otherwise insert a 0." It's nicely self-documenting, and I like that, especially in code I write for somebody new to the environment.


If you wanted to be slick, you could do it quite easily (and a lot faster, with a lot less code) using the port input pins register.

Assuming you used pins 0-7 (yes, using the RX/TX may not be safe =), you could quickly do:

Code: [Select]

result = PIND;

Yup, that's it =)

PIND = one bit per state, where the first bit (Bx) is pin 7 and the last bit (B0000000x) is pin 0.  Thus, Binary one = B00000001, and only having pin 0 high would cause PIND's value to be B00000001.  If you spread the pins across two registers, you'd need to do some bit-shifting and some and operations to get the right values in place, but that's pretty easy to do.

The drawbacks?  You have to use PIND which may change on different processors.  The benefits?  If you're doing time-sensitive stuff, this will run eons faster than 8 digitalRead()s, and will compile to a lot less code.



The drawbacks?

Also not portable across several different AVR chips supported by the Arduino platform.


Go Up