Adding binary numbers

Hello. OK, how do I explain this...

I have an MCP23017 switch that requires a value 0-255 to turn on the 8x outputs.

I have 8x inputs that can be configured in a setup menu to allow certain combinations of those outputs to be operated.

For example:

Switch 1 configuration: 00110001 This would be outputs 3,4, and 8 on.
Switch 2 configuration: 11000000
Switch 3 configuration: 11000000
Switch 4 configuration: 10000001
Switch 5 configuration: 00000100
Switch 6 configuration: 11000000 This would be outputs 1 and 2 on.
Switch 7 configuration: 00001000
Switch 8 configuration: 11000000

These are stored in EEPROM as as 8x variables with a value 0-255.

How do I add these 8x switches up, so that I end up with a single binary result of all the switches positions?

You might have more than one switch on, and therefore that particular output will be flagged as on more than once.

So, using the example above:

Switch 1 configuration: 00110001
Switch 2 configuration: 11000000
Switch 3 configuration: 11000000
Switch 4 configuration: 10000001
Switch 5 configuration: 00000100
Switch 6 configuration: 11000000
Switch 7 configuration: 00001000
Switch 8 configuration: 11000000

Result = 11111101

I am sure it has something to do with Bitwise, but I have not worked it out yet.

Sorry - all the binary lists line up until you post it :confused:

You are looking for the bitwise OR operator

byte a = 0b00110001;
byte b = 0b11000000;

byte combi = a | b;

Hi,
simply compute the result by binary OR (|)
something like this:
Result=00000000b;
Result |= conf1;
Result |= conf2;

And by the way this is not adding. :slight_smile:

So, with fear of being ripped to shreds...

Why doesn't this work?

MCP23017_value = (Input1_assign,BIN) | (Input2_assign,BIN) | (Input3_assign,BIN) | (Input4_assign,BIN) | (Input5_assign,BIN) | (Input6_assign,BIN) | (Input7_assign,BIN) | (Input8_assign,BIN);

I don't understand how an OR function combines the binary numbers. To my simple mind, that would be an AND function?

OK. With some code alterations, and the following statement, I got this working with the | function.

MCP23017_value = Input1_assign | Input2_assign | Input3_assign | Input4_assign | Input5_assign | Input6_assign | Input7_assign | Input8_assign;

I assume this is 'comparing' then, not adding?

Many thanks for the advice.

SteveRC2017:
Why doesn't this work?

MCP23017_value = (Input1_assign,BIN) | (Input2_assign,BIN) | (Input3_assign,BIN) | (Input4_assign,BIN) | (Input5_assign,BIN) | (Input6_assign,BIN) | (Input7_assign,BIN) | (Input8_assign,BIN);

Because that BIN as a second "parameter"* is only valid for a function call, more specific, a print() call. You can't just use it randomly somewhere else.

SteveRC2017:
I don't understand how an OR function combines the binary numbers. To my simple mind, that would be an AND function?

If I tell you to turn to eat an apple if you're at home OR on vacation, will you eat an apple if you're at home but not on vacation?

And when I tell you to turn to eat an apple if you're at home AND on vacation, will you eat an apple if you're at home but not on vacation? :wink:

Same here, you want a 1 in the final variable if one of the others has a 1 there, not if they all have a 1 there.

Have a look at the truth tables of OR and AND.

SteveRC2017:
I assume this is 'comparing' then, not adding?

No, it's OR'ing them :wink: And in words I think (bitwise) "adding" is the most fitting but a bit ambiguous.

  • It's a bit more complex then that. Brackets here don't indicate a function call but just a group which is evaluated before the rest is done, same as in math. The comma is a bit of a weird operator in normal lines of code (aka, NOT in a function call). It tells the compiler to take the first operand, evaluate it, throw away the result, and go on with the second operand. Aka, here the whole 'Inputx_assign' is just thrown out.