// Arup Guha
// 9/28/2020
// Code showing use of bitwise operators in C

#include <stdio.h>

int main(void) {

    int x = 167, y = 50;

    // Print out examples using x and y.
    printf("%d and %d = %d\n", x, y, x&y);
    printf("%d or %d = %d\n", x, y, x|y);
    printf("%d xor %d = %d\n", x, y, x^y);

    // show bit shifting.
    printf("%d right shifted by %d bits is %d\n", 113, 3, 113>>3);
    printf("%d right shifted by %d bits is %d\n", 108, 3, 108>>3);
    printf("%d left shifted by %d bits is %d\n", 11, 4, 11<<4);

    // Set up a 16 bit block.
    unsigned short block;
    block = (1<<0) + (1<<5) + (1<<8) + (1<<11) + (1<<14) + (1<<15);

    // 8 bit cyclic right shift.

    // least significant bits (which would fall off)
    unsigned short right8 = ((1<<8)-1) & block; //33

    // Regular right shift.
    unsigned short left8 = block >> 8;  // 201

    // Take the old right 8 bits and move them to the left half.
    // Keep the old left half 8 bits and OR them to the right side.
    unsigned short result = (right8 << 8) | left8;

    // Show each part.
    printf("%u %u %u\n", right8, left8, result);

    return 0;
}
