Reputation: 171
was searching the web for an easy guide and couldn't find one.
I build a converter from decimal to binary and have the 1's and 0's saved in a vector. I want now to turn that vector into one integer. I found all kinds of ways (that I didn't really understand) to convert to string and than to integer, can that be avoided? I'd like to keep it as simple as possible.
here is my code:
c.hpp:
#ifndef C_HPP
#define C_HPP
#include <vector>
class Dualrechnung{
private:
std::vector<int> vec;
int z = 123456; //Eingegebene Zahl
int t_z = z; //temp Zahl
public:
Dualrechnung();
~Dualrechnung();
};
#endif
c.cpp:
#include <vector>
#include <cmath>
#include <sstream>
#include "c.hpp"
#include <iostream>
Dualrechnung::Dualrechnung()
{
int b;
for (int i=0; (t_z-(pow(2,i))) >= 0; i++) //finds biggest power of 2 in z
{
b= i;
}
t_z-=pow(2,b); //whats left after the biggest power
vec.push_back(1);
for(int i = b; i > 0; --i) //checks for every power of 2 multiples smaller than b if they exist in z. if they are, saves 1 for that power in the vector and if not, saves 0.
{
b--;
if((t_z-pow(2,b)) >= 0)
{
vec.push_back(1);
t_z-=pow(2,b);
}
else{vec.push_back(0);}
}
// here I'd like that integer with the information from the vector
int bin; //binary value of z
std::cout << z << " in Dual ist " << bin;
}
Dualrechnung::~Dualrechnung(){}
c_test.cpp:
#include "c.cpp"
#include <iostream>
int main(){
Dualrechnung *d = new Dualrechnung();
delete d;
return 0;
}
Upvotes: 0
Views: 2691
Reputation: 44238
If I understood you correctly you need to convert int
to set of bits and vice versa. Just use std::bitset
which has constructor from unsigned long
and method to convert it back (you can also convert it to/from string of 0 and 1s).
Upvotes: 2
Reputation: 659
I wrote smth like this for you:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> myNumInVect = {1, 1, 0, 0, 1};
int myNumInInteger = 0;
int baseOfMyNumInVector = 2;
for (int i : myNumInVect) {
myNumInInteger = myNumInInteger * baseOfMyNumInVector + i;
}
cout << myNumInInteger;
return 0;
}
(output of this code is 25 as expected (11001(2)=25(10))
it uses Horner's method, so number of operation '*' = vector.size(). It's easy adaptable to others bases positional numeral systems vector (by changing the baseOfMyNumInVector).
-----------@edit-----------
I know you have it, but i want also to show you how easy can be done conversion dec2givenBase without any pow, or smth like this:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int myNumInInteger = 111;
int baseOfMyNumInVector = 2;
vector<int> myNumInVect;
while (myNumInInteger > 0) { // using Horner Scheme for switching base
myNumInVect.push_back(myNumInInteger % baseOfMyNumInVector);
myNumInInteger /= baseOfMyNumInVector;
}
std::reverse(myNumInVect.begin(), myNumInVect.end()); // reverse (bcs there is no push_front() method in vector)
if (myNumInVect.empty()) { // handling the 0 case
myNumInVect.push_back(0);
}
for (auto digit : myNumInVect) { // write the result
cout << digit;
}
return 0;
}
Upvotes: 1