Answer to Question #5292 in C++ for aimelawan

Question #5292
5. Write a C++ program that can interleave bits of two entered 16 bit variable and stores the result in a 32 bit variable. For example, if user provides x and y, then the resulting number has bits of x at even position and bits of y at odd position. You will have to explore bitwise operators for the purpose
1
Expert's answer
2012-07-24T11:47:58-0400
#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int x, y, r;
r = 0;
cin >> x;
cin >> y;
for(int i = 15; i >= 0; i--){
int t = 1;
t = t << i;
r = r << 1;
t = t & y;
t = t >> i;
r += t;
r = r << 1;
t = 1;
t = t << i;
t = t & x;
t = t >> i;
r += t;
}
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS