Hi,
my question is about how to use bitwise operators on C++ std::string
... through overloading or as function does not matter
Example for an working XOR/^
function for std::string
:
std::string XOR(std::string value, std::string key)
{
std::string retval(value);
long unsigned int klen = key.length();
long unsigned int vlen = value.length();
unsigned long int k = 0;
unsigned long int v = 0;
for (; v < vlen; v++) {
retval[v] = value[v] ^ key[k];
k = (++k < klen ? k : 0);
}
return retval;
}
What I am missing now is a replacement for NOT/~
, AND/&
plus OR/|
.
Example C++ Code (the last two lines would like to have it... :-P):
//note: matrix_content[][] holds unsigned, *checked* integers...
// char(foo) works...
std::string vertical_master = "";
for (unsigned short int k = 0; k < axis_max; k++) {
for (unsigned short int l = 0; l < axis_max; l++) {
horizontal_master += char(matrix_content[l][k]);
vertical_master += char(matrix_content[k][l]);
}
}
std::string vertical_shift1_0 = vertical_master;
usigned short int bit = "@"; //<- just an example... chatched via
// std::string::substr() and std::string::c_str()
for (unsigned long int x = 0; x < axis_max; x++) {
vertical_shift1_0 += char(bit);
}
std::string vertical_shift2_0;
for (unsigned long int x = 0; x < axis_max; x++) {
vertical_shift2_0 += char(0);
}
vertical_shift2_0 += vertical_master;
std::string vertical_or = ~(vertical_shift1 | vertical_shift2);
std::string vertical_and = ~(vertical_shift1_0 & vertical_shift2_0);
In PHP/Perl I can do nasty :-P things like this
$vertical_shift1_0 = $vertical_master.str_repeat(chr(0), $axis_max);
$vertical_shift2_0 = str_repeat(chr(0), $axis_max).$vertical_master;
$vertical_or = chunk_split(~($vertical_shift1 | $vertical_shift2), $axis_max, chr(170));
$vertical_and = chunk_split(~($vertical_shift1_0 & $vertical_shift2_0), $axis_max, chr(170));
... and I'm missing it ;-). However XOR works. How to get AND/OR/NOT?