Detect Circular Pattern Match

Code

#include <stdio.h>
#include <stdint.h>

#define ROTATE(reg, pos) (((reg << pos) | (reg >> (16 - pos))) & 0xFFFF)

uint8_t is_circular_match(uint16_t reg, uint16_t target) {
    for (int pos = 0; pos < 16; pos++) {
        if (ROTATE(reg, pos) == target) {
            return 1;
        }
    }
    return 0;
}

int main() {
    uint16_t reg, target;
    scanf("%hu %hu", &reg, &target);
    printf("%hhu", is_circular_match(reg, target));
    return 0;
}

Solving Approach

This code checks if a 16-bit target pattern can be obtained by any circular left rotation of a 16-bit register value. It defines a macro to perform the rotation and iterates through all 16 possible rotation positions, comparing each result to the target. If a match is found, it returns 1; otherwise, it returns 0. The main function reads the register and target values from input and prints the result of the circular match check.

 

 

Upvote
Downvote
Loading...

Input

45056 11

Expected Output

1