Detect Circular Pattern Match

Code

#include <stdio.h>
#include <stdint.h>
//for left rotation
#define SHIFT_LEFT(reg,n) (reg<<n)
#define WRAP_RIGHT(reg,n) (reg>>(16-n))
#define MASK ((1U<<16)-1)
#define ROTATE_LEFT(reg,n) (SHIFT_LEFT(reg,n) | WRAP_RIGHT(reg,n))&(MASK)
uint8_t is_circular_match(uint16_t reg, uint16_t target) {
    // Your code here
    uint16_t rotated_reg = 0;
    for(uint8_t i=0;i<16;i++){
        rotated_reg = ROTATE_LEFT(reg,i);
        if(rotated_reg == 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

 

 

 

Upvote
Downvote
Loading...

Input

45056 11

Expected Output

1