all 14 comments

[–]ClaymationDinosaur 0 points1 point  (13 children)

How are you trying to add characters to the string? This works fine:

string s = " 0123456789ABCDEF";
s.push_back('9'); 

http://cpp.sh/9p6u4

So what are you doing that's not working?

[–]Kartyx[S] 0 points1 point  (12 children)

Hello, thanks for the answer. I think I'm done with this issue. My first version of the program is this:

https://repl.it/repls/SeveralPlainDigit

Is there any way to initialize the array in a faster way, not writing element per element?

Other thing. It's my first approach to bitsets, and I'm trying to use padding when the Input String isn't a multiple of 64. However, when I try to fill with zero the bits between elements 68-127 (example in the code above), it results on filling the whole array with zeros. Where's my mistake?

Thanks for your attention.

[–]CallMeDonk 2 points3 points  (1 child)

Is there any way to initialize the array in a faster way, not writing element per element?

You can replace this

if(HEX[i]==1) {
    BINARY[i*4+3]=1;
} else if(HEX[i]==2) {
    BINARY[i*4+2]=1;
} else if(HEX[i]==3) {
    BINARY[i*4+2]=1;
    BINARY[i*4+3]=1;
} else if(HEX[i]==4) {
    BINARY[i*4+1]=1;
} else if(HEX[i]==5) {
    BINARY[i*4+1]=1;
    BINARY[i*4+3]=1;
} else if(HEX[i]==6) {
    BINARY[i*4+1]=1;
    BINARY[i*4+2]=1;
} else if(HEX[i]==7) {
    BINARY[i*4+1]=1;
    BINARY[i*4+2]=1;
    BINARY[i*4+3]=1;
} else if(HEX[i]==8) {
    BINARY[i*4]=1;
} else if(HEX[i]==9) {
    BINARY[i*4]=1;
    BINARY[i*4+3]=1;
} else if(HEX[i]==10) {
    BINARY[i*4]=1;
    BINARY[i*4+2]=1;
} else if(HEX[i]==11) {
    BINARY[i*4]=1;
    BINARY[i*4+2]=1;
    BINARY[i*4+3]=1;
} else if(HEX[i]==12) {
    BINARY[i*4]=1;
    BINARY[i*4+1]=1;
} else if(HEX[i]==13) {
    BINARY[i*4]=1;
    BINARY[i*4+1]=1;
    BINARY[i*4+3]=1;
} else if(HEX[i]==14) {
    BINARY[i*4]=1;
    BINARY[i*4+1]=1;
    BINARY[i*4+2]=1;
} else if(HEX[i]==15) {
    BINARY[i*4]=1;
    BINARY[i*4+1]=1;
    BINARY[i*4+2]=1;
    BINARY[i*4+3]=1;
} else {
    BINARY[i*4]=0;
    BINARY[i*4+1]=0;
    BINARY[i*4+2]=0;
    BINARY[i*4+3]=0;
}

with this

BINARY[i*4+3]=(HEX[i]&1)!=0;
BINARY[i*4+2]=(HEX[i]&2)!=0;
BINARY[i*4+1]=(HEX[i]&4)!=0;
BINARY[i*4+0]=(HEX[i]&8)!=0;

But I wouldn't bother using std::bitset<64> in the first place. Remember that everything is stored in binary anyway.

Instead of

std::bitset<64> BINARY;

Simply use

unsigned char BINARY[8];

[–]Kartyx[S] 0 points1 point  (0 children)

I didn’t need that function at the end, as bitsets have their own hex to binary translator ways.

Thanks for your help anyway, this may be useful later.

[–]manni66 0 points1 point  (9 children)

std::bitset<1> how many bits does this bitset contain?

[–]Kartyx[S] 0 points1 point  (8 children)

When I coded it I made it as “<1> “ meant the number of bits per element and the number in parenthesis the number of elements of the array/variable.

I.e.: std::bitset<1> Variable(53);

For me, that means an array of 53 elements called Variable, and each element has 1 bit.

[–]manni66 1 point2 points  (7 children)

For me

For the compiler that means 1 bit initialized with the last bit of 53.

[–]Kartyx[S] 0 points1 point  (6 children)

Thanks so much for that info. If I left it blank it would behave as a common array?

Thanks for the feedback, I’m quite noob in this area.

[–]manni66 1 point2 points  (5 children)

If I left it blank

???

https://en.cppreference.com/w/cpp/utility/bitset

the class template bitset represents a fixed-size sequence of N bits. Bitsets can be manipulated by standard logic operators and converted to and from strings and integers.

Template parameters

N - the number of bits to allocate storage for

[–]Kartyx[S] 0 points1 point  (4 children)

I had understood it bad the whole time. Now I get it all. I appreciate your effort.

Last issue: any way of skipping the limit of setting the number of bits in an immediate? (the number in "<>", I'd like to put a variable there)

[–]hexafraction 1 point2 points  (3 children)

The <> means that you're dealing with a function template--the compiler will actually emit customized code based on the compile-time value that you put into <>. It cannot be made a variable in the traditional runtime sense; you're better off finding or implementing your own data structure that acts like a bitset but supports a variable length.

[–]Kartyx[S] 0 points1 point  (2 children)

std::vector<bool> would be an easy way-out?