C++/C++

Advertisement


Question
Hi, I am a university student now, and I want to write a C/C++ for uintvar, please help me to write this C++ program, I have no idea what to do, Thanks a lot
some information about uintvar
Representing a value as a uintvar: Write down the binary representation of the given value. Group them into 7-bit groups starting with the lowest significant bit. For example, 137 is 10001001. Grouped into 7-bit groups, this will be 1:0001001. For each group, except the last group, add an eighth bit at MSB and set it to 1. For the last group, add the eighth bit at MSB as before but set it to 0. Our 137 example, then becomes 10000001:00001001. In other words, the uintvar corresponding to 137 (89 in hex) has two bytes: 81 and 9, both in hex.

Thanks for your help

Answer
Hello,

Try this code:


#include <iostream>
#include<bitset>
void convertToUnitVar(int num);

using namespace std;
int main (int argc, char * const argv[]) {
  
int num;
cout << "Enter the number"<<endl;
cin>>num;
convertToUnitVar(num);
  //return 0;
system("pause");

 
}
void convertToUnitVar(int num)
{
int value;
bitset<8>store;
bitset<8>bitsetObject(num);
cout <<bitsetObject<<endl;
int val = bitsetObject.size();

for(int i =0;i<=(val-1); i++)
{
store[i]=bitsetObject[i+1];
if(i==6)break;
}

bitsetObject[val-1] = bitsetObject[0];
for (int i=6; i>=0; i--) {
bitsetObject[i] =0;
}

cout<<"unitvar corresponding to "<<num<< " has two bytes: "<<bitset<8>(bitsetObject).to_ulong()<<"and "<<bitset<8>(store).to_ulong()<<endl;

}  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


henry

Expertise

I can confidently answer any question both basic and advanced in C++. Areas like templates, algorithms, Gui and design patterns are of interest to me. i will not answer questions about web programming.

Experience

I have a 3- year- work experience in software Engineering.

Education/Credentials
I am a bachelor of Engineering graduate

©2016 About.com. All rights reserved.