0% found this document useful (0 votes)
13 views

Bitwise Class Activity Problem:: Programming Homework Help - Expert Homework Helpers

C Homework Help
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Bitwise Class Activity Problem:: Programming Homework Help - Expert Homework Helpers

C Homework Help
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Problem:

Bitwise Class Activity


• Write a 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 perform the
bitwise operation.
• Print out the resulting integer value in
decimal.
• No get opt is needed.
• Use integer value 8, use left shift, use 2
bits for how many.

© 2022 All Rights Reserved.


Programming Homework Help | Expert Homework Helpers
Solution:
#include <stdio.h>
 
int main(int argc, char** argv)
{
char buffer[50];
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
© 2022 All Rights Reserved.
Programming Homework Help | Expert Homework Helpers
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);
}
}

© 2022 All Rights Reserved.


Programming Homework Help | Expert Homework Helpers

You might also like