Urgent question for final Test

Hello, and greetings from Germany.

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:

digitalWrite(D1, (BestZelle&0x0001)>>0);
digitalWrite(D2, (BestZelle&0x0002)>>1);
digitalWrite(D3, (BestZelle&0x0004)>>2);
digitalWrite(D4, (BestZelle&0x0008)>>3);
digitalWrite(D5, (BestZelle&0x0010)>>4);
digitalWrite(D6, (BestZelle&0x0020)>>5);
digitalWrite(D7, (BestZelle&0x0040)>>6);
digitalWrite(D8, (BestZelle&0x0080)>>7);

Annotations:

  1. 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).

  2. 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. :stuck_out_tongue_closed_eyes:

We apologize in any circumstances for our lack of knowledge, but we have enough to do with operational amplifiers and the Exam preparation.

In advance, thank you very much for your help!

Marcus and Janos. =)

BestZelle is probably a byte, so will have 8 bits

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

Note that there are smarter ways of doing this

Maybe better way to look at it is in its binary form,

digitalWrite(D1, (BestZelle&0B0000000000000001)>>0);
digitalWrite(D2, (BestZelle&0B0000000000000010)>>1);
digitalWrite(D3, (BestZelle&0B0000000000000100)>>2);
digitalWrite(D4, (BestZelle&0B0000000000001000)>>3);
digitalWrite(D5, (BestZelle&0B0000000000010000)>>4);
digitalWrite(D6, (BestZelle&0B0000000000100000)>>5);
digitalWrite(D7, (BestZelle&0B0000000001000000)>>6);
digitalWrite(D8, (BestZelle&0B0000000010000000)>>7);

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'.

Wow, thank you very much for your quick help! Now it is a lot easier to understand what the program does.

Marcus and Janos

Maybe better way to look at it is in its binary form,

Even better with spaces, so the damned mess is readable:

digitalWrite(D1, (BestZelle & 0B0000000000000001) >> 0);
digitalWrite(D2, (BestZelle & 0B0000000000000010) >> 1);
digitalWrite(D3, (BestZelle & 0B0000000000000100) >> 2);
digitalWrite(D4, (BestZelle & 0B0000000000001000) >> 3);
digitalWrite(D5, (BestZelle & 0B0000000000010000) >> 4);
digitalWrite(D6, (BestZelle & 0B0000000000100000) >> 5);
digitalWrite(D7, (BestZelle & 0B0000000001000000) >> 6);
digitalWrite(D8, (BestZelle & 0B0000000010000000) >> 7);

digitalWrite(D7, (BestZelle & 0B0000000001000000) >> 6);

Because the destination is 1-bit location, the data (data source) should also be 1-bit. Therefore, the following code would be more appropriate --

 digitalWrite(D7, bitRead(((BestZelle & 0B0000000001000000) >> 6), 0));

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 --

 digitalWrite(D7, bitRead(((BestZelle & 0B0000000001000000) >> 6), 0));

I don't think so.

Whandall:
I don't think so.

Is there any argument in favor of your proposition?

It is overcomplicated crap code?

Whandall:
It is overcomplicated crap code?

But, not faulty?

GolamMostafa:
But, not faulty?

Who should care?

GolamMostafa:

 digitalWrite(D7, bitRead(((BestZelle & 0B0000000001000000) >> 6), 0));

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.

boolrules:
Plus you call an extra function to no effect whatsoever.

No, bitRead is a macro from Arduino.h, but that does not make it much better, it's just a waste.

#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
digitalWrite(D5, (BestZelle&0x0010)>>4);

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).
dWr.png
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?

dWr.png

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. :roll_eyes:

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 :grinning: