Question

In: Computer Science

Write a program that copies the even number of bits from a into the corresponding bits...

Write a program that copies the even number of bits from a into the corresponding bits in b ,only use bit-manipulation instructions, no loop
int main ()

{

uint_32 a = 0 xaabbccdd ;

uint_32 b = 0 x11223344 ;

// Replace bits 0 ,2 ,4 ,... of b with bits 0 ,2 ,4 ,... from a . uint_32 result = ...;

// Print out the result as a hexadecimal number

}

Solutions

Expert Solution

#include <bits/stdc++.h>
using namespace std;
  
//function to replace bits
unsigned int replaceBits(unsigned int x, unsigned int y)
{
// Getting all even bits of x
unsigned int even_bits = x & 0xAAAAAAAA;
  
// Getting all odd bits of y
unsigned int odd_bits = y & 0x55555555;
  
even_bits >>= 1; // Right shift will give even bits
odd_bits <<= 1; // Left shift will give odd bits
  
return (even_bits | odd_bits); // Combine even bits of x and odd bits of y
}
  
// Driver code
int main()
{
unsigned int x = 0xaabbccdd;   
unsigned int y = 0x11223344;
  
unsigned int num, temp, i = 1, j, r;
num = replaceBits(x,y); //stroing the decimal equivalent value after Replacing bits 0 ,2 ,4 ,... of b with bits 0 ,2 ,4 ,... from a
  
//below is the code to convert the decimal to hexadecimal
char hex[50];
temp = num;
while (temp != 0)
{
r = temp % 16;
if (r < 10)
hex[i++] = r + 48;
else
hex[i++] = r + 55;
temp = temp / 16;
}
cout << "\nHexadecimal equivalent of " << num << " is : "; //printing the hexadecimal value
for (j = i; j > 0; j--)
cout << hex[j];
  
return 0;
}

Please let me know if anything is required.


Related Solutions

Write a program that prompts the user for an even number from 2 to 100 until...
Write a program that prompts the user for an even number from 2 to 100 until the number 90 is encountered. Not including the 90, calculate the minimum value. In case you know what this means: DO NOT USE LISTS! We will look into the use of lists later. This has to be done in the python program. Here's what I have so far: inp = 0 min = 0 while inp != 90:     inp = int(input("Please enter an even...
2.c++ if and loop statement Write a program that will count the number of even number...
2.c++ if and loop statement Write a program that will count the number of even number and odd numbers between two inputted numbers. Display the numbers and compute the sum and average of all the even numbers and the sum and average all the odd numbers. Sample outputs: Enter starting number:3 Enter starting number:4 Enter ending number:10 Enter ending number:10 odd numbers Even number 3 4 5 6 7 8 9 10 number of even numbers=4 number of even numbers=4...
Write a program that processes numbers, corresponding to student records read in from a file, and...
Write a program that processes numbers, corresponding to student records read in from a file, and writes the required results to an output file (see main ( )). Your program should define the following functions: double read_double (FILE *infile) — Reads one double precision number from the input file. Note: You may assume that the file only contains real numbers. int read_integer (FILE *infile) - Reads one integer number from the input file. double calculate_sum (double number1, double number2, double...
Write a program which shows how two-dimensional arrays which contain multiple copies of your id number...
Write a program which shows how two-dimensional arrays which contain multiple copies of your id number and age are passed to methods. Explain in your own words each method and class used in the program. My id number is 12133149
Write a program which shows how two-dimensional arrays which contain multiple copies of your id number...
Write a program which shows how two-dimensional arrays which contain multiple copies of your id number and age are passed to methods. Explain in your own words each method and class used in the program.
Write a program that contains the following Write a function copies with two int parameters, named...
Write a program that contains the following Write a function copies with two int parameters, named n and x. It should dynamically allocate a new array of size n.  The function should then copy the value in x to every position in the array. The function should return a pointer to the new array. In the main function, call the copies function from part 1. with size 5 and value -1.  Output the resulting array to the screen, and deallocate the array....
Write a program in C++ that Copies Story.txt to DuplicateStory.txt. Each line in Story.txt will be...
Write a program in C++ that Copies Story.txt to DuplicateStory.txt. Each line in Story.txt will be written three times in DuplicateStory.txt.
In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
Program Name: Divisible. Write a program that calculates the number of integers in a range from...
Program Name: Divisible. Write a program that calculates the number of integers in a range from 1 to some user-specified upper bound that are divisible by 3, the sum of those integers, and the number and sum of those integers not divisible by 3. Your program must display meaningful output which includes: the selected upper bound number of integers divisible by 3 sum of integers divisible by 3 number of integers not divisible by 3 sum of integers not divisible...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT