Question

PRG255 3.2 (2 marks) Write a C program that uses the bitwise shift operators to shut the The program will ask the user to ent

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <stdio.h>
void to_binary(unsigned int num);
int main(void) {
unsigned int num, result;
int c, flag;
printf("Enter a number in range 0 to 65535:");
scanf("%d",&num);
printf("Input number of bits to shift:");
scanf("%d",&c);
printf("Shift to left (enter 0) or right (enter 1):");
scanf("%d",&flag);
if(flag==0) //shifts c bits to left
result=num<<c;
if(flag==1) //shifts c bits to right
result=num>>c;
printf("Result in Decimmal: %d",result);
to_binary(result);
return 0;
}
void to_binary(unsigned int num)
{
int bNum[32];
int i = 0;
while (num > 0)
{
bNum[i] = num%2;
num = num/2;
i++;
}
printf("\nResult in Binary:");
for (int j = i - 1; j >= 0; j--)
printf("%d",bNum[j]);
}

2 3 4 #include <stdio.h> void to_binary (unsigned int num); int main(void) { unsigned int num, result; int c, flag; printf(E

Outputs:

clang version 7.0.0-3-ubuntu0.18.04.1 (tags/RELEASE 700/f ? clang-7 -pthread -im -o main main.c } ./main Enter a number in ra

clang version 7.0.0-3-ubuntuo.18.04.1 (tags/RELEASE_700/f } clang-7 pthread -lm -o main main.c } ./main Enter a number in ran

Add a comment
Know the answer?
Add Answer to:
PRG255 3.2 (2 marks) Write a C program that uses the bitwise shift operators to shut...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program that uses the bitwise shift operators to shift the bits to the...

    Write a C program that uses the bitwise shift operators to shift the bits to the right >> or the left > m; /* This shifts m bits to the right, and the m least significant bits are lost.*/ The following statements are the same. num = num >> 3; num >>= 3; Show the operation in binary by calling the following function as defined in 3.1, void to_binary(unsigned int n); The function converts decimal to binary and outputs the...

  • (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt...

    (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....

  • CNIT 105 In Lab10 Due: By the end of your lab session OBJECTIVES: bitwise operations main...

    CNIT 105 In Lab10 Due: By the end of your lab session OBJECTIVES: bitwise operations main () function: 1. Declare an int variable, name it number. 2. Prompt the user to enter a whole number- read it into the variable. Validate the range in a loop. Valid range is 20 to 200 both inclusive . 3. Display the number to the screen (base 10) 4, Display the number in hexadecimal (Use %X) 5. Display number to the screen -It will...

  • 1. Prompt the user for one of the arithmetic operators ('op'): +, -, *,/, or **...

    1. Prompt the user for one of the arithmetic operators ('op'): +, -, *,/, or ** 2. Prompt the user for two input integers ('a' and'b') 3. The input numbers can be positive or negative 4. Perform the arithmetic operation using the two input numbers 5. The first entered number ('a') is the left side, the second ('b') the right side of the operation For exponentiation, the first number is the base, the second the exponent Print (display) the following...

  • C programming lab: Description: In this lab you will write a program that will contain two...

    C programming lab: Description: In this lab you will write a program that will contain two functions, setlsbs() and getlsbs(). These functions will use bitwise operators to embed and extract "hidden" bits in a character array. Write a program to test your functions as follows: Obtain a random number seed from the command line of your program using command line arguments. Initialize an array p of 8 unsigned char with random numbers from 0 to 255 Initialize a separate unsigned...

  • Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display...

    Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display the numbers in 3 formats. Then check, whether the larger of the 2 is evenly divisible by the smaller. Detail: Write a complete C program, more complex than the typical "hello world" program. Prompt the user to enter 2 integer numbers that are not negative. After either entry, display that number again. Print the smaller in hexadecimal, in decimal, and in octal format. Include...

  • Prime Number Programing in C Note: The program is to be written using the bitwise operation....

    Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • MIPS

    1.  Write an input-output utility io_lib.asm that contains the following subprograms:     a.  input_sub:  get user input          - prompt the user to enter an integer          - print the entered input as hexadecimal in the format of:  0xnnnnnnnn      b.  output_sub:  print output          - print a text string provided by the caller          - print the value in hexadecimal format 2.  Write a logic operation utility logic_lib.asm that contains the following...

  • write a C program that evaluate algebraic expressions over real numbers in Reverse Polish Notation (RPN)

    Your program must evaluate algebraic expressions over real numbers in Reverse Polish Notation (RPN). + (addition), - (subtraction), * (multiplication) and / (division) should be treated as arithmetic operations. Depending on the selection, numerical values must be entered and displayed in decimal, hexadecimal or binary form. Example: The expression ((3+4) *(7+1.6-12) -5) / (3-8.7)Will be calculated on your RPN calculator as 3 4 + 7 1.6 + 12 - * 5 - 3 8.7 - / Your RPN computer's user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT