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
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)
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":
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.
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.
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.