#include <stdio.h>
#include <stdint.h>
uint8_t is_circular_match(uint16_t reg, uint16_t target) {
for (int i = 0; i < 16; i++) {
uint16_t rotated = (reg << i) | (reg >> (16 - i));
if (rotated == target)
return 1;
}
return 0;
}
int main() {
uint16_t reg, target;
scanf("%hu %hu", ®, &target);
printf("%hhu", is_circular_match(reg, target));
return 0;
}
Great — let’s break down the solving approach for detecting a circular pattern match between two 16-bit values: reg
and target
.
🧠 Problem Understanding
You’re given:
reg
)target
)You need to check whether any circular left rotation of reg
results in a value equal to target
.
🔁 What Is Circular Rotation?
A circular left rotation by i
bits means:
0b1011000000000000
left by 13 bits gives 0b0000000000001011
.✅ Step-by-Step Approach
Use bitwise operations:
rotated = (reg << i) | (reg >> (16 - i));
Ensure the result stays within 16 bits:
rotated &= 0xFFFF;
rotated == target
, return 1
(match found).0
.🧪 Time Complexity
Input
45056 11
Expected Output
1