All submissions

Rotate Left in an 8-bit Register

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

uint8_t rotate_left(uint8_t reg, uint8_t n) {
    // Your code here
     int i=0;
    for(i=0;i<n;i++){
       int val = reg>>7&1; 
       reg = reg<<1;
       reg = reg | (val << 0);
        
    }
    return reg;
}

int main() {
    uint8_t reg, n;
    scanf("%hhu %hhu", &reg, &n);
    printf("%u", rotate_left(reg, n));
    return 0;
}

Solving Approach

 

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


uint8_t rotate_left(uint8_t reg, uint8_t n) {
    // Your code here
     int i=0;
    for(i=0;i<n;i++){
       int val = reg>>7&1; 
       reg = reg<<1;
       reg = reg | (val << 0);
        
    }
    return reg;
}


int main() {
    uint8_t reg, n;
    scanf("%hhu %hhu", &reg, &n);
    printf("%u", rotate_left(reg, n));
    return 0;
}

 

Step 1:- iterate the loop n times

for(i=0;i<n;i++) {

Step 2:- fetching the value in position 7

       int val = reg>>7&1; 

Step 3:- left shifting the reg values one time

       reg = reg<<1;

Step 4:- inserting val is left shifting  at zero position

       reg = reg | (val << 0);

 

 

Loading...

Input

176 1

Expected Output

97