Like the assembly in C language, there is no operator to rotate the bits, so if we require rotating a bit, then we have to do it manually.
Basically, bit rotation is similar to the shift operation except that in shift operation the bits that fall off at one end are put back to the other end.
There are two types of rotation possible left and right. In the left rotation, the bits that fall off at the left end are put back at the right end and in the right rotation, the bits that fall off at the right end are put back at the left end.
Example: If data is stored using 8 bits, then the left rotation of a data 32(00100000) by 2 becomes 128 (10000000). As similar to left rotation, if data is stored using 8 bits, then the right rotation of the data 32(00100000) by 2 becomes 8 (00001000).
#include <stdio.h>
#define INT_BITS 32
#define ROTATE_LEFT(pos, data) ((data << pos)|(data >> (INT_BITS - pos)))
#define ROTATE_RIGHT(pos, data) ((data >> pos)|(data << (INT_BITS - pos)))
int main()
{
int pos = 2; // Number of rotation
int data = 32; //data which will be rotate
printf("%d Rotate Left by %d is ", data, pos);
printf("%d \n", ROTATE_LEFT(pos, data));
printf("%d Rotate Right by %d is ",data, pos);
printf("%d \n", ROTATE_RIGHT(pos, data));
return 0;
}
Compute the maximum number we have to write the below expression.
result = a ^ ((a ^ b) & -(a < b)); // max(a, b)
In above expression,if a > b, then -( a > b) become 0, so it behave like below expression
result = a ^ ((a ^ b) & -(0));
result = a ^ 0; // oring with 0 does not effect
result = a; //Maximum number
Like the assembly in C language, there is no operator to rotate the bits, so if we require rotating a bit, then we have to do it manually.
Basically, bit rotation is similar to the shift operation except that in shift operation the bits that fall off at one end are put back to the other end.
There are two types of rotation possible left and right. In the left rotation, the bits that fall off at the left end are put back at the right end and in the right rotation, the bits that fall off at the right end are put back at the left end.
Example:
If data is stored using 8 bits, then the left rotation of a data 32(00100000) by 2 becomes 128 (10000000). As similar to left rotation, if data is stored using 8 bits, then the right rotation of the data 32(00100000) by 2 becomes 8 (00001000).
Compute the maximum number we have to write the below expression.
need an explanation for this answer? contact us directly to get an explanation for this answer