sketch formula line

position = pinval_1 || (pinval_2 << 1) || (pinval_3 << 2) || (pinval_4 << 3);

pinval_1, pinval_2, pinval_3, pinval_4 are the result of reading pins.

What would I see from "position" using serial.println(position)?
I using a 4 bit hex complimentar BCD switch and I know it works, seeing the right HIGH's and LOW's pattern for each switch click. All I see from serial.println is LOW when on click 0, and HIGH every where else. I think the above formula is in error, but really don't understand it.

Another member of this forum showed this as a solution for a similar question.

The formula is slightly wrong. It should be:

 position = pinval_1 | (pinval_2 << 1) | (pinval_3 << 2) | (pinval_4 << 3);

Pete

Hey Pete,

You're a genius! In the Arduino Cookbook, I now see a big difference in logical "or" ( || ) and bitwise "or" ( | ). Looks like this will be a study area for me!
Now, each click shows right number.

Thanks you, sir

What would I see from "position" using serial.println(position)?

I really think that you'd learn more by just trying it.

That snippet doesn't tell US anything about the input values (in pinval_N, so we can't guess what the output would be. YOU have access to the values, so you should be able to define a relationship between the input and the output.

Of course, you could also learn (by reading the reference page) what << does, and KNOW what the relationship between the input and the output is.