A binary convolution is described by a number M, and is applied to a number N. For each bit in the binary representation of M, if the bit is set (1), the corresponding bit in the output is given by XORing the two bits adjacent to the corresponding bit in N (wrapping around when necessary). If the bit is not set (0), then the corresponding bit in the output is given by the corresponding bit in N.
A worked example (with 8-bit values):
- Let
N = 150,M = 59. Their binary respresentations are (respectively)10010110and00111011. - Based on
M's binary representation, bits 0, 1, 3, 4, and 5 are convolved.- The result for bit 0 is given by XORing bits 1 and 7 (since we wrap around), yielding
1. - The result for bit 1 is given by XORing bits 0 and 2, yielding
0. - The result for bit 2 is given by the original bit 2, yielding
1. - The result for bit 3 is given by XORing bits 2 and 4, yielding
0. - The result for bit 4 is given by XORing bits 3 and 5, yielding
0. - The result for bit 5 is given by XORing bits 4 and 6, yielding
1. - The results for bits 6 and 7 are given by the original bits 6 and 7, yielding
0and1.
- The result for bit 0 is given by XORing bits 1 and 7 (since we wrap around), yielding
- The output is thus
10100110(166).
The Challenge
Given N and M, output the result of performing the binary convolution described by M upon N. Input and output may be in any convenient, consistent, and unambiguous format. N and M will always be in the (inclusive) range [0, 255] (8-bit unsigned integers), and their binary representations should be padded to 8 bits for performing the binary convolution.
Test Cases
150 59 -> 166
242 209 -> 178
1 17 -> 0
189 139 -> 181
215 104 -> 215
79 214 -> 25
190 207 -> 50
61 139 -> 180
140 110 -> 206
252 115 -> 143
83 76 -> 31
244 25 -> 245
24 124 -> 60
180 41 -> 181
105 239 -> 102
215 125 -> 198
49 183 -> 178
183 158 -> 181
158 55 -> 186
215 117 -> 198
255 12 -> 243