# Bitwise Operator using C Homework solution

## Bitwise Class Activity

• Write a C assignment program that will ask the user for an unsigned integer.
• Then ask the user whether they want to shift right or left.
• Then ask them how many bits to shift it.
• After getting all the values to perform the bitwise operation.
• Print out the resulting integer value in decimal.
• No getopt is needed.
• Use integer value 8, use left shift, use 2 bits for how many.

Solution:```#include < stdio.h> int main(int argc, char** argv) { char buffer; unsigned val; char dir; int amount; int result; printf("Please enter an unsigned integer: "); //Read in string fgets(buffer, sizeof(buffer) - 1, stdin); //Get int from string sscanf(buffer, "%u", &val); printf("Shift left or right? (L or R): "); //Read in string fgets(buffer, sizeof(buffer) - 1, stdin); //Get char from string sscanf(buffer, "%c", &dir); printf("How many bits to shift: "); //Read in string fgets(buffer, sizeof(buffer) - 1, stdin); //Get int from string sscanf(buffer, "%d", &amount); //Shift Left if (dir == 'l' || dir == 'L') { result = val << amount; printf("Shifting %u left by %d gives %d\n", val, amount, result); } //Shift Right else { result = val >> amount; printf("Shifting %u right by %d gives %d\n", val, amount, result); } } ```