Question

In C programming language, Use Bitwise operations to do the operations below. Assume all variables are...

In C programming language,

  1. Use Bitwise operations to do the operations below. Assume all variables are unsigned char (1 byte).
    1. Change variable x so that its 2 right-most bits become 0.
    2. Change the variable x so that its 2 left-most bits become 1.
    3. Rotate variable x to the right by 3 bits.
    4. x = y *4;
    5. x = y / 4;
0 0
Add a comment Improve this question Transcribed image text
Answer #1

******************************************************************************************
Please Upvote the answer as it matters to me a lot :)
*****************************************************************************************
As per HomeworkLib expert answering guidelines,Experts are supposed to answer only certain number of questions/sub-parts in a post.Please raise the remaining as a new question as per HomeworkLib guidelines.
******************************************************************************************

#include <stdio.h>
int main()
{
unsigned char x;
x=123;
// x= 01111011 in binary
x= (x >>2)<<2;
printf("%d\n",(int)x);
// cout<<(int)x<<endl;
// binary representaion of 120 is 01111000
x= 123;
x= x|(3<<6);
// (33<<6) = 11 left shifted 6 times which gives 11000000
printf("%d\n",(int)x);
// cout<<(int)x<<endl;
//251 = 11111011 in binary
x=7;
x=(x >> 3)|(x << (8 - 3));
printf("%d\n",(int)x);
// cout<<(int)x<<endl;
//224= 11100000
int y=6;
x= y<<2;// y*4
x= y/4;// y/4
return 0;
}

Add a comment
Know the answer?
Add Answer to:
In C programming language, Use Bitwise operations to do the operations below. Assume all variables are...
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
  • 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...

  • 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...

  • Question 4 (2 points) Consider the C statements below. Assume x has been assigned a value....

    Question 4 (2 points) Consider the C statements below. Assume x has been assigned a value. int mask = 0xFF000000; y = x ^ mask; Which one of the following best describes the value computed for variable y? Question 4 options: y contains the most significant byte of x complemented, with the rest of the bits unchanged y contains the most significant byte of x unchanged, with the rest of the bits 0 y contains the most significant byte of...

  • First, review your C language data types Learn how to use the strtok() function in C language. Th...

    First, review your C language data types Learn how to use the strtok() function in C language. There are plenty of examples on the Internet. Use this function to parse both an example IP address, eg. 192.168.1.15, into four different strings. Next use the atoi() function to convert these into four unsigned char's. (Hint: you will need to typecast, eg unsigned char X-(unsigned char)atoi("234"); Now, if you view a 32 bit number in base 256, the right most column would...

  • PRG255 3.2 (2 marks) Write a C program that uses the bitwise shift operators to shut...

    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 enter an unsigned also how many bits for the shift operation. Display the entered operation in both decimal and binary formats, vise shirt operators to shift the bits to the right >> or the left << user to enter an unsigned integer number, choose right shift or left shift, and orauon. Display the entered number and...

  • 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...

  • (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....

  • 4. [10 marksLet us consider a system where every integer is unsigned and uses 11 bits....

    4. [10 marksLet us consider a system where every integer is unsigned and uses 11 bits. Most programming languages provide ways to manipulate the bits of integers (signed or unsigned). For instance, if r and y are two such integers, then • & is the bitwise AND operator. r&y denotes the integer whose ith bit is the ith bit of and the ith bit of y. For instance, assuming that we have unsigned integers 145310 101101011012 and 78810 = 011000101002,...

  • In this assignment, you must complete the 5 functons below. Each function's body must consist of...

    In this assignment, you must complete the 5 functons below. Each function's body must consist of just a single C statement. Furthermore, the statement must not be a control statement, such as if, switch, or any kind of loop. For each problem, you are also restricted to the operators and constants as specified. */ /* 1. Write a function which zeros a variable x. A pointer to x is passed as a parameter. YOU MAY USE NO CONSTANTS in this...

  • In the C language, Write a program that inputs 4 hexadecimal digits as strings (example 7F),...

    In the C language, Write a program that inputs 4 hexadecimal digits as strings (example 7F), converts the string digits to a long – use strtol(input string,&ptr,base) to do the conversion. Just declare ptr as a char ptr, don’t worry about what it does. Copy each converted number [ a long] into unsigned char’s [like the variable num1 below – hint: cast a long variable into the unsigned char’s] before performing the following logical operations on them (parenthesis might help,...

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