Me and a friend of mine are having troubles solving a problem with a particular given code for our final Test in Electronics.
In January we are electronics technicians-to-be and not Programmers, and for the function of our connected circuit we want to understand how the program works in order to get a deeper understanding of what our circuit will do when applied.
Here is one part of the code that we struggle with:
Beforehand, "BestZelle" is not defined as an integer, which we don't understand. But, regardless of that we thought "BestZelle" should be within the range of 0 to 4 (decimal) or 1 to 4 (decimal).
We don't know why the result of the comparison of "BestZelle" and "0x00**" is bitshifted to the right. In our understanding, the result of "digitalWrite" just can be HIGH or LOW. Thats is what leads our heads to an instant burn-out.
We apologize in any circumstances for our lack of knowledge, but we have enough to do with operational amplifiers and the Exam preparation.
Each bit of BestZelle is tested by using logical AND with a 1 and the result (0 or 1) is shifted right the correct number of times to put the result in bit 0 which is then written to the output pin.
The digital pins will, therefore, be set to the bit pattern of BestZelle which is probably a byte
Each '&' operation will convert every bit in BestZelle to '0' except where there is a '1' in the binary, which is left as it was in the original. The result is then shifted to the right '>>' by the number of places specified. In the end, the value is either '1' or '0' depending on the original value of BestZelle.
For example, if BestZelle is 1234 which is binary 0B0000010011010010 and you apply what is referred to as the 'mask' from digitalWrite(D6, (BestZelle&0B0000000000010000)>>4); the result is 0B0000000000010000 and when you shift it right 4 positions, you end up with 0B0000000000000001 which is '1'.
GolamMostafa:
Because the destination is 1-bit location, the data (data source) should also be 1-bit. Therefore, the following code would be more appropriate --
You are doing exactly the work that bitRead( ) would have done by normalizing it to bit zero! What a waste. Plus you call an extra function to no effect whatsoever.
That's SO ugly...
I'd have written something quite different, which maybe I'll post in a week or so, in case your "final test" involves re-writing the code in a better way...
boolrules:
You are doing exactly the work that bitRead( ) would have done by normalizing it to bit zero! What a waste. Plus you call an extra function to no effect whatsoever.
This is not I who is making the waste; if there is any waste at all that can be attributed to the function/macro digitalWrite(arg1, arg2); itself (Fig-1).
Figure-1: digitalWrite() function
The 2nd argument of the function demands that the value should be HIGH, which will be assigned to arg1 that is 1-bit wide. HIGH could be any value from 0x0001 - 0xFFFF; 0x0002 is also a HIGH value; but, it is not going to activate arg1 of the function. Therefore, HIGH must be at least 0x0001 -----> 1. Any sensible person will try his best to remain consistent with data types. If destination is 1-bit wide, the source also be 1-bit wide. When you see this is a waste, I would respect your criticism. However, I will not complain this style :digitalWrite(D1, (BestZelle & 0B0000000000000001) >> 0); due to @Pauls; this style has consumed extra storage by inserting (optional) spaces in order to bring beauty to the eyes?
Spaces only take up spaces in the source file. Seems that the OPs were satisfied around about post #3. The rest of this appears to be some kind of a measuring contest. 'Beauty is in the eye of the beholder' someone once said, or perhaps the poor sod that has to modify it, but it was understanding that they were looking for.
Hey, thank you very much for getting us to the heart of the problem for the second time, DKWatson.
We are absolutley overwhelmed (awed) by the helpfulness and expertise of ALL YOU GUYS. :-*
Again, thank you very much, we have learned a lot!!
Regarding the code: The whole program is predetermined / given to us by the examination board, so we are not allowed neither have we the duty/task to change it or to make it better. All of our concerns were to get a deeper understanding of some notations and functions of the code itself, to get to know something we quite don't have to know, just by interest.
But besides, we had quite a big laugh about the committee, because some of you proposed "smarter ways of doing this"... ;D