Question

In: Computer Science

C++: how to do a bit rotation Left rotation of n = 11100101 by 3 makes...

C++: how to do a bit rotation

Left rotation of n = 11100101 by 3 makes n = 00101111 (Left shifted by 3 and first 3 bits are put back in last).
Right rotation of n = 11100101 by 3 makes n = 10111100 (Right shifted by 3 and last 3 bits are put back in first.

int left(int n, unsigned int d)

{

}

int right(int n, unsigned int d)

{

}

Solutions

Expert Solution

Full Program with the required functions:

#include<bits/stdc++.h>
using namespace std;

int left(int n, unsigned int d){
    return (n << d)|(n >> (8 - d));
}

int right(int n, unsigned int d){
    return (n >> d)|(n << (8 - d));
}

int main(){
    cout<<left(16,2)<<endl;
    cout<<right(16,2)<<endl;
}

Please refer to the screenshot of the code to understand the indentation of the code

Sample Run:

Hope this helped. Please do upvote and if there are any queries please ask in comments section.


Related Solutions

How to compute the Z, N, C, and Z the condition code bit values? Given A...
How to compute the Z, N, C, and Z the condition code bit values? Given A and B, and after subtracting how do I find the condition codes? Please provide a few examples with the explanation.
In addition, digits are added bit by bit from right to left with the carries being...
In addition, digits are added bit by bit from right to left with the carries being passed to the next digit to the left. Subtraction uses addition although the operand is negated before being added. Multiplication and division on the other hand can be much more complex. Research how both of these operations are handled at a hardware level. Provide an explanation of the algorithms that would be used to create those calculations.
how do i run 3 files on c++?
how do i run 3 files on c++?
An n-bit binary string is a sequence of length n over the alphabet {0,1}. How many...
An n-bit binary string is a sequence of length n over the alphabet {0,1}. How many n-bit binary strings are there? How many n-bit binary strings b1,…,bn are there such that b1b2≠00? In other words, how many n-bit binary strings don't begin with 00? How many n-bit binary strings b1,…,bn are there such that b1b2≠00 and b2b3≠11? How many n-bit binary strings b1,…,bn are there such that b1b2≠00 and such that b2b3≠01?
Explain the theory of designing a m-bit by n-bit binary divider.
Explain the theory of designing a m-bit by n-bit binary divider.
4- a) Draw a 4-Bit Shift Register that shifts from right to left instead of left...
4- a) Draw a 4-Bit Shift Register that shifts from right to left instead of left to right using D-Flip flops. b) Draw the serial transfer from shift register A on the right to the shift register B on the left in block form including the timing diagrammes. c) Draw the serial transfer table in the following form, assuming an initial value of 1101 in the register A and 0110 in the register B. Timing Pulse Shift Register B Shift...
\sum _{n=1}^{\infty }\:\frac{4\left(-1\right)^n+2^n}{3^n} Determine whether the series is convergent or divergent. If it is convergent, find...
\sum _{n=1}^{\infty }\:\frac{4\left(-1\right)^n+2^n}{3^n} Determine whether the series is convergent or divergent. If it is convergent, find its sum.
How do you use character table to generate a reducible representation of the 3n translational rotation...
How do you use character table to generate a reducible representation of the 3n translational rotation and vibrational degrees of freedom
Solve the recurrence equation. T(n) = 3T (n/3) + Cn T(1) = C
Solve the recurrence equation. T(n) = 3T (n/3) + Cn T(1) = C
Write the Verilog code for a 12-bit shift register with ability to shift left or right,...
Write the Verilog code for a 12-bit shift register with ability to shift left or right, synchronized parallel load, asynchronous reset, and serial output? The register is able to load the 12-bit input value (at the positive edge of the clock) if the load signal is one, shift the value in register one bit to the right (at the positive edge of the clock) if the rss signal is one, shift the value in register one bit to the left(at...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT