0% found this document useful (0 votes)
28 views3 pages

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

The document describes a programming assignment that requires writing a C program to perform bitwise operations on an unsigned integer based on user input. The program prompts the user for an integer, the direction of the shift (left or right), and the number of bits to shift. It then performs the specified operation and prints the resulting integer in decimal format.
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)
28 views3 pages

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

The document describes a programming assignment that requires writing a C program to perform bitwise operations on an unsigned integer based on user input. The program prompts the user for an integer, the direction of the shift (left or right), and the number of bits to shift. It then performs the specified operation and prints the resulting integer in decimal format.
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