How can i merge 8 variables to 1;

hi to all and nice to meet you
first of all sorry for my bad english
secondly i hopw you can help me with my problem.

i need to merge 8 bits to 1 varaible.
For example.
i have "temp" variables that each one stores 1 or 0 from digitalreading from arduino pins:

int temp[7];
byte mybinary;

temp[0] = digitalRead(pin1); //1 for example
temp[1] = digitalRead(pin2); //0 for example
temp[2] = digitalRead(pin3); //1 for example
temp[3] = digitalRead(pin4); //0 for example
temp[4] = digitalRead(pin5); //1 for example
temp[5] = digitalRead(pin6); //0 for example
temp[6] = digitalRead(pin7); //1 for example
temp[7] = digitalRead(pin8); //0 for example

how can i merge these values (from temp) to "mybinary" variable;

all i want is "mybinary" variable store "10101010" value like this

mybinary=10101010 or mybinary=B10101010

i tried mybinary=temp[0] & temp[1] &..... &temp[7] but it didtnt work
:frowning:

please help me

Try mybinary=temp[0] | (temp[1] << 1) | (temp[2] << 2) | (temp[3] << 3) and so on up to 7

it works! but not completly, it doesnt stores the last bit and i dont know why

and another question how can i type "|"? and what that does in arduino?

The last bit can go wrong if the compiler gets confused by thinking variables are signed when they are unsigned, and the size of the array should be 8 not 7. Try changing "int temp[7]" to "unsigned int temp[8]" or "byte temp[8]".

"|" is bitwise or, on some keyboards it is a broken bar (small gap in the middle)

it doesnt work. i tried both

The issue is this definition:

int temp[7];

temp is defined to have 7 elements with index from 0 to 6. If you need 8 elements, you must define it as "temp[8]".

The alternative is to do it in one step such as:

  byte my8bits  = digitalRead(pin1) | (digitalRead(pin2)<<1) ... | (digitalRead(pin8)<<7)

damn, it still doesnt works. have anyone tried that?

I have understand that the problem is not from temp varaible but it can't store more than 7 bits!!!!

i tried mybinary=1|0<<1|1<<2|0<<3|1<<4|0<<5|1<<6|0<<7;
and the print of that is 1010101

any help?

i tried mybinary=1|0<<1|1<<2|0<<3|1<<4|0<<5|1<<6|0<<7;
and the print of that is 1010101

That's correct isn't it? The "0<<7" is the highest (most significant) bit of your answer, and is not printed (leading zeroes are usually dropped). Then you have a 1 for "1<<6", a 0 for "0<<5", a 1 for "1<<4", a 0 for "0<<3", a 1 for "1<<2", a 0 for "0<<1" and a 1 for "1":

0     1     0     1     0     1     0     1
0<<7  1<<6  0<<5  1<<4  0<<3  1<<2  0<<1  1<<0

What's the problem?

Andrew

mmm i didnt know that leading zeroes are dropped,

sorry i am new on that....

but wiill be any problem if i shiftout that variable in shift registers?
i mean, is 1111 the same with 11110000?

i mean, is 1111 the same with 11110000?

No, because 1111 is actually 00001111 (if it's an 8-bit number) and that's clearly different to 11110000. Leading zeroes are those at the start (left hand end) of the number.

Andrew

so if i shift out 1111, it is the same with 00001111 right?

I've not personally used shiftout, but as those are the same binary number (again, if we're talking 8-bit numbers here) then I don't see how the results could be different.

Andrew

shiftOut works on BYTE size date (8-bits).

http://arduino.cc/en/Reference/ShiftOut

But it has a choice of shifting out most-significant bit first or least-significant bit first, that is shifting to the left or shifting to the right.

The byte is always 8-bits in memory. it's only the print() function that is dropping leading zeros when displaying the value.

So in memory, 1111 is the same as 00001111.

For shiftOut, if you do MSBFIRST, then 1111 is the same as 00001111, because you are starting with the left bit first.

But if you do LSBFIRST, then 1111 would be the same as 11110000, because you are starting at the right-hand side of the 00001111 that is in memory.

thanks alot for the answer. i solved my problem thanks to you

last question;
how can i store the "full" binary with the zeros in the start in a variable (so i can use MSBFIRST) ?

It is already is stored that way. In the memory, the byte is always 8-bits long. Those leading zeros are there. (And int is 16-bits; long int is 32-bits)

It is only the print() and println() functions that are not showing them to you because normal human convention is to not put leading zeros on numbers.

If it would be pins 0-7 instead of 1-8 the whole result would be found in "PIND" without any further processing. This would be much simpler any very much faster.

Cheers, Udo

Chears thanks!