I am reading data from an IR sensor array (8 sensors) and converting the ADC result to binary via a set threshold.
If n is the current result, for example: (N.B: n-1 is the previous result)
n-1 = 0b00111000
n = 0b01110011
I want to perform some logic operations in my Arduino code such that the result would yield 0b01110000 since the first group of 1's in 'n' 'anded' with 'n-1' yields a non-zero value, hence this is not discarded. Whereas the second gorup of 1's (consisting of two bits) 'anded' with 'n-1' yields a zero value, this is discarded.
Another example:
n-1 = 0b00111000
n = 0b10001110
Would result in 0b00001110 since the first group of 1's (consisting of 1 bit in this example) 'anded' with n-1 yields zero, hence this is discarded. Whereas the second group of 1's (consisting of 3 bits) 'anded' with n-1 yields a non-zero result. Hence, this group would not be discarded.
I can't figure out what bitwise operations I should use and how to separate the current result in separate groups of 1's to achieve this result.
-
2\$\begingroup\$ Your description is hard to follow, but I don't think this can be done simply with bitwise operations. I think you are going to write a more rigorous definition of your algorithm and use some loops and conditional statements. \$\endgroup\$Elliot Alderson– Elliot Alderson2021年04月17日 00:52:43 +00:00Commented Apr 17, 2021 at 0:52
-
\$\begingroup\$ @ElliotAlderson I edited it for more clarity, the second example should explain it better. I was hoping i could achieve this in less computational power to avoid using a bunch of unnecessary loops and conditional statements \$\endgroup\$Simon Sultana– Simon Sultana2021年04月17日 01:01:10 +00:00Commented Apr 17, 2021 at 1:01
-
1\$\begingroup\$ Bitwise operations don’t support the concept of a group of bits and so you can’t directly do this in the way that you want. Intuitively what you want is a function that can identify a group of bits and (presumably) check whether there’s a roughly-corresponding group in the previous byte. \$\endgroup\$Frog– Frog2021年04月17日 01:36:37 +00:00Commented Apr 17, 2021 at 1:36
-
\$\begingroup\$ Sounds like a crude way to quantize a low SNR ratio by clusters of bits. Why not improve the SNR in the 1st place since they don't appear to represent any IR current values and you are applying non-linear functions. Define the entire system otherwise , you may be chasing "skinny little rats" \$\endgroup\$Tony Stewart EE since 1975– Tony Stewart EE since 19752021年04月17日 02:48:29 +00:00Commented Apr 17, 2021 at 2:48
-
\$\begingroup\$ "I am reading data from an IR sensor array (8 sensors) and converting the ADC result to binary" - isn't the ADC result already binary? \$\endgroup\$Bruce Abbott– Bruce Abbott2021年04月17日 04:37:46 +00:00Commented Apr 17, 2021 at 4:37
1 Answer 1
Something like this: (?)
uint8_t
Solve(uint8_t n, uint8_t prev_n)
{
uint8_t result = 0;
uint8_t group_bits = 0;
uint8_t mask_bit = 0x80;
while (1)
{
if (n & mask_bit)
{
//If current mask bit is set in n, save it in the current group...
group_bits |= mask_bit;
}
else
{
//If current mask bit is not set in n, then we found a group of 1 bits...
if (prev_n & group_bits)
{
//Store to result if any of the current group of 1 bits is set in prev_n...
result |= group_bits;
}
group_bits = 0;
//Break loop if mask_bit shifted right 8 times...
if (!mask_bit)
{
break;
}
}
mask_bit >>= 1;
}
return result;
}
-
\$\begingroup\$ It wouldn't work if I had three groups of 1's. Or if the groups of 1's were in one nibble \$\endgroup\$Simon Sultana– Simon Sultana2021年04月19日 14:44:00 +00:00Commented Apr 19, 2021 at 14:44
-
\$\begingroup\$ @SimonSultana I guess I misunderstood the question. I modified the code, hopefully this is what you want. \$\endgroup\$Unimportant– Unimportant2021年04月19日 22:02:59 +00:00Commented Apr 19, 2021 at 22:02