Detect Circular Pattern Match

Code

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

uint8_t is_circular_match(uint16_t reg, uint16_t target) {
    // A 16-bit register has 16 possible circular rotations
    for (int i = 0; i < 16; i++) {
        if (reg == target) {
            return 1; // Match found
        }
        
        // Circular Left Shift logic:
        // (reg << 1) shifts everything left, leaving a 0 at the LSB.
        // (reg >> 15) moves the MSB to the LSB position.
        // Bitwise OR combines them.
        reg = (reg << 1) | (reg >> 15);
    }
    
    return 0; // No match after a full rotation
}

int main() {
    uint16_t reg, target;
    // Use %hu for unsigned short (uint16_t)
    if (scanf("%hu %hu", &reg, &target) == 2) {
        printf("%hhu", is_circular_match(reg, target));
    }
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

45056 11

Expected Output

1