taking bits from bytes. need help

Hi i want to take the first 4 bits from a binary and the last 4 bits from another binary example

byte temp_1=B*10110000; byte temp_2=B00000111*; byte temp_3;

i want to store on variable temp_3 the content 101101111 (it has actually the first 4 bits "1011" from the temp_1 and the last 4 bits from the temp_2)

byte temp3=(temp1&B11110000)¦(temp2&B00001111);

thanks, but how does this works?

http://arduino.cc/en/Reference/BitwiseAnd, this should explain things.

While maybe not always the fastest solution, Arduino does have some built in bit operations that you can use:

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

Be sure to check out the other bit commands listed at the bottom of that page.

Lefty

Not the best explanation I've seen so far.

In short, the 0 bits in the constant operand (say B11110000) in the AND operation will erase the content of the other operand, while the 1 bits preserve the content of the other operand.

So if you do (00000001 AND temp) then the result is 7 zeros and the last digit is the same as the last digit of the temp. This erases the high 7 bits of temp and only preserves the last bit in the result.

If you do (10000001 AND temp) then the result is 6 zeros and the first and last digits are the same as the first and last digits of the temp. This erases the middle 6 bits of temp and only preserves the first and last bits in the result.

Just imagine the 1's are punched holes and 0's are no holes on a sheet and you have 8 answers on a bubble sheet exam in temp. You line the two sheets up and only the answers behind a hole is seen (whatever bit value 0 or 1), the rest is not seen (0's).

acmiilangr:
thanks, but how does this works?

The way I find to think of how “and” ("&") works, is quite simply that, for each bit in a byte that’s being “AND’ed”, “if this bit AND that bit are 1, the output bit is 1, otherwise it’s 0”. Meaning, a byte consisting of 10110000 “AND” a byte 00000111 , take each bit: “1 and 0 = 0”, “0 and 0 = 0”, “1 and 0 = 0”, “1 and 0 = 0”… in the end you end up with 00000000, because there are no bits with “1” in both places. It’s mostly used to turn a bit off - if you want to turn off bit 6 in “01101011” without affecting other bits, you would use “01101011 OR 11011111” - that is, all bits in the “AND” are “1” except the one you want to turn off - so the output would be “01001011”.

“OR” (the “|”) is what you need here. In that case, with the same binary bits as above, 10110000 “OR” 00000111, we think different: “1 or 0 = 1”, “0 or 0 = 0”, “1 or 0 = 1”, “1 or 0 = 1”… and in the end, you come out with 10110111, each bit sort of “falls through” into the other. It’s kinda the opposite of “AND”. It’s often used to turn bits on, unlike “AND” which is used to turn bits off.

Problem is, there’s no simple cut-and-dry way to “take these 4 bits and put them into these 4 bits”. You could shift one part over by 4 bits to put it in place and then “OR” it with the first so-called “nibble” to make 4 bits, but it would have to be assigned to a zeroed-out variable (or a new one, like using it inside a function argument) to make sure the “OR” works properly. Since “OR” can only turn bits on, if you use it on an existing number, it may conflict with what’s already in the variable. Personally, I often use “(varLo4 & 0xF) | (varHi4 << 4)” to mix two 4-bit “nibbles” together - the “varLo4 & 0xF” isolates the low 4 bits by “ANDing” it with 00001111 to zero-out the 4 bits that varHi4 is going to get “OR’d” into.

Hope this helps :wink:

edit: Updated the examples to use OP’s requested binary :wink: